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

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

Introduction

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

Prototype

public static Matrix getRotateInstance(double theta, float tx, float ty) 

Source Link

Document

Convenience method to create a rotated instance.

Usage

From source file:Bulletin.myPdf.java

public float TextCentered(String text, PDFont font, int size, float X, float Y, float rowHeight) {
    try {// www.j a  v  a  2s  . c  o m
        this.cos.beginText();
        this.cos.setFont(font, Math.round(textScale * size));
        float dtw = 0.5f * getTextWidth(font, text, size);
        float dth = 0.3f * getFontHeight(font, size);
        int nbLines = text.split("#").length;
        Y += 0.5f * (nbLines - 1) * rowHeight;

        for (String line : text.split("#")) {
            if (line.length() == 0) {
                continue;
            }
            //                System.out.println("X="+X+" Y="+Y);
            //                System.out.println("dtw="+dtw+" dth="+dth);
            Matrix M = Matrix.getTranslateInstance(X, Y);
            M.concatenate(Matrix.getRotateInstance(this.textAngle, 0, 0));
            M.concatenate(Matrix.getTranslateInstance(-dtw, -dth));

            this.cos.setTextMatrix(M);
            this.cos.showText(this.fix(line));
            //                this.cos.showText(new String(Base64.getDecoder().decode(line), "UTF-8"));

            Y -= rowHeight;
        }
        this.cos.endText();
        this.cos.stroke();

    } catch (IOException ex) {
        Logger.getLogger(Bulletin2.class.getName()).log(Level.SEVERE, null, ex);
    }
    return rowHeight;
}

From source file:Bulletin.myPdf.java

public float TextLeft(String text, PDFont font, int size, float X, float Y, float rowHeight) {
    try {//from  www .  j a  v a  2  s . com
        this.cos.beginText();
        this.cos.setFont(font, Math.round(textScale * size));
        float dth = 0.3f * getFontHeight(font, size);
        int nbLines = text.split("#").length;
        Y += 0.5f * (nbLines - 1) * rowHeight;

        for (String line : text.split("#")) {
            if (line.length() == 0) {
                continue;
            }
            //                System.out.println("X="+X+" Y="+Y);
            //                System.out.println("dtw="+dtw+" dth="+dth);
            Matrix M = Matrix.getTranslateInstance(X, Y);
            M.concatenate(Matrix.getRotateInstance(this.textAngle, 0, 0));
            M.concatenate(Matrix.getTranslateInstance(0, -dth));

            this.cos.setTextMatrix(M);
            this.cos.showText(this.fix(line));
            //                this.cos.showText(new String(Base64.getDecoder().decode(line), "UTF-8"));
            Y -= rowHeight;
        }
        this.cos.endText();
        this.cos.stroke();

    } catch (IOException ex) {
        Logger.getLogger(Bulletin2.class.getName()).log(Level.SEVERE, null, ex);
    }
    return rowHeight;
}

From source file:Bulletin.test.java

public static void test2() {
    try {/*from w ww  .j a v a 2 s .c  o  m*/
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        PDPageContentStream cos = null;
        try {
            cos = new PDPageContentStream(document, page);
        } catch (IOException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }

        float X = 501.4f;
        float Y = 556.0f;
        //            String text = "HALLO";
        //            String text = "HALLO#HOE#GAAT#HET";
        //            String text = "HALLO#HOE#GAAT";
        String text = "Non atteints";
        int rh = 10;

        cos.moveTo(X, Y - 50);
        cos.lineTo(X, Y + 50);
        cos.stroke();
        cos.moveTo(X - 50, Y);
        cos.lineTo(X + 50, Y);
        cos.stroke();

        cos.beginText();
        cos.setFont(PDType1Font.HELVETICA, 6);
        float dtw = 0.5f * getTextWidth(PDType1Font.HELVETICA, text, 6);
        float dth = 0.0f * getFontHeight(PDType1Font.HELVETICA, 6);
        int nbLines = text.split("#").length;

        Y += 0.5f * (nbLines - 1) * rh;

        for (String line : text.split("#")) {
            Matrix M = Matrix.getTranslateInstance(X, Y);
            M.concatenate(Matrix.getRotateInstance(Math.toRadians(0), 0, 0));
            M.concatenate(Matrix.getTranslateInstance(-dtw, -dth));
            cos.setTextMatrix(M);
            cos.showText(line);
            Y -= rh;
        }
        cos.close();
        document.addPage(page);
        document.save("/tmp/test.pdf");
        document.close();

    } catch (IOException ex) {
        Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.esteco.jira.pdf.UsingTextMatrix.java

License:Apache License

/**
 * creates a sample document with some text using a text matrix.
 *
 * @param message The message to write in the file.
 * @param outfile The resulting PDF.//from w  w w .j  a  v  a2 s  .  com
 * @throws IOException If there is an error writing the data.
 */
public void doIt(String message, String outfile) throws IOException {
    // the document
    PDDocument doc = null;
    try {
        doc = new PDDocument();

        // Page 1
        PDFont font = PDType1Font.HELVETICA;
        PDPage page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        float fontSize = 12.0f;

        PDRectangle pageSize = page.getMediaBox();
        float centeredXPosition = (pageSize.getWidth() - fontSize / 1000f) / 2f;
        float stringWidth = font.getStringWidth(message);
        float centeredYPosition = (pageSize.getHeight() - (stringWidth * fontSize) / 1000f) / 3f;

        PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();
        // counterclockwise rotation
        for (int i = 0; i < 8; i++) {
            contentStream.setTextMatrix(Matrix.getRotateInstance(i * Math.PI * 0.25, centeredXPosition,
                    pageSize.getHeight() - centeredYPosition));
            contentStream.showText(message + " " + i);
        }
        // clockwise rotation
        for (int i = 0; i < 8; i++) {
            contentStream.setTextMatrix(
                    Matrix.getRotateInstance(-i * Math.PI * 0.25, centeredXPosition, centeredYPosition));
            contentStream.showText(message + " " + i);
        }

        contentStream.endText();
        contentStream.close();

        // Page 2
        page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        fontSize = 1.0f;

        contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();

        // text scaling and translation
        for (int i = 0; i < 10; i++) {
            contentStream.setTextMatrix(new Matrix(12 + (i * 6), 0, 0, 12 + (i * 6), 100, 100 + i * 50));
            contentStream.showText(message + " " + i);
        }
        contentStream.endText();
        contentStream.close();

        // Page 3
        page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        fontSize = 1.0f;

        contentStream = new PDPageContentStream(doc, page, AppendMode.OVERWRITE, false);
        contentStream.setFont(font, fontSize);
        contentStream.beginText();

        int i = 0;
        // text scaling combined with rotation
        contentStream.setTextMatrix(new Matrix(12, 0, 0, 12, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(0, 18, -18, 0, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(-24, 0, 0, -24, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.setTextMatrix(new Matrix(0, -30, 30, 0, centeredXPosition, centeredYPosition * 1.5f));
        contentStream.showText(message + " " + i++);

        contentStream.endText();
        contentStream.close();

        doc.save(outfile);
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
}