Page Loading Events

Table of Contents

About the Page Loading Events Example

The Page Loading Events example demonstrates how to use the ICEpdf Document's Page class to register a PageLoadingListener. In this example the listener implementation will collects page loading metrics and print a summary when each page loading ended event is received.

For more information on the PageLoadingListener specifics please consult the JavaDoc API.

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)

// save page caputres to file.
float scale = 1.0f;
float rotation = 0f;

// Paint each pages content to an image and write the image to file
for (int i = 0; i < document.getNumberOfPages(); i++) {
    BufferedImage image = (BufferedImage)
            document.getPageImage(i,
                                  GraphicsRenderingHints.SCREEN,
                                  Page.BOUNDARY_CROPBOX, rotation, scale);
    RenderedImage rendImage = image;
    // capture the page image to file
    try {
        System.out.println("\t capturing page " + i);
        File file = new File("imageCapture1_" + i + ".png");
        ImageIO.write(rendImage, "png", file);

    } catch (IOException e) {
        e.printStackTrace();
    }
    image.flush();
}

The MetricsPageLoadingListener implementation is as follows.

public class MetricsPageLoadingListener implements PageLoadingListener {

    private static final Logger logger =
            Logger.getLogger(MetricsPageLoadingListener.class.toString());

    public static final DecimalFormat formatter = new DecimalFormat("#.###");
    public static final DecimalFormat percentFormatter = new DecimalFormat("#");
    private int pageIndex;
    private int pageCount;

    private long startLoading;
    private long endLoading;

    private long startInit;
    private long endInit;

    private long imageCount;
    private long imageLoadDuration;

    private long startPaint;
    private long endPaint;
    private long paintCount;

    public MetricsPageLoadingListener(int pageCount) {
        this.pageCount = pageCount;
    }

    public void pageLoadingStarted(PageLoadingEvent event) {
        startLoading = System.nanoTime();
        pageIndex = ((Page) event.getSource()).getPageIndex();
        imageCount = event.getImageResourceCount();
    }

    public void pageInitializationStarted(PageInitializingEvent event) {
        startInit = System.nanoTime();
    }

    public void pageInitializationEnded(PageInitializingEvent event) {
        endInit = System.nanoTime();
    }

    public void pageImageLoaded(PageImageEvent event) {
        imageLoadDuration += event.getDuration();
    }

    public void pagePaintingStarted(PagePaintingEvent event) {
        startPaint = System.nanoTime();
        paintCount = event.getShapesCount();
    }

    public void pagePaintingEnded(PagePaintingEvent event) {
        endPaint = System.nanoTime();
    }

    public void pageLoadingEnded(PageLoadingEvent event) {
        endLoading = System.nanoTime();
        displayConsoleMetrics();
    }

    private void displayConsoleMetrics() {
        System.out.println("Loading page: " + (pageIndex + 1) + "/" + pageCount);
        double totalTime = convert(endLoading - startLoading);
        double initTime = convert(endInit - startInit);
        double paintTime = convert(endPaint - startPaint);
        double imageTime = convert(imageLoadDuration);
        System.out.println("        init time: " + formatter.format(initTime) +
                "ms (" + percentFormatter.format((initTime / totalTime) * 100) + "%)");

        System.out.println("       paint time: " + formatter.format(paintTime) +
                "ms (" + percentFormatter.format((paintTime / totalTime) * 100) + "%) " +
                paintCount + " shapes");
        System.out.println("       image time: " + formatter.format(imageTime) + "ms");
        System.out.println("  avg. image time: " + formatter.format(imageTime / imageCount) + "ms for "
                + NumberFormat.getNumberInstance(Locale.US).format(imageCount) + " image(s)");
        System.out.println("       total time: " + formatter.format(totalTime));
    }

    private double convert(long duration) {
        return duration / 1.0E09;
    }
}

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

© Copyright 2017 ICEsoft Technologies Canada Corp.