Using iTextSharp PdfStamper to Overlay an Image on an Existing PDF File

I can overlay an image on an existing PDF using the PDFStamper method and PdfContentByte content.AddImage.

My problem arises when an image is already overlaid on an existing document. In fact, you can see the top edge of the small image I'm trying to overlay. It is clearly hidden under the existing image overlay.

I'm having trouble trying to get an overlay image on top of an existing image overlay.

My code is:

System.Drawing.Image bitmap PdfReader pdfReader = new PdfReader(pathToOriginalPdf); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(pathToTimestampedPdf, FileMode.Create, FileAccess.Write, FileShare.None)); MemoryStream imageStream = new MemoryStream(); bitmap.Save(imageStream, ImageFormat.Bmp); byte[] bitmapBytes = imageStream.ToArray(); iTextSharp.text.Image image = Image.GetInstance(bitmapBytes); PdfContentByte underContent; try { underContent = pdfStamper.GetOverContent(1); underContent.AddImage(image); } 

I need a way to either smooth the existing overlay of images onto the contents of the PDF, or set the z-order so that my newly added overlay can sit on top.

For some reason, PdfStamper selects a new image below the existing one.

Thanks in advance.

+4
source share
1 answer

This will help if we see the PDF file. Then we did not need to guess, we would know.

However, I suspect that your “existing image overlay” is part of the annotation. Nothing you put in the content of the page will ever appear above the annotation.

Options (if I'm right):

Add your own annotation

For this, I would use a PushbuttonField with LAYOUT_ICON_ONLY. Draw an image in PdfTemplate and use it for the icon button.

The z-order for annotations is determined by the order of the page annotation array. New annotations are added to this array. No problems.

 PushbuttonField fld = new PushbuttonField(stamper.getWriter(), box, name); fld.setLayout(PushbuttonField.LAYOUT_ICON_ONLY); fld.setImage(myImage); stamper.addAnnotation(fld.getField(), 1); 

You may need to use setScaleIcon (), setHorizontalAdjustment (), setVerticalAdjustment (), setProportionalIcon () and maybe a couple others to make your image look exactly the way you want it to.

Flatten in one pass, add your image to another

If the existing image annotation is something that iText can smooth out (maybe not), you can do what you want in two passes. The first pass will be just "setFormFlattening (true); close ();" and the second will be all that you are doing now.

 ByteArrayOutputStream output = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper( firstReader, output ); stamper.setFormFlattening(true); stamper.setFreeTextFlatten(true); // probably not needed. stamper.close(); PdfReader secondReader = new PdfReader(output.toByteArray()); FileOutputStream finalOutput = new FileOutputStream( outputPath ); stamper = new PdfStamper(secondReader, finalOutput); // do your thing here. stamper.getOverContent(1).addImage(image); 
+2
source

Source: https://habr.com/ru/post/1338930/


All Articles