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

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

Introduction

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

Prototype

public void scale(float sx, float sy) 

Source Link

Document

Scales this matrix by the given factors.

Usage

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

License:Apache License

@Override
public void drawText(AttributedCharacterIterator iterator, IFontTextDrawerEnv env)
        throws IOException, FontFormatException {
    PDPageContentStream contentStream = env.getContentStream();

    contentStream.beginText();/*from   www .j a  v a  2s.com*/

    Matrix textMatrix = new Matrix();
    textMatrix.scale(1, -1);
    contentStream.setTextMatrix(textMatrix);

    StringBuilder sb = new StringBuilder();
    boolean run = true;
    while (run) {

        Font attributeFont = (Font) iterator.getAttribute(TextAttribute.FONT);
        if (attributeFont == null)
            attributeFont = env.getFont();

        Number fontSize = ((Number) iterator.getAttribute(TextAttribute.SIZE));
        if (fontSize != null)
            attributeFont = attributeFont.deriveFont(fontSize.floatValue());
        PDFont font = applyFont(attributeFont, env);

        Paint paint = (Paint) iterator.getAttribute(TextAttribute.FOREGROUND);
        if (paint == null)
            paint = env.getPaint();

        /*
         * Apply the paint
         */
        env.applyPaint(paint);

        boolean isStrikeThrough = TextAttribute.STRIKETHROUGH_ON
                .equals(iterator.getAttribute(TextAttribute.STRIKETHROUGH));
        boolean isUnderline = TextAttribute.UNDERLINE_ON.equals(iterator.getAttribute(TextAttribute.UNDERLINE));
        boolean isLigatures = TextAttribute.LIGATURES_ON.equals(iterator.getAttribute(TextAttribute.LIGATURES));

        run = iterateRun(iterator, sb);
        String text = sb.toString();

        /*
         * If we force the text write we may encounter situations where the font can not
         * display the characters. PDFBox will throw an exception in this case. We will
         * just silently ignore the text and not display it instead.
         */
        try {
            showTextOnStream(env, contentStream, attributeFont, font, isStrikeThrough, isUnderline, isLigatures,
                    text);
        } catch (IllegalArgumentException e) {
            if (font instanceof PDType1Font && !font.isEmbedded()) {
                /*
                 * We tried to use a builtin default font, but it does not have the needed
                 * characters. So we use a embedded font as fallback.
                 */
                try {
                    if (fallbackFontUnknownEncodings == null)
                        fallbackFontUnknownEncodings = findFallbackFont(env);
                    if (fallbackFontUnknownEncodings != null) {
                        env.getContentStream().setFont(fallbackFontUnknownEncodings, attributeFont.getSize2D());
                        showTextOnStream(env, contentStream, attributeFont, fallbackFontUnknownEncodings,
                                isStrikeThrough, isUnderline, isLigatures, text);
                        e = null;
                    }
                } catch (IllegalArgumentException e1) {
                    e = e1;
                }
            }

            if (e != null)
                System.err.println("PDFBoxGraphics: Can not map text " + text + " with font "
                        + attributeFont.getFontName() + ": " + e.getMessage());
        }
    }
    contentStream.endText();
}

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

License:Apache License

@SuppressWarnings("SpellCheckingInspection")
void exportGraphic(String dir, String name, GraphicsExporter exporter) {
    try {/*from   ww w  .j  a  v  a 2  s. co m*/
        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);
    }
}