Can I watermark XFA PDF with iText (commercial or not)?

Ok, after spending quite a lot of time on this, I think I may have found a solution shortly after I posted the question. A little awkward, but hey, it looks like now.

I don't want to delete the question yet if anyone wants details. Basically, I tried to specify point (c) again, but in the XML I returned, I used an inline image. So it seems like I can change the master page of an XFA template using iText. More tests to follow ...

=========================

Problem : Apply watermark to PDF XFA (dynamic or not). At the end, the PDF should still be XFA and should have all the security settings. I have no control over incoming PDFs.

Question . Can I use iText 5 for this? If so, is it through PdfStamper.getUnderContent () or through XfaForm.setXfa () or something else? The first 2 didn't work for me.

I wonder if there is any XFA detail missing that I am missing (i.e. when I try to replace XML), not sure how XFA is actually tamper proof. Do I need to generate some UUIDs, encrypt some things, something about signatures ...

Btw if I take PDF generated by iText after XML replacement then open and save in LC the watermark shows

thanks, cristian

========================

Anyway, this is a short version of my question. If you think you can help and / or are interested in more details ...

I know this or similar questions have come up before, but what I've tried so far hasn't worked for me. I also admit that I am not an IText, XFA or PDF Standard specialist. I've looked at a few forum posts, the Itext book, looked at the specs, no luck so far.

PDF input has no regular usage rights, no security. When it opens in Acrobat it will show restrictions on modifying documents / assembling documents / adding templates, but I feel like all XFAs have that. Nothing comes back from the following ...

    System.out.println(" permissions " + reader.getPermissions());
    System.out.println(" usage rights " + reader.hasUsageRights());
    System.out.println(" viewer pref" + reader.getSimpleViewerPreferences());

      

Here are some of my things:

a) makes LC in it ( https://forums.adobe.com/thread/496558 )

It works. If I try to place an image on the main page, it will show as a watermark on all pages when saved with LC.

b) trying to write while using iText ( https://sourceforge.net/p/itext/mailman/message/17225398/ )

I found a post from '07 suggesting using pdfStamper.getUnderContent (). The stream doesn't seem to end if it actually fires for the person setting

For me the code works for plain PDF but not for XFA

PdfContentByte under = pdfStamper.getUnderContent(1);
under.beginText();
BaseFont FONT = BaseFont.createFont("c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
under.setFontAndSize(FONT, 40);
under.showTextAligned(Element.ALIGN_CENTER, "TEST_TEXT", 200, 600, 45);
under.endText();

      

c) generate XML and replace it with iText ( Some PDF watermarks are not displayed with iText )

After reading the post above and section 8.6 of itext in action , it seemed like the right way to go, so:

  • I am creating a simple XFA using LiveCycle
  • Save as xfa1.pdf and extract xml (using iText) to xfa1.xml
  • in LC add image to main page and save as xfa2.pdf
  • open xfa2.pdf and notice the watermark is present
  • extract XML from xfa2 to xfa2.xml and compare to xfa1.xml - notice the image element
  • either put the image element in the original XML or use xfa2.xml and replace the XFA in the original xfa1.pdf with iText, call it xfa3_itext;

Relevant code:

XfaForm xfa = new XfaForm(reader);
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
fact.setNamespaceAware(true);
DocumentBuilder db = fact.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(xml));
xfa.setDomDocument(doc);
xfa.setChanged(true);
XfaForm.setXfa(xfa, stamper.getReader(), stamper.getWriter());

      

I tried with a sample PDF that was provided by the client and no luck with a simple XFA form created in LiveCycle Designer. If I open xfa3_itext in LiveCycle it watermarks and if I save it again from LiveCycle as dynamic XFA PDF the new PDF displays the watermark.

phew, this is a long post ... sorry.

thanks for reading and for any feedback

+3


source to share





All Articles