Search in sources :

Example 1 with PdfExtGState

use of com.itextpdf.kernel.pdf.extgstate.PdfExtGState in project java-example by saxingz.

the class A0025PdfApplication method addText2.

private static void addText2(PdfCanvas canvas, Rectangle pageSize, int totalPage, int i, Document document, PdfDocument pdfDoc, PdfPage page) throws IOException {
    // Draw header text
    canvas.beginText().setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA), 7).moveText(pageSize.getWidth() / 2 - 24, pageSize.getHeight() - 10).showText(randomStr() + WECHAT_STR + randomStr()).endText();
    // Draw footer line
    canvas.setStrokeColor(Color.BLACK).setLineWidth(.2f).moveTo(pageSize.getWidth() / 2 - 30, 20).lineTo(pageSize.getWidth() / 2 + 30, 20).stroke();
    // Draw page number
    canvas.beginText().setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA), 7).moveText(pageSize.getWidth() / 2 - 7, 10).showText(randomStr() + WECHAT_STR + randomStr()).endText();
    int waterTimes = randomInt(WATER_TIMES_RANGE);
    System.out.println("本页水印: " + waterTimes + "层");
    for (int j = 0; j < waterTimes; j++) {
        // Draw watermark
        Paragraph p = new Paragraph(WECHAT_STR).setFontSize(randomInt(WATER_FONT_SIZE_RANGE));
        PdfExtGState gs1 = new PdfExtGState().setFillOpacity(WATER_OPACITY);
        canvas.setExtGState(gs1);
        document.showTextAligned(p, pageSize.getWidth() / WATER_POSITION_RANGE * randomInt(WATER_POSITION_RANGE), pageSize.getHeight() / WATER_POSITION_RANGE * randomInt(WATER_POSITION_RANGE), pdfDoc.getPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, randomInt(360));
        // add random char
        Paragraph p2 = new Paragraph(getRandomWechat(WECHAT_STR)).setFontSize(randomInt(WATER_FONT_SIZE_RANGE));
        PdfExtGState gs2 = new PdfExtGState().setFillOpacity(WATER_OPACITY);
        canvas.setExtGState(gs2);
        document.showTextAligned(p2, pageSize.getWidth() / WATER_POSITION_RANGE * randomInt(WATER_POSITION_RANGE), pageSize.getHeight() / WATER_POSITION_RANGE * randomInt(WATER_POSITION_RANGE), pdfDoc.getPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, randomInt(360));
        // transparent
        Paragraph p3 = new Paragraph(WECHAT_STR).setFontSize(randomInt(WATER_FONT_SIZE_RANGE));
        PdfExtGState gs3 = new PdfExtGState().setFillOpacity(0);
        canvas.setExtGState(gs3);
        document.showTextAligned(p3, pageSize.getWidth() / WATER_POSITION_RANGE * randomInt(WATER_POSITION_RANGE), pageSize.getHeight() / WATER_POSITION_RANGE * randomInt(WATER_POSITION_RANGE), pdfDoc.getPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, randomInt(360));
    }
    canvas.saveState();
    canvas.restoreState();
}
Also used : PdfExtGState(com.itextpdf.kernel.pdf.extgstate.PdfExtGState) Paragraph(com.itextpdf.layout.element.Paragraph)

Example 2 with PdfExtGState

use of com.itextpdf.kernel.pdf.extgstate.PdfExtGState in project java-example by saxingz.

the class A0025PdfApplication method addText1.

private static void addText1(PdfCanvas canvas, Rectangle pageSize, int totalPage, int i, Document document, PdfDocument pdfDoc, PdfPage page) throws IOException {
    // Draw header text
    canvas.beginText().setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA), 7).moveText(pageSize.getWidth() / 2 - 24, pageSize.getHeight() - 10).showText("I want to believe").endText();
    // Draw footer line
    canvas.setStrokeColor(Color.BLACK).setLineWidth(.2f).moveTo(pageSize.getWidth() / 2 - 30, 20).lineTo(pageSize.getWidth() / 2 + 30, 20).stroke();
    // Draw page number
    canvas.beginText().setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA), 7).moveText(pageSize.getWidth() / 2 - 7, 10).showText(String.valueOf(i)).showText(" of ").showText(String.valueOf(totalPage)).endText();
    // Draw watermark
    Paragraph p = new Paragraph("wechat-diyicaizi").setFontSize(60);
    canvas.saveState();
    PdfExtGState gs1 = new PdfExtGState().setFillOpacity(0.2f);
    canvas.setExtGState(gs1);
    document.showTextAligned(p, pageSize.getWidth() / 2, pageSize.getHeight() / 2, pdfDoc.getPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
    canvas.restoreState();
}
Also used : PdfExtGState(com.itextpdf.kernel.pdf.extgstate.PdfExtGState) Paragraph(com.itextpdf.layout.element.Paragraph)

Aggregations

PdfExtGState (com.itextpdf.kernel.pdf.extgstate.PdfExtGState)2 Paragraph (com.itextpdf.layout.element.Paragraph)2