Replace Images from PDF

Get technical support of Document .Net in C# and VB.Net
iguron
Posts: 2
Joined: Mon Jun 07, 2021 9:47 am
Contact:

Replace Images from PDF

Post by iguron »

Hi guys,

First of all excellent and nice product.

We are evaluating your product and for sure we would buy a Document.net license soon.

during our testings we have only one thing missing.

We have not been able to replace one image inside a pdf. We have a pdf with some picture items, and after extracting the picture and modify it we want to replace the existing one by the new one.

Is it possible?
Oliver
Posts: 27
Joined: Wed Aug 19, 2020 11:59 am
Contact:

Re: Replace Images from PDF

Post by Oliver »

Hi.
Yes. It's possible to do.
But If you want to replace an image, you need to know its characteristics.
Those, to find an image and replace it with another picture, we need to know its features. How can we identify it in the PDF file?
Or you know any text before, after this image?

I may offer you the similar code sample :

Code: Select all

DocumentCore dc = DocumentCore.Load(loadPath);

            Regex regex = new Regex(@"<image>", RegexOptions.IgnoreCase);
            Picture pict = new Picture(dc, InlineLayout.Inline(new Size(50, 10)), pictPath);
            foreach (ContentRange item in dc.Content.Find(regex).Reverse())
            {
                item.Replace(pict.Content);               
            }
Guest

Re: Replace Images from PDF

Post by Guest »

Thanks Oliver for the quick answer.

We need to get the original image, add some shapes to the image, and the resulting image replace the original one.

so in your example, first we need get the image in a bitmap or picture, make some funny stuff and then replace the image...
iguron
Posts: 2
Joined: Mon Jun 07, 2021 9:47 am
Contact:

Re: Replace Images from PDF

Post by iguron »

Hi Oliver,

following your example, how can we convert "item" to picture in order to modify the image, before replace?
Oliver
Posts: 27
Joined: Wed Aug 19, 2020 11:59 am
Contact:

Re: Replace Images from PDF

Post by Oliver »

Ok. You need to use this code. You will find all Pictures in the Document (PDF, RTF, DOCX) and replace these images on another Pictures.

Code: Select all

// Path to a loadable document.
            string loadPath = @"d:\download\smile.pdf";
            string pictPath = @"..\..\ignature.png";

            // Load a document intoDocumentCore.
            DocumentCore dc = DocumentCore.Load(loadPath);
            Picture picture = new Picture(dc, InlineLayout.Inline(new Size(50, 50)), pictPath);
          
            foreach (Element el in dc.GetChildElements(true,  ElementType.Picture).Reverse())
            {
                {
                  
                    {
                      el.Content.Replace(picture.Content);
                    }
                }
            }

            // Save our document into PDF format.
            string savePath = @"..\..\Replaced.pdf";
            dc.Save(savePath, new PdfSaveOptions());
MaxSautin
Posts: 1
Joined: Wed May 19, 2021 1:07 pm
Contact:

Re: Replace Images from PDF

Post by MaxSautin »

I want to add some extra staff, first of all you can get the image data, load it, next create a new picture and replace the old.

Code: Select all

            foreach (Element el in dc.GetChildElements(true, ElementType.Picture).Reverse())
            {
                if (el is Picture)
                {
                    Picture pict = el as Picture;
                    // Get image bytes
                    var bytes = pict.ImageData.ImageBytes;

                    // Do some staff with picture
                }                        
            }
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests