Example usage for org.apache.pdfbox.util Matrix getScaleInstance

List of usage examples for org.apache.pdfbox.util Matrix getScaleInstance

Introduction

In this page you can find the example usage for org.apache.pdfbox.util Matrix getScaleInstance.

Prototype

public static Matrix getScaleInstance(float sx, float sy) 

Source Link

Document

Convenience method to create a scaled instance.

Usage

From source file:com.truckzoo.test.pdf.SuperimposePage.java

License:Apache License

public static void main(String[] args) throws IOException {
    /*if (args.length != 2)
    {//  www. ja  v a2s.co  m
    System.err.println("usage: " + SuperimposePage.class.getName() +
            " <source-pdf> <dest-pdf>");
    System.exit(1);
    }*/
    String sourcePath = args[0];
    String destPath = args[1];

    PDDocument sourceDoc = null;
    try {
        // load the source PDF
        sourceDoc = PDDocument.load(new File(sourcePath));
        int sourcePage = 1;

        // create a new PDF and add a blank page
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);

        // write some sample text to the new page
        PDPageContentStream contents = new PDPageContentStream(doc, page);
        contents.beginText();
        contents.setFont(PDType1Font.HELVETICA_BOLD, 12);
        contents.newLineAtOffset(2, PDRectangle.LETTER.getHeight() - 12);
        contents.showText("Sample text");
        contents.endText();

        // Create a Form XObject from the source document using LayerUtility
        LayerUtility layerUtility = new LayerUtility(doc);
        PDFormXObject form = layerUtility.importPageAsForm(sourceDoc, sourcePage - 1);

        // draw the full form
        contents.drawForm(form);

        // draw a scaled form
        contents.saveGraphicsState();
        Matrix matrix = Matrix.getScaleInstance(0.5f, 0.5f);
        contents.transform(matrix);
        contents.drawForm(form);
        contents.restoreGraphicsState();

        // draw a scaled and rotated form
        contents.saveGraphicsState();
        matrix.rotate(1.8 * Math.PI); // radians
        contents.transform(matrix);
        contents.drawForm(form);
        contents.restoreGraphicsState();

        contents.close();
        doc.save(destPath);
        doc.close();
    } finally {
        if (sourceDoc != null) {
            sourceDoc.close();
        }
    }
}