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.

Mon Aug 03, 2020 5:52 pm

Hi,

I'd like to add multiple Images to an existing page in a PDF document.
When just using DrawImage, existing text is not visible anymore, because images are drawn on top of canvas.
Is there a way to insert an image in a way, that already existing content is rendered over that image?
I know the BackgroundImage property, would that be a way to achieve that?

If so:
  • Is BackgroundImage always semi transparent?
  • Can I add more than one image as BackgroundImage?

jseibert
 
Posts: 11
Joined: Wed Jul 29, 2020 7:36 am

Tue Aug 04, 2020 6:27 am

Hello,

Thanks for your inquiry.
When using the DrawImage method, you can set the image transparency to achieve your needs, as shown below.
Code: Select all
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(@"XXX.pdf");
            PdfPageBase page = pdf.Pages[0];

            PdfImage image1 = PdfImage.FromFile(@"XXX.png");
            PdfImage image2 = PdfImage.FromFile(@"XXX.png");


            //Add one section to the PDF document
            PdfSection section = pdf.Sections.Add();

            //Set the Image size in PDF file
            float imageWidth = image1.PhysicalDimension.Width / 2;
            float imageHeight = image1.PhysicalDimension.Height / 2;

           
            //write and save the image loaded into PDF
            page.Canvas.SetTransparency(0.3f);
            page.Canvas.DrawImage(image1, 0, 0, imageWidth, imageHeight);
            page.Canvas.DrawImage(image2, 0, page.Size.Height / 2, imageWidth, imageHeight);
            page.Canvas.Save();


            //Save pdf file.
            pdf.SaveToFile("Transparency9.pdf");



As for using BackgroundImage method, you can set the transparency of the background image according to your needs, as shown below. And it is not possible to add multiple images as background images on the same page. Please feel free to contact us if you have any further questions.

Code: Select all
                PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.Size.Width / 3, page.Canvas.Size.Height / 5));
                //!!! Set the transparency
                brush.Graphics.SetTransparency(0.3f);
                //Draw image on brush graphics
                brush.Graphics.DrawImage(backgroundImage, new PointF((brush.Size.Width - image.Width) / 2, (brush.Size.Height - image.Height) / 2));
                //use the brush to draw rectangle
                page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));


Sincerely
Elena
E-iceblue support team
Last edited by Elena.Zhang on Tue Aug 04, 2020 7:58 am, edited 1 time in total.
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Tue Aug 04, 2020 6:37 am

Thank you for your answer, but transparency does not help here in all situations.
Image should be fully visible (opaque) and already existing content should be fully visible.
If for instance image is drawn in full page size (using DrawImage), anything being already on page will be hidden by that image.

Being able to set transparency for BackgroundImage is a way to overcome that situation (thank you for the hint), but will end in an other issue, when I need to add more than one image.

jseibert
 
Posts: 11
Joined: Wed Jul 29, 2020 7:36 am

Tue Aug 04, 2020 9:17 am

Hi,

Thank you for your prompt reply.
Please refer to the following code. If this is not what you want, please provide your PDF file, your images as well as the output you expect. You can upload them here or send them to us (support@e-iceblue.com) via email.

Code: Select all
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(@"XXX.pdf");
            PdfPageBase page = pdf.Pages[0];

            PdfDocument newPdf = new PdfDocument();
            PdfPageBase newPage = newPdf.Pages.Add(page.Size,new PdfMargins(0));

            PdfImage image1 = PdfImage.FromFile(@"XXX.png");
            PdfImage image2 = PdfImage.FromFile(@"XXX.png");

            //write and save the image loaded into PDF

            newPage.Canvas.DrawImage(image1, 0, 0);
            newPage.Canvas.DrawImage(image2, 40, 100);

            newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0), page.Size);


            newPdf.SaveToFile("result.pdf");


Sincerely
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Tue Aug 04, 2020 3:13 pm

I am asking to add an image to an existing pdf, but render it below all other contents, but not as BackgroundImage.
source_pdf.png

will render as
output_pdf.png


Code: Select all
var doc = new PdfDocument();
      var basePath = TestContext.CurrentContext.TestDirectory;
      var pdfFile = Path.Combine(basePath, "test.pdf");
      doc.LoadFromFile(pdfFile);

      var page = doc.Pages[0];

      var img = PdfImage.FromFile(Path.Combine(basePath, "interior_2_4.jpg"));
      var template = new PdfTemplate(new SizeF(1495, 971));
      template.Graphics.DrawImage(img, -0f, -20f, img.Width, img.Height);
      page.Canvas.DrawTemplate(template, new PointF(0f, 0f), new SizeF(782f, 508f));

      var path = Path.Combine(@"c:\temp\", Path.GetRandomFileName());
      doc.SaveToFile($"{path}.pdf");


this will not work, but that is to be expected. Question still is, how can it be achieved.

jseibert
 
Posts: 11
Joined: Wed Jul 29, 2020 7:36 am

Tue Aug 04, 2020 4:18 pm

well, did not read carefully, so you've created a new document and copied pages over.
Can I achieve it when I just set page at index to newly created page?
I did test something similar, with
Code: Select all
      var page = doc.Pages[5];
      var pageSettings = doc.PageSettings;
      var margin = pageSettings.Margins;      var img = PdfImage.FromFile(Path.Combine(basePath, "interior_2_4.jpg"));
      var template = new PdfTemplate(new SizeF(1495, 971));
      template.Graphics.DrawImage(img, -0f, -0f, img.Width, img.Height);
      var origSize = page.Size;
      var newPage = doc.Pages.Insert(5, origSize);
      newPage.Canvas.DrawTemplate(template, new PointF(30f - margin.Left, 30f - margin.Top), new SizeF(782f, 535f));
      var origTemplate = page.CreateTemplate();
      newPage.Canvas.DrawTemplate(origTemplate,new PointF(0f - margin.Left, 0f - margin.Top));
      doc.Pages.Remove(page);

jseibert
 
Posts: 11
Joined: Wed Jul 29, 2020 7:36 am

Tue Aug 04, 2020 7:13 pm

did a mix with your hints and my code and it solved this issue.

thank you for your support

jseibert
 
Posts: 11
Joined: Wed Jul 29, 2020 7:36 am

Wed Aug 05, 2020 1:57 am

Hello,

Glad to hear that you solved this issue!
If you encounter any issues related our product in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.PDF