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 Dec 02, 2020 5:09 pm

Hi All,

I'm a new member and this is my first post. I'm having trouble determining how to find the beginning of the content of a page (i.e. the point at the very start, taking margins into account).

Here's ultimately what I'm trying to accomplish: I need to insert/stamp/imprint an image at the beginning of each page of a pdf. It needs to be flush with the left margin, and the image size will be adjusted to fit within the top margin of the page. It's essentially letterhead.

My plan for solving this is to find the x and y coordinates of the start of the content of the page, adjust the image height to fit within the top margin, and "place" the image above the x,y coordinates.

I've searched this forum without success (searching "find page margins" among others). Also, it doesn't look like I can effectively search the api documentation. It's structured by namespace, but I have no idea which namespace a method/property like this would be in. I have also gone through the Spire.PDF Program Guide without success.

Any help would be much appreciated.
Thanks,
Pete

Pete2870
 
Posts: 2
Joined: Wed Dec 02, 2020 4:08 pm

Thu Dec 03, 2020 3:36 am

Hello,

Thanks for your inquiry.

According to your description, please refer to the following code to draw the picture in Pdf, I have attached my output files. If the code doesn’t meet your needs, please provide us with your input file and the excepted result for further investigation. Thanks in advance.
Code: Select all
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(@"E:\testdoc\sample.pdf");

            foreach(PdfPageBase page in pdf.Pages)
            {
                float width = page.Size.Width;
                float height = page.FindAllText().Finds[0].Bounds.Y;
                PdfImage image = PdfImage.FromFile(@"E:\downapp\pic\color.jpg");

                page.Canvas.DrawImage(image, 0, 0, width, height);
            }

            pdf.SaveToFile("addpictoExist.pdf", FileFormat.PDF);


If you have any other questions, please feel free to contact us.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Dec 04, 2020 10:03 pm

Thanks for the help, Marcia.

I tried it and it did write the image on each page of the pdf. However, the image spans the entire width of the page. I need it to be placed as-is, flush with the left margin. The attached zip file has screenshots of the output pdf file. "SpireVersion.png" shows how it turned out. "OtherVendorVersion.png" is how we would like it to look. As you can see, the image is flush with the left margin, and does not span the page.

I wish I could include the actual pdf's, but they contain sensitive information that cannot be disclosed.

Another issue with the output pdf is, when I open it by double-clicking the file, the layout of of Adobe Reader is quite a bit different than it is for the other file. The toolbar is missing most of the buttons that are displayed in the other. There is also a "Cover sheet" header below the toolbar, and I cannot adjust the view of the pages. Please see the screenshots in the attached file.

I have included the 2 image files that could be used: "AspirusLogo3.png" and "P1Logo.png" (the user selects one at runtime).

How can I place it in the location I want it to be (flush with the left margin)? Can it be pasted as-is, and not spanned across the page?

Thanks in advance for your help.

Pete

P.S. I could not open the zip file you attached (access denied). I hope you don't have the same problem with mine! :)

Pete2870
 
Posts: 2
Joined: Wed Dec 02, 2020 4:08 pm

Mon Dec 07, 2020 3:47 am

Hello,

Thanks for your message and sorry for the late reply as weekend.

Based on the expected result pictures you provided, I have modified the code, please refer to the following code. Besides, if the toolbar still does not show up, please provide us with your version of Adobe reader.
Thanks in advance.

Code: Select all
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(@"E:\testdoc\sample.pdf");

            foreach(PdfPageBase page in pdf.Pages)
            {
               
                PdfImage image = PdfImage.FromFile(@"E:\testdoc\Visuals\P1Logo.png");
                //set image x coordinates
                float x = page.FindAllText().Finds[0].Bounds.X;
                //set image width
                float width = image.Width;
                //set image height
                float height = image.Height;
                //set image y coordinates
                float y = page.FindAllText().Finds[0].Bounds.Y - height-2;
                //draw image
                page.Canvas.DrawImage(image, x, y, width, height);
            }

            pdf.ViewerPreferences.HideMenubar = false;
            pdf.ViewerPreferences.HideToolbar = false;
            pdf.ViewerPreferences.HideWindowUI = false;

            pdf.SaveToFile("addpictoExist.pdf", FileFormat.PDF);
            pdf.Close();


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Wed Dec 09, 2020 10:04 am

Hello,

Hope you are doing well.

How is your issue now? If the toolbar still does not show up, Could you please provide us with your version of Adobe reader for further investigation?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.PDF