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

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

Introduction

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

Prototype

public void rotate(double theta) 

Source Link

Document

Rotares this matrix by the given factors.

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)
    {//  w ww. j  a v a 2s .  com
    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();
        }
    }
}

From source file:org.nmrfx.processor.gui.graphicsio.PDFWriter.java

License:Open Source License

public void showCenteredText(String message, double startX, double startY, String anchor, double rotate)
        throws GraphicsIOException, IllegalArgumentException {
    try {//from w w  w. j  a  va 2 s.c om
        int aLen = anchor.length();
        double xFrac = 0.0;
        double yFrac = 0.0;
        if (aLen > 0) {
            switch (anchor) {
            case "nw":
                xFrac = 0.0;
                yFrac = 1.0;
                break;
            case "n":
                xFrac = 0.5;
                yFrac = 1.0;
                break;
            case "ne":
                xFrac = 1.0;
                yFrac = 1.0;
                break;
            case "e":
                xFrac = 1.0;
                yFrac = 0.5;
                break;
            case "se":
                xFrac = 1.0;
                yFrac = 0.0;
                break;
            case "s":
                xFrac = 0.5;
                yFrac = 0.0;
                break;
            case "sw":
                xFrac = 0.0;
                yFrac = 0.0;
                break;
            case "w":
                xFrac = 0.0;
                yFrac = 0.5;
                break;
            default:
                throw new IllegalArgumentException("Invalid anchor \"" + anchor + "\"");
            }
        }
        font = PDType1Font.HELVETICA;
        float fontSize = 12;
        float stringWidth = font.getStringWidth(message) * fontSize / 1000f;
        PDFontDescriptor pdfFontDescriptor = font.getFontDescriptor();
        float stringHeight = pdfFontDescriptor.getCapHeight() * fontSize / 1000f;
        float xOffset = -stringWidth * (float) xFrac;
        float yOffset = -stringHeight * (float) yFrac;

        contentStream.newLineAtOffset(tX(startX + xOffset), tY(startY - yOffset));
        if (rotate != 0.0) {
            Matrix matrix = new Matrix();
            matrix.translate(tX(startX), tY(startY));
            matrix.rotate(rotate * Math.PI / 180.0);
            matrix.translate(xOffset, -yOffset);
            contentStream.setTextMatrix(matrix);
        }

        contentStream.showText(message);
    } catch (IOException ioE) {
        throw new GraphicsIOException(ioE.getMessage());
    }

}