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

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

Introduction

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

Prototype

public void translate(float tx, float ty) 

Source Link

Document

Translates this matrix by the given amount.

Usage

From source file:de.rototor.pdfbox.graphics2d.PdfBoxGraphics2DTestBase.java

License:Apache License

@SuppressWarnings("SpellCheckingInspection")
void exportGraphic(String dir, String name, GraphicsExporter exporter) {
    try {/*from  w ww  .  java  2 s .com*/
        PDDocument document = new PDDocument();

        PDFont pdArial = PDFontFactory.createDefaultFont();

        File parentDir = new File("target/test/" + dir);
        // noinspection ResultOfMethodCallIgnored
        parentDir.mkdirs();

        BufferedImage image = new BufferedImage(400, 400, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D imageGraphics = image.createGraphics();
        exporter.draw(imageGraphics);
        imageGraphics.dispose();
        ImageIO.write(image, "PNG", new File(parentDir, name + ".png"));

        for (Mode m : Mode.values()) {
            PDPage page = new PDPage(PDRectangle.A4);
            document.addPage(page);

            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            PdfBoxGraphics2D pdfBoxGraphics2D = new PdfBoxGraphics2D(document, 400, 400);
            PdfBoxGraphics2DFontTextDrawer fontTextDrawer = null;
            contentStream.beginText();
            contentStream.setStrokingColor(0, 0, 0);
            contentStream.setNonStrokingColor(0, 0, 0);
            contentStream.setFont(PDType1Font.HELVETICA_BOLD, 15);
            contentStream.setTextMatrix(Matrix.getTranslateInstance(10, 800));
            contentStream.showText("Mode " + m);
            contentStream.endText();
            switch (m) {
            case FontTextIfPossible:
                fontTextDrawer = new PdfBoxGraphics2DFontTextDrawer();
                registerFots(fontTextDrawer);
                break;
            case DefaultFontText: {
                fontTextDrawer = new PdfBoxGraphics2DFontTextDrawerDefaultFonts();
                registerFots(fontTextDrawer);
                break;
            }
            case ForceFontText:
                fontTextDrawer = new PdfBoxGraphics2DFontTextForcedDrawer();
                registerFots(fontTextDrawer);
                fontTextDrawer.registerFont("Arial", pdArial);
                break;
            case DefaultVectorized:
            default:
                break;
            }

            if (fontTextDrawer != null) {
                pdfBoxGraphics2D.setFontTextDrawer(fontTextDrawer);
            }

            exporter.draw(pdfBoxGraphics2D);
            pdfBoxGraphics2D.dispose();

            PDFormXObject appearanceStream = pdfBoxGraphics2D.getXFormObject();
            Matrix matrix = new Matrix();
            matrix.translate(0, 20);
            contentStream.transform(matrix);
            contentStream.drawForm(appearanceStream);

            matrix.scale(1.5f, 1.5f);
            matrix.translate(0, 100);
            contentStream.transform(matrix);
            contentStream.drawForm(appearanceStream);
            contentStream.close();
        }

        document.save(new File(parentDir, name + ".pdf"));
        document.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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  v  a  2s .c  o  m
        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());
    }

}