Server side editing an existing pdf

5 Answers 1057 Views
PdfProcessing
Bhusan
Top achievements
Rank 1
Bhusan asked on 06 May 2021, 06:09 AM

I've a .NET core web-api project and I need to edit the existing pdf (like adding an image/ text) without using UI. The pdf files are stored in Azure Cloud which I can access through URI.

So, is that doable? Would appreciate if you could point me to the right Telerik libraries and any examples would be great.

 

5 Answers, 1 is accepted

Sort by
1
Dimitar
Telerik team
answered on 11 May 2021, 12:18 PM

Hi Bhusan,

You can use NamedDestination for this. First, you need to add the destination and specify the location where you want to insert the content: 

var doc = provider.Import(File.ReadAllBytes(@"..\..\SampleDoc.pdf"));

doc.NamedDestinations.Add("ContentLocation", new Location() { Page = doc.Pages[0], Left = 50, Top = 150 });

Then when the document is loaded you can retrieve the location and draw the desired content: 

NamedDestination destiantion = null;
doc.NamedDestinations.TryGetValue("ContentLocation", out destiantion);

if (destiantion != null)
{
    var loc = destiantion.Destination as Location;
    var editor = new FixedContentEditor(destiantion.Destination.Page);

    editor.Position.Translate(loc.Left.Value, loc.Top.Value);
    editor.DrawText("Test");
}

File.WriteAllBytes(@"..\..\result.pdf", provider.Export(doc));

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Bhusan
Top achievements
Rank 1
commented on 11 May 2021, 02:11 PM

Hi Dimitar,

This question might sound silly but please bear with me.
In my case, the NamedDestinations needs to be added manually. So, I cant use the following line.

doc.NamedDestinations.Add("ContentLocation", new Location() { Page = doc.Pages[0], Left = 50, Top = 150 });

So, I tried adding NamedDestinations using Adobe Acrobat. But when I load the pdf, it detects the NamedDestination but the destination value is always null. So, is there a way to specify the location using Adobe Arcobat?

Also, even though the image drwan is png which has a transparent background, When drawn on the pdf, the background is not transparent. How to solve this issue?
0
Dimitar
Telerik team
answered on 06 May 2021, 01:11 PM

Hi Bhusan,

Yes, this is supported. You can use the RadPdfPRocessing library. To add content to an existing document you can use the FixedContentEditor. Detailed information is available in the following articles: 

In this case, you need to use the version for Net Standard since it does not depend on any Windows assemblies: NuGet Packages.

Examples are available in our SDK repository as well: document-processing-sdk/PdfProcessing at master · telerik/document-processing-sdk

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Bhusan
Top achievements
Rank 1
commented on 10 May 2021, 08:36 AM

I've an existing pdf. In there are fields, Date and Company Name . What I need to do is stamp Date and Company Name in that specific location. I don't have the coordinates.
So, how can I achieve this?
0
Dimitar
Telerik team
answered on 11 May 2021, 05:09 AM

Hello Bhusan,

Yes, this is doable, but you will need to download the file. I do not think that it can be edited remotely. And this is the exact library that can do this. We do not have any example that involves Azure Cloud. You can check the links in my previous post to see how you can edit the documents. To import the file you need a stream or a byte array: Using PdfFormatProvider.

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

Bhusan
Top achievements
Rank 1
commented on 11 May 2021, 05:16 AM

Hi Dimitar,

Okay, I'll can import the file using stream or byte array. But the problem is I need to stamp an image in the pdf at a definite location of which I don't have the coordinates. The pdf will contain a drawn line where I need to stamp an image. So, how do I get the coordinates of that line so that I can stamp an image in there?
0
Dimitar
Telerik team
answered on 11 May 2021, 09:17 AM

Hi Bhusan,

You can use the following approach to iterate all elements and find the line: 

var provider = new PdfFormatProvider();
var doc = provider.Import(File.ReadAllBytes(@"..\..\SampleDoc.pdf"));
foreach (var page in doc.Pages)
{
    foreach (var element in page.Content)
    {
        Console.WriteLine(element);
        if (element is Telerik.Windows.Documents.Fixed.Model.Graphics.Path)
        {
            var path = element as Telerik.Windows.Documents.Fixed.Model.Graphics.Path;
            Console.WriteLine(path.Position);
        }
    }
}

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Bhusan
Top achievements
Rank 1
commented on 11 May 2021, 10:38 AM

if (element is Telerik.Windows.Documents.Fixed.Model.Graphics.Path)

I don't understand what this line does. Can you explain what Telerik.Windows.Documents.Fixed.Model.Graphics.Path means? Also, can we use some kind of tag or annotation to get specific location of the pdf? If yes, how can I?

Thanks,
0
Dimitar
Telerik team
answered on 12 May 2021, 08:55 AM

Hello Bhusan,

If you cannot add the named destinations, I would suggest adding a form field (like a button for example) or a bookmark. You can remove either of them later when adding the content. Is this possible for you?

In Net Standart, we are converting the images to Jpeg and this is why the transparency is lost. If you are using a Windows server or environment for this you can use the Windows assemblies where this works out of the box. In addition, I have logged this on our feedback portal. You can track its progress, subscribe to status changes, and add your comment to it here. I have updated your Telerik points as well. 

Unforatutelly I cannot suggest a temporary solution. 

I am looking forward to your reply.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Bhusan
Top achievements
Rank 1
commented on 12 May 2021, 12:01 PM | edited

Thanks, finally done.
Tags
PdfProcessing
Asked by
Bhusan
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or