Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Mar 25, 2020 12:11 pm

Hi
How I can Insert an Image in existing PDF file under specific text...

hamzasukkar
 
Posts: 16
Joined: Wed Mar 25, 2020 8:47 am

Thu Mar 26, 2020 2:29 am

Hi,

Thanks for your post.
Please note that for inserting an image into an existing pdf document, you can only draw this image in a blank space on the page. If you want to implement the following content after inserting the image move down automatically. I am sorry that this is not achievable in the PDF document. Because of the structure of the PDF document is not like Word document, the Word document is a flow document, when inserting an image into a Word document, the following content can move down automatically but in the PDF document, all elements are fixed one-to-one corresponding coordinates on the page. When drawing an image in the area where the content exists, the original content will be covered.

Here is a demo showing drawing an image in the blank space under the specified text. The test document is attached.
Code: Select all
PdfDocument pdf = new PdfDocument(folder + "source.pdf");
            //Get the first page
            PdfPageBase page = pdf.Pages[0];
            PdfTextFind[] finds = page.FindText("Here is an image", TextFindParameter.IgnoreCase).Finds;
            float X =finds[0].Bounds.X;
            float Y = finds[0].Bounds.Y + finds[0].Bounds.Height;
           
            PdfImage image = PdfImage.FromFile(folder + "E-iceblueLogo.png");
            page.Canvas.DrawImage(image, X, Y + 2, image.Width, image.Height);
            String output = "output.pdf";
            pdf.SaveToFile(output);


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Thu Mar 26, 2020 7:36 am

Thank You :)
I'ts work
Can you please write this Code in VB....
Thank you

hamzasukkar
 
Posts: 16
Joined: Wed Mar 25, 2020 8:47 am

Thu Mar 26, 2020 7:53 am

Hi,

Thanks for your feedback.
Glad to hear that it works.
Here is code in VB.net.
Code: Select all
 Dim pdf As PdfDocument = New PdfDocument((folder + "source.pdf"))
        Dim page As PdfPageBase = pdf.Pages(0)
        Dim finds() As PdfTextFind = page.FindText("Here is an image", TextFindParameter.IgnoreCase).Finds
        Dim X As Single = finds(0).Bounds.X
        Dim Y As Single = (finds(0).Bounds.Y + finds(0).Bounds.Height)
        Dim image As PdfImage = PdfImage.FromFile((folder + "E-iceblueLogo.png"))
        page.Canvas.DrawImage(image, X, (Y + 2), image.Width, image.Height)
        Dim output As String = "output.pdf"
        pdf.SaveToFile(output)


Feel free to contact us if you need any assistances.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Mon Apr 20, 2020 5:28 am

Thank you

hamzasukkar
 
Posts: 16
Joined: Wed Mar 25, 2020 8:47 am

Mon Apr 20, 2020 8:24 am

Hi,

You are welcome.
If you need any other help, please feel free to contact us.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF