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.

Tue Jul 06, 2021 4:28 pm

Hey,

I have a PDF document with a single page I am trying to setup as a layer.

So I can put a second layer with text over the top then hide the first layer.

I figured the best way would be to take an image of the first PDF doc and have it as a full page image to ensure that the location of text is acurate when printed.

however I am struggling to get the top left corner of the image in the top left corner of the document...

I tried setting margins to 0 but no matter what I try the image is offset, and even though I set the image size to the height and width of the page canvas it does not appear to be the correct size...

not sure what I am missing.

I also tried setting the image as a page background but that didn't work at all, just provided a white background.

The layer portion is working as I want (able to toggle the image viability on and off), so if I can just figure out the sizing, positioning and margins issue I should be good :)

Code: Select all
           
            //Setup PDF
            PdfDocument MyPDF = new PdfDocument();
            PdfPageBase MyPage = MyPDF.Pages.Add();


            //Load 1500 form background
            Spire.Pdf.Graphics.Layer.PdfLayer Layer1500 = MyPDF.Layers.AddLayer("1500");
            PdfCanvas PageCanvas = Layer1500.CreateGraphics(MyPDF.Pages[0].Canvas);
            Image image = Image.FromFile(@"D:\CMS-1500.png");
            PdfImage pdfimage = PdfImage.FromImage(image);


            PointF position = new PointF(0, 0);
            int width = image.Width;
            int height = image.Height;
            PageCanvas.DrawImage(pdfimage, position);.DrawImage(pdfimage, position);


CMS-1500.png

1500 preview.png

Thandius
 
Posts: 11
Joined: Tue Jun 16, 2020 2:56 pm

Wed Jul 07, 2021 10:40 am

Thanks for your inquiry.
I tested your case on my side and found that the picture is actually drawn from the top left corner. The method doc.Pages.Add() will create an A4 size PDF page with 40 point margins by default. Also I noticed that your picture itself has white area around, which caused your misunderstanding. I suggest that you remove the blank area from your image and then add Pdf page with 0 margin (doc.Pages.Add(PdfPageSize.A4,new PdfMargins(0))) to have a try.
Besides, I used the following code to set the image as background, but still did n't find any problem. The version I used is the latest Spire.PDF (Spire.PDF Pack(Hot Fix) Version:7.6.15 ). Here I also attached my generated file for your reference.
Code: Select all
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
//Load an image
Image backgroundImage = Image.FromFile("CMS-1500.png");
page.BackgroundImage = backgroundImage;
pdf.SaveToFile("output.pdf");

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Wed Jul 07, 2021 12:04 pm

I am using the freespire office which I think has a much older version of the PDF dll

I have found a work around, I was able to get the original PDF and setup the contents on their own layer.

Then I can just draw on a second layer over the top without having to put an image on the background.

Which achieves the same result (in fact it works out better).

thank you

Thandius
 
Posts: 11
Joined: Tue Jun 16, 2020 2:56 pm

Thu Jul 08, 2021 7:46 am

Hello,

Glad to hear that. If you need assistance when using our products in the future, please feel free to contact us.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF