Example usage for org.apache.pdfbox.pdmodel PDPageContentStream PDPageContentStream

List of usage examples for org.apache.pdfbox.pdmodel PDPageContentStream PDPageContentStream

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDPageContentStream PDPageContentStream.

Prototype

public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent, boolean compress)
        throws IOException 

Source Link

Document

Create a new PDPage content stream.

Usage

From source file:com.evanbelcher.DrillBook.display.DBDesktopPane.java

License:Open Source License

/**
 * Prints every page to a pdf file//from   ww w .  j a va  2  s . c om
 *
 * @throws IOException if the file cannot be found or the pdf cannot be created
 */
protected void printAllPagesToPdf() throws IOException {
    io.clearActivePoints();
    ddf.updateAll(io.getActivePoints());
    File f = new File(Main.getFilePath());
    f.mkdirs();
    String fileName = DBMenuBar.cleanseFileName(Main.getState().getCurrentFileName().substring(0,
            Main.getState().getCurrentFileName().length() - 6));

    f = new File(Main.getFilePath() + fileName + " full show" + ".pdf");

    boolean crop = true;
    a: for (int pageNum : Main.getPages().keySet()) {
        for (Point p : Main.getPages().get(pageNum).getDots().keySet()) {
            if (p.getX() < field.getWidth() * 0.1 + field.getX()
                    || p.getX() > field.getWidth() * 0.9 + field.getX()) {
                crop = false;
                break a;
            }
        }
        if (Main.getPages().get(pageNum).getTextPoint().getX() < field.getWidth() * 0.1 + field.getX()
                || Main.getPages().get(pageNum).getTextPoint().getX() + 100 > field.getWidth() * 0.9
                        + field.getX()) {
            crop = false;
            break;
        }
    }

    float scale = 1.0f;
    if (crop)
        scale = 0.8f;
    PDDocument doc = null;

    try {
        doc = new PDDocument();

        for (int i : Main.getPages().keySet()) {
            Main.setCurrentPage(i);

            BufferedImage bi = new BufferedImage(getSize().width, getSize().height,
                    BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.createGraphics();
            paintComponent(g);
            g.dispose();

            PDPage page = new PDPage(
                    new PDRectangle((float) field.getWidth() * scale, (float) field.getHeight()));
            doc.addPage(page);
            PDImageXObject pdImage = LosslessFactory.createFromImage(doc, bi);
            PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true);

            contentStream.drawImage(pdImage, -1 * field.x - (float) (((1 - scale) / 2.0f) * field.getWidth()),
                    -1 * field.y, pdImage.getWidth(), pdImage.getHeight());

            contentStream.close();
        }
        doc.save(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (doc != null)
            doc.close();
        pdf.updateAfterPrintAll();
    }
}

From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java

License:Open Source License

private static void title(String text, PDFont font, int fontSize, PDDocument doc) throws IOException {

    if (positionHeight + (page.getMediaBox().getHeight() / 6) >= page.getMediaBox().getHeight()) {
        if (positionTitleWidth > DEFAULT_TITLE_MARGIN_WIDTH) {
            initNewPage(doc);/*from w ww. ja va 2s  .  c  om*/
        } else {
            writeToRight();
        }
    }

    // Jump line if not the first page title.
    if (positionHeight != DEFAULT_MARGIN_HEIGHT) {
        positionHeight += (titleSpaceHeight * 2);
    }

    PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false);

    stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);

    float textWidth = font.getStringWidth(text) / 1000 * fontSize;
    float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;

    stream.beginText();
    stream.setFont(font, fontSize);

    stream.newLineAtOffset(positionTitleWidth, page.getMediaBox().getHeight() - positionHeight - textHeight);

    stream.showText(text);
    stream.endText();
    stream.close();

    positionHeight += titleSpaceHeight + textHeight;

}

From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java

License:Open Source License

private static int centerText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc)
        throws IOException {

    PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false);

    stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);

    float textWidth = font.getStringWidth(text) / 1000 * fontSize;
    float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;

    stream.beginText();/*from   www.ja  v  a 2 s . co  m*/
    stream.setFont(font, fontSize);
    stream.newLineAtOffset((page.getMediaBox().getWidth() - textWidth) / 2,
            page.getMediaBox().getHeight() - positionHeight - textHeight);
    stream.showText(text);
    stream.endText();
    stream.close();

    positionHeight += textHeight;

    return (int) textHeight;

}

From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java

License:Open Source License

private static void rightText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc)
        throws IOException {

    PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false);

    stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);

    float textWidth = font.getStringWidth(text) / 1000 * fontSize;
    float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;

    stream.beginText();//from  w w w  .ja v  a 2  s  . com
    stream.setFont(font, fontSize);
    stream.newLineAtOffset(page.getMediaBox().getWidth() - textWidth - DEFAULT_LOGO_RIGHT_MARGIN_WIDTH,
            page.getMediaBox().getHeight() - positionHeight - textHeight);
    stream.showText(text);
    stream.endText();
    stream.close();

    positionHeight += textHeight;

}

From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java

License:Open Source License

private static void leftText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc)
        throws IOException {

    PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false);

    stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);

    float textWidth = font.getStringWidth(text) / 1000 * fontSize;
    float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;

    stream.beginText();//from   w ww. j av a  2s.c o m
    stream.setFont(font, fontSize);
    stream.newLineAtOffset(DEFAULT_LOGO_LEFT_MARGIN_WIDTH,
            page.getMediaBox().getHeight() - positionHeight - textHeight);
    stream.showText(text);
    stream.endText();
    stream.close();

    positionHeight += textHeight;

}

From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java

License:Open Source License

private static void centerImage(PDImageXObject image, PDPage page, PDDocument doc, int height, int width)
        throws IOException {

    PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false);

    stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);

    stream.drawImage(image, (page.getMediaBox().getWidth() - width) / 2,
            page.getMediaBox().getHeight() - positionHeight - height, width, height);
    stream.close();// w  w  w  . j  a v a 2 s  . c  om

    positionHeight += height;

}

From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java

License:Open Source License

private static void leftImage(PDImageXObject image, PDPage page, PDDocument doc, int height, int width)
        throws IOException {

    PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false);

    stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);

    stream.drawImage(image, DEFAULT_LOGO_LEFT_MARGIN_WIDTH,
            page.getMediaBox().getHeight() - positionHeight - height, width, height);
    stream.close();//from ww w. j a va 2s  . c  o m

    positionHeight += height;

}

From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java

License:Open Source License

private static void rightImage(PDImageXObject image, PDPage page, PDDocument doc, int height, int width)
        throws IOException {

    PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false);

    stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);

    stream.drawImage(image, (page.getMediaBox().getWidth() - width) - DEFAULT_LOGO_RIGHT_MARGIN_WIDTH,
            page.getMediaBox().getHeight() - positionHeight - height, width, height);
    stream.close();/*from  ww w . j a  va  2s.com*/

    positionHeight += height;

}

From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java

License:Open Source License

private static void attribute(PDImageXObject logo, int logoHeight, int logoWidth, String value, String label,
        PDFont font, int fontSize, PDDocument doc, String index, boolean isPercent, Color color)
        throws IOException {

    int dataFontSize = (int) (fontSize * 1.5f);
    int labelFontSize = fontSize;

    float logoYCoordinate = page.getMediaBox().getHeight() - positionHeight - logoHeight;
    float textWidth = (font.getStringWidth(value) / 1000 * dataFontSize)
            + (font.getStringWidth(label) / 1000 * labelFontSize);
    float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * dataFontSize;
    float textYCoordinate = page.getMediaBox().getHeight() - positionHeight - (logoHeight / 2)
            - (textHeight / 2);/*from  w w w  . java  2s.com*/
    float dataXCoordinate = positionLogoWidth + logoHeight + DEFAULT_SPACE_WIDTH;

    positionHeight += spaceHeight + ((logoHeight < textHeight) ? textHeight : logoHeight);

    if (positionHeight >= page.getMediaBox().getHeight()) {
        if (positionLogoWidth > DEFAULT_ICON_MARGIN_WIDTH) {
            initNewPage(doc);
            attribute(logo, logoHeight, logoWidth, value, label, font, labelFontSize, doc, index, isPercent,
                    color);
        } else {
            writeToRight();
            attribute(logo, logoHeight, logoWidth, value, label, font, labelFontSize, doc, index, isPercent,
                    color);
        }
    }

    else {

        PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false);
        try {
            stream.drawImage(logo, positionLogoWidth, logoYCoordinate, logoWidth, logoHeight);
            stream.beginText();
            stream.setFont(font, dataFontSize);
            stream.setNonStrokingColor(color);
            stream.newLineAtOffset(dataXCoordinate, textYCoordinate + 6);
            stream.showText(value);
            stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);
            stream.setFont(font, labelFontSize);
            stream.showText(label);

            if (index != null) {
                stream.newLineAtOffset(textWidth, (int) (labelFontSize * 0.3));
                stream.setFont(PDType1Font.COURIER_BOLD, (int) (labelFontSize * 0.8));
                stream.showText("(");
                stream.setNonStrokingColor(color);
                stream.showText(index);
                if (isPercent) {
                    stream.showText("%");
                }
                stream.setNonStrokingColor(DEFAULT_TEXT_COLOR);
                stream.showText(")");
            }
        } finally {
            stream.endText();
            stream.close();
        }

    }
}

From source file:com.przemo.pdfmanipulate.PDFBuilder.java

private static void renderForm(Formularz form, PDDocument doc) throws IOException {
    int i = 0;//from w w  w  .  j  a v a  2  s. c o m
    PDFont f = PDType1Font.HELVETICA;
    float fontSize = 17.0f;
    for (FormPage page : form.getPages()) {
        PDPage pag = doc.getPage(i);
        PDRectangle pageSize = pag.getMediaBox();
        try (PDPageContentStream contentStream = new PDPageContentStream(doc, pag,
                PDPageContentStream.AppendMode.APPEND, false)) {
            contentStream.setFont(f, fontSize);
            contentStream.beginText();
            contentStream.setNonStrokingColor(Color.black);
            for (Pole p : page.getPola()) {
                int x = p.getPozycjaX();
                if (p.isRightAlign()) {
                    x -= f.getStringWidth(p.getWartosc()) / 1000 * fontSize;
                }
                contentStream
                        .setTextMatrix(Matrix.getTranslateInstance(x, pageSize.getHeight() - p.getPozycjaY()));
                contentStream.showText(p.getWartosc());
            }
            contentStream.endText();
        }
        i++;
    }

}