Watermark Capture Example

Table of Contents

About the Watermark Capture Example

The Watermark Capture example demonstrates how to use the ICEpdf Document class to capture PDF page renders as image files using the watermark callback API. Once a file has been opened, the document pages can be iterated over, to generate an image capture of each page, if a watermark callback has been set it will be called after each page is painted.

The source-code for this example is located at:

A primer on using Maven or Gradle build commands can be found here (Maven) and here (Gradle)

// open the document
document.setFile(filePath);

// attache a watermark
document.setWatermarkCallback(new MyWatermarkCallback());

// create a list of callables.
int pages = document.getNumberOfPages();
java.util.List<Callable<Void>> callables = new ArrayList<Callable<Void>>(pages);
for (int i = 0; i <= pages; i++) {
    callables.add(new CapturePage(document, i));
}
executorService.invokeAll(callables);
executorService.submit(new DocumentCloser(document)).get();

The watermark paint code will paint a "This is Page X" each page where X is the current page number.

public class MyWatermarkCallback implements WatermarkCallback {
    // to avoid memory leaks be careful not to save an instance of page in
    // your implementation
    public void paintWatermark(Graphics g, Page page, int renderHintType,
                               int boundary, float userRotation, float userZoom) {
        Graphics2D g2 = (Graphics2D) g;

        // setup the graphics context and a 45 degree rotation effect.
        Rectangle2D.Float mediaBox = page.getPageBoundary(boundary);
        AffineTransform af = new AffineTransform();
        af.scale(1, -1);
        af.rotate(-45.0 * Math.PI / 180.0, mediaBox.getWidth() / 2.0, -mediaBox
                .getHeight() / 2.0);
        g2.transform(af);

        // apply transparency
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f));
        // ICEpdf red.
        g2.setColor(new Color(186, 0, 0));
        // draw Some text.
        String footerText = "This Page " + (page.getPageIndex() + 1);
        g2.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, 36));
        FontMetrics fontMetrics = g2.getFontMetrics();
        Rectangle2D fontBounds = fontMetrics.getStringBounds(footerText.toCharArray(),
                0, footerText.length(),
                g2);

        int x = (int) (mediaBox.x + (mediaBox.width - fontBounds.getWidth()) / 2.0);
        int y = -(int) (mediaBox.y - (mediaBox.height - fontBounds.getHeight()) / 2.0);
        g2.drawString(footerText, x, y);
    }
}
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

© Copyright 2017 ICEsoft Technologies Canada Corp.