Some PDF file watermarks are not displayed using iText

Our company uses iText to print some watermark text (rather than an image) on some pdf formats. I noticed that 95% of the forms show the watermark correctly, about 5% do not. I tested, copied 2 original pdf files, one was marked normally, the other was not in order, then tested through a small program, the same result: one got a mark, the other did not. Then I tried the latest version of the iText jar file (version 5.0.6), the same thing. I checked the properties of the PDF file, security settings, etc., It seems that nothing is displayed. The result file was resized and markd "changed to iText version ...." after the program execution.

Here is an example of a watermark code (using its version version 2.1.7), notes topText, mainText, bottonText, make 3 lines of watermarks in PDF format as a watermark.

Any help is appreciated !!

public class WatermarkGenerator {

    private static int TEXT_TILT_ANGLE = 25;
    private static Color MEDIUM_GRAY = new Color(160, 160, 160);
    private static int SUPPORT_FONT_SIZE = 42;
    private static int PRIMARY_FONT_SIZE = 54;

    public static void addWaterMark(InputStream pdfInputStream,
        OutputStream outputStream, String topText, 
        String mainText, String bottomText) throws Exception {
        PdfReader reader = new PdfReader(pdfInputStream);
        int numPages = reader.getNumberOfPages();

        // Create a stamper that will copy the document to the output
        // stream.
        PdfStamper stamp = new PdfStamper(reader, outputStream);
        int page=1;

        BaseFont baseFont = 
            BaseFont.createFont(BaseFont.HELVETICA_BOLDOBLIQUE,
                BaseFont.WINANSI, BaseFont.EMBEDDED);

        float width;
        float height;

        while (page <= numPages) {
            PdfContentByte cb = stamp.getOverContent(page);
            height = reader.getPageSizeWithRotation(page).getHeight() / 2;
            width = reader.getPageSizeWithRotation(page).getWidth() / 2;

            cb = stamp.getUnderContent(page);
            cb.saveState();
            cb.setColorFill(MEDIUM_GRAY);

            // Top Text
            cb.beginText();
            cb.setFontAndSize(baseFont, SUPPORT_FONT_SIZE);
            cb.showTextAligned(Element.ALIGN_CENTER, topText, width,
                    height+PRIMARY_FONT_SIZE+16, TEXT_TILT_ANGLE);
            cb.endText();

            // Primary Text
            cb.beginText();
            cb.setFontAndSize(baseFont, PRIMARY_FONT_SIZE);
            cb.showTextAligned(Element.ALIGN_CENTER, mainText, width,
                    height, TEXT_TILT_ANGLE);
            cb.endText();

            // Bottom Text
            cb.beginText();
            cb.setFontAndSize(baseFont, SUPPORT_FONT_SIZE);
            cb.showTextAligned(Element.ALIGN_CENTER, bottomText, width,
                    height-PRIMARY_FONT_SIZE-6, TEXT_TILT_ANGLE);
            cb.endText();
            cb.restoreState();

            page++;
        }

        stamp.close();
    }
}
+3
source share
2 answers

We solved the problem by changing the file option Adobe LifecycleSave. File-> Save-> Properties-> Save As, and then look at the Save As type, the default is Acrobat 7.0.5 Dynamic PDF Form File, we changed it to use 7.0.5 Static PDF Form File (in fact, any static will work). A file saved in static does not contain a watermark problem. Thanks to Mark for pointing in the right direction.

+1
source

underContent, overContent. . , , . PostScript .


, PDF, , , XFA ( LiveCycle Designer). Acrobat ( ) XFA ( xml), . , . Acrobat PDF XFA, PDF , .

- XFA, PDF .

:

PdfReader reader = new PdfReader(...);
AcroFields acFields = reader.getAcroFields();
XfaForm xfaForm = acFields.getXfaForm();
if (xfaForm != null && xfaForm.isXfaPresent()) {
  // Ohs nose.
  throw new ItsATrapException("We can't repel XML of that magnitude!");
}

, , , .

, , XML... " " .

.

+1

All Articles