0

The SRC_PDF I'm using was previously filled by a previous method using the following code:

    XfaForm xfa = form.getXfa(); 
    xfa.fillXfaForm(new FileInputStream(XML));
    stamper.close();
    reader.close();

Now I'd like to create an image overlay on the 2nd page of the filled document. By using the PDFStamper (even with the use of append), I now lose all my filled fields but do get the image overlay on page 2. Has someone figured out how to do an image overlay on a prepopulated XFA PDF form?

    package example.pdf;

    import java.io.FileOutputStream;
    import org.junit.Test;

    import com.itextpdf.text.Image;
    import com.itextpdf.text.pdf.PdfContentByte;
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.PdfStamper;

    public class SignaturePDFTest {
        public static final String SRC_PDF = "target/XFA_Form_filled.pdf";
        public static final String DEST_PDF = "target/XFA_Form_withImageOverlay.pdf";
        public static final String OVERLAY_GRAPHIC = "src/test/imageOverlay.png";


    @Test
    public void testPdfStamp() throws Exception
    {
        PdfReader pdfReader = new PdfReader(SRC_PDF);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(DEST_PDF), '\0', true);
        Image image = Image.getInstance(OVERLAY_GRAPHIC);
        image.scaleToFit(200, 170);

        for (int i=1; i<=pdfReader.getNumberOfPages(); i++)
        {
            PdfContentByte content = pdfStamper.getOverContent(i);
            image.setAbsolutePosition(450f, 450f);
            if (i==2) content.addImage(image);
        }   
        pdfStamper.close();
        pdfReader.close();
    }
}

After I run the pdfStamper overlay I lose the filled fields from the previous calls: xfa.fillXfaForm(new FileInputStream(XML)); Is there a specific way to place an image on a specific page when the PDF form has XFA fields?

Mouser
  • 13,132
  • 3
  • 28
  • 54
  • 1
    Usually you have to decide whether you use XFA technology (using PDF as a wrapper only) or actual PDF technology. Only specially crafted XFA found truly interact with PDF contents. – mkl Jan 03 '15 at 08:39
  • What @mkl says is correct. Your first code snippet assumes that the PDF document doesn't really consist of PDF syntax, but that the PDF is a container for XML. Your second snippet however depends entirely on the fact that the PDF consists of PDF syntax. You are mixing technologies you shouldn't mix. If you want the XFA form to be ready to 'accept' an image, you should construct your XFA form that way. – Bruno Lowagie Jan 03 '15 at 09:59

0 Answers0