Saxon
Saxon

Reputation: 33

how to add watermark to pdf(itext7.1.2 java)

It's the code I followed one sample.

the code i was modified a little.

public class MyEventHandler implements IEventHandler {
@Override
public void handleEvent(Event event) {
    // TODO Auto-generated method stub
    PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
    PdfDocument pdfDoc = docEvent.getDocument();
    PdfPage page = docEvent.getPage();
    PdfCanvas pdfCanvas = new PdfCanvas(
        page.newContentStreamBefore(), page.getResources(), pdfDoc);

    DeviceRgb colorWatermark = new DeviceRgb(220,36,31);
    //Add watermark
    Canvas canvas = new Canvas(pdfCanvas, pdfDoc, page.getPageSize());
    canvas.setProperty(Property.FONT_COLOR, colorWatermark);
    canvas.setProperty(Property.FONT_SIZE, 60);
    try {
        canvas.setProperty(Property.FONT, PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H",true));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    canvas.showTextAligned(new Paragraph("CONFIDENTIAL"),
        298, 421, pdfDoc.getPageNumber(page),
        TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
    pdfCanvas.release();
  }
}

when the code goto the line:canvas.showTextAligned()

It throws out an exception:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to com.itextpdf.layout.property.UnitValue
at com.itextpdf.layout.renderer.AbstractRenderer.getPropertyAsUnitValue(AbstractRenderer.java:405)
at com.itextpdf.layout.renderer.TextRenderer.layout(TextRenderer.java:196)
at com.itextpdf.layout.renderer.LineRenderer.layout(LineRenderer.java:327)
at com.itextpdf.layout.renderer.ParagraphRenderer.layout(ParagraphRenderer.java:205)
at com.itextpdf.layout.renderer.BlockRenderer.layout(BlockRenderer.java:219)
at com.itextpdf.layout.renderer.RootRenderer.addChild(RootRenderer.java:287)
at com.itextpdf.layout.renderer.CanvasRenderer.addChild(CanvasRenderer.java:89)
at com.itextpdf.layout.RootElement.createAndAddRendererSubTree(RootElement.java:377)
at com.itextpdf.layout.RootElement.add(RootElement.java:106)
at com.itextpdf.layout.RootElement.showTextAligned(RootElement.java:364)
at com.schindler.zhangzhiwei.quote.action.MyEventHandler.handleEvent(MyEventHandler.java:56)

what happened? what is UnitValue? how to change it?

this is the main code:

try {

        writer = new PdfWriter(new File(openSaveDialog()));
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    pdf = new PdfDocument(writer);
    pdf.addEventHandler(PdfDocumentEvent.END_PAGE, new MyEventHandler());

Upvotes: 0

Views: 363

Answers (2)

Saxon
Saxon

Reputation: 33

I have solve the problem finally.i give up the method of canvas. then, i transmitted the document parameter to the MyEventHandler. Use the following sentence:document.add(new Paragraph("Hello World").setFixedPosition(298, 421, 100).setRotationAngle(45).setFontColor(colorWatermark).setFontSize(100));

Upvotes: 0

GhostCat
GhostCat

Reputation: 140447

Here:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to com.itextpdf.layout.property.UnitValue

This means that the framework you are using assumes that a certain value should be of class UnitValue, but turns out to be an Integer.

A bit of guessing here: you have code like

canvas.setProperty(Property.FONT_SIZE, 60);

The above will end up putting an Integer object into some "property map". Most likely, you are not using that API correctly.

Meaning: the answer boils down to you studying the API documents for the product you are using. You have to understand how to correctly setup these property parameters.

From looking at the javadoc for UnitValue, it looks like this class is used to somehow represent positions. So I would start by looking at properties that deal with "positioning".

Upvotes: 1

Related Questions