pdf - How to make PdfLayer.SetPrint work with PdfStamper? -


I'm adding a watermark to an existing PDF using the iText PdfStamper class. And I want these watermarks to be turned on or off, so I'm using class PdfLayer .
But I also want to see these watermarks when printed in these files: Then I am using the function PdfLayer.setPrint () . This is the last step which unfortunately does not work as expected.

Here my code is:

  PdfReader reader = new PdfReader ("C: /Temp/input.pdf"); Pdfstammer stamp = new PDFFormer (reader, new file output stream ("C: / temp / output.pdf")); PdfWriter Author = stamp.getWriter (); PDFlayer layer = new PDFLayer ("watermark", author); Layer.setOn (true); Layer.setPrint ("watermark", true); BaseFont BF = BaseFont.CorteFont (); PdfContentByte cb = stamp.getOverContent (1); Cb.beginText (); Cb.setFontAndSize (BF, 18); Cb.beginLayer (Layer); Cb.showTextAligned (element.lIGN_LEFT, "watermark line 1", 50, 55, 0); Cb.showTextAligned (element.lIGN_LEFT, "watermark line2", 50, 40, 0); Cb.endLayer (); Cb.endText (); Stamp.close (); Reader.close ();  

When I check the layer properties from Adobe Reader (version 10), I suppose that "Initial State: Print" property resides on "Prints In Visible", while it "Always print".

I also tried to create layers on a new PDF document and there setPrint () works.

What am I doing?

I have the same problem as an alternative solution, Instead of using new document and getImportedPage you can use pdfStamper . Unfortunately, you lose the hyperlink because all pages convert to images I tried to use the PdfCopy but I re-submitted the same issue. I'm really interested in a solution that allows me to add a watermark without changing the source file Degraded Sample Solution :

  PDF downloader pdfReaderS = new PDF reader (file path); document document = new document (pdfReaderS.getPageSizeWithRotation (1)); PdfWriter pdfWriterD = PdfWriter.getInstance (document, new FileOutputStream (filepathD)); Document.open (); PdfContentByte pdfContentByteD = pdfWriterD.getDirectContent (); BaseFont baseFont = BaseFonts.Contefont (BaseFont.Helveiteca, BaseFont.INNSI, BaseFont.Embad); Int n = pdfReaderS.getNumberOfPages (); PDFfare PDFLayer = new PDFLayer ("watermark", pdf redirect); PdfLayer.setPrint ("print" is true); PdfLayer.setView (visibleScreen); For (int i = 1; i & lt; = n; i ++) {rectangles pages = PDF reader szet pageviewing (i); Float PageWidth = PagesyS.getWidth () / 2; Float PageHift = PageSizeSGhatHit () / 2; Float degree = (float) (Math. Todegrijze (Math. Totten (Parsize Gate Hight) / Pagesy S.getWidth ()))); Document.setPageSize (pageSizeS); Document.newPage (); PDF Imported Page PDF imported page = pdfWriterD.getImportedPage (pdfReaderS, i); Int Rotation = pdfReaderS.getPageRotation (i); // This value can be 0, 90, 180 or 270. If (rotation == 0) pdfContentByteD.addTemplate (pages, 1, 0, 0, 1, 0, 0); And if (rotation == 90) pdfContentByteD.addTemplate (pages, 0, -1, 1, 0, 0, pages. Gatehit ()); Otherwise if (rotation == 180) pdfContentByteD.addTemplate (pages, -1, 0, 0, -1, parsize.gatehit (), PageSecs .getWidth ()); Else if (rotation == 270) pdfContentByteD.addTemplate (pages, 0, 1, -1, 0, pages .getWidth (), 0); PdfContentByteD.beginLayer (pdfLayer); PdfContentByteD.beginText (); PdfContentByteD.setFontAndSize (BaseFont, Police Size); PdfContentByteD.setColorFill (col); PdfContentByteD.showTextAligned (PdfContentByte.ALIGN_CENTER, Text, PageWidth, PageHit, Degree); PdfContentByteD.endText (); PdfContentByteD.endLayer (); } Document.close (); PdfReaderS.close ();  

Comments