Home > Back-end >  iText7 Add image to back
iText7 Add image to back

Time:04-01

Is there a way to add this image to back and not over lay text that is already on the page? or do I need to use the Rectangle that is cutting into the text?

 float mapimageX = 111.318f;
 float mapimageY = 130.791f;
 float mapimageWidth = 755.454f;
 float mapimageHeight = 432.094f;
 ImageData imageData = ImageDataFactory.Create(@"PathTOFile");
 imagerectangle = new iText.Kernel.Geom.Rectangle(mapimageX, mapimageY, mapimageWidth, mapimageHeight);
 canvasPage.AddImageFittedIntoRectangle(imageData, imagerectangle,true);

Example

enter image description here

CodePudding user response:

As you mention in a comment, you create the PdfCanvas like this:

PdfCanvas canvasPage = new PdfCanvas(pdfPage);

This essentially is equivalent to

PdfCanvas canvasPage = new PdfCanvas(pdfPage.NewContentStreamAfter(), pdfPage.GetResources(), pdfPage.GetDocument());

plus some optimizations, i.e. the new content you draw is added after the existing page content. Thus, the new content covers existing page content in the same area.

If you want the existing page content to cover your new content where applicable, you have to add the new content before the existing page content. You can do that like this:

PdfCanvas canvasPage = new PdfCanvas(pdfPage.NewContentStreamBefore(), pdfPage.GetResources(), pdfPage.GetDocument());

Beware, though: Some PDF creators first cover the whole page area with a white rectangle before drawing stuff on it. If you try to manipulate such a PDF, anything you create in the back will be covered by said white rectangle and, therefore, will be invisible. For such cases adding in front using special blend modes may be better.

  • Related