如何使用itext pdf在PDF文件中添加水印

时间:2017-01-30 12:33:38

标签: java itext

请指导我如何做到这一点。我尝试了相同但不能使用itext pdf jar。

1 个答案:

答案 0 :(得分:-2)

请尝试使用以下详细信息

class pdfWatermark {
    public static void main(String args[]) {
        try {
            PdfReader reader = new PdfReader("E://Sample.pdf");
            int n = reader.getNumberOfPages();

            // Create a stamper that will copy the document to a new file
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("text1.pdf"));
            int i = 1;
            PdfContentByte under;
            PdfContentByte over;

            Image img = Image.getInstance("E://Capture.PNG");
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);

            img.setAbsolutePosition(200, 400);

            while (i < n) {
                // Watermark under the existing page
                under = stamp.getUnderContent(i);
                under.addImage(img);

                // Text over the existing page
                over = stamp.getOverContent(i);
                over.beginText();
                over.setFontAndSize(bf, 18);
                over.showText("page " + i);
                over.endText();

                i++;
            }

            stamp.close();

        } catch (Exception de) {
        }
    }
}