Example usage for org.apache.pdfbox.pdmodel.common PDRectangle PDRectangle

List of usage examples for org.apache.pdfbox.pdmodel.common PDRectangle PDRectangle

Introduction

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

Prototype

public PDRectangle(float width, float height) 

Source Link

Document

Constructor.

Usage

From source file:com.cdd.bao.template.RenderSchema.java

License:Open Source License

public void createPageTemplate() throws IOException {
    RenderContext ctx = new RenderContext();

    renderPageTemplate(ctx);/* w ww .j ava 2  s  .  c  o m*/
    if (ctx.width == 0 || ctx.height == 0)
        return;

    PDPage page = new PDPage(new PDRectangle(ctx.width, ctx.height));
    document.addPage(page);

    ctx.stream = new PDPageContentStream(document, page);
    renderPageTemplate(ctx);
    ctx.stream.close();
}

From source file:com.cdd.bao.template.RenderSchema.java

License:Open Source License

public void createPageAssay(Schema.Assay assay) throws IOException {
    RenderContext ctx = new RenderContext();

    renderPageTemplate(ctx);//from ww w  .j  ava  2s.c om
    if (ctx.width == 0 || ctx.height == 0)
        return;

    PDPage page = new PDPage(new PDRectangle(ctx.width, ctx.height));
    document.addPage(page);

    ctx.stream = new PDPageContentStream(document, page);
    renderPageAssay(ctx, assay);
    ctx.stream.close();
}

From source file:com.cdd.bao.template.RenderSchema.java

License:Open Source License

public void createPageProperties() throws IOException {
    RenderContext ctx = new RenderContext();

    renderPageHierarchy(ctx, vocab.getPropertyHierarchy());
    if (ctx.width == 0 || ctx.height == 0)
        return;/*from   w  ww.java2s .c  o m*/

    PDPage page = new PDPage(new PDRectangle(ctx.width, ctx.height));
    document.addPage(page);

    ctx.stream = new PDPageContentStream(document, page);
    renderPageHierarchy(ctx, vocab.getPropertyHierarchy());
    ctx.stream.close();
}

From source file:com.cdd.bao.template.RenderSchema.java

License:Open Source License

public void createPageValues() throws IOException {
    RenderContext ctx = new RenderContext();

    renderPageHierarchy(ctx, vocab.getValueHierarchy());
    if (ctx.width == 0 || ctx.height == 0)
        return;//from  w  ww  .j a v  a  2  s  .  c  o m

    PDPage page = new PDPage(new PDRectangle(ctx.width, ctx.height));
    document.addPage(page);

    ctx.stream = new PDPageContentStream(document, page);
    renderPageHierarchy(ctx, vocab.getValueHierarchy());
    ctx.stream.close();
}

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

License:Open Source License

/**
 * Prints the current page to a pdf file
 *
 * @throws IOException if the file cannot be found or the pdf cannot be created
 *///from  w w  w  . j av a2  s. c  o  m
protected void printCurrentPageToPdf() throws IOException {
    io.clearActivePoints();
    ddf.updateAll(io.getActivePoints());
    String fileName = DBMenuBar.cleanseFileName(
            Main.getState().getCurrentFileName().substring(0, Main.getState().getCurrentFileName().length() - 6)
                    + ": " + Main.getCurrentPage().toDisplayString().replaceAll("\\|", "-"));
    File f = new File(Main.getFilePath());
    f.mkdirs();
    f = new File(Main.getFilePath() + fileName + ".pdf");

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

    PDDocument doc = null;

    try {
        doc = new PDDocument();
        boolean crop = true;
        State.print(field);
        for (Point p : Main.getCurrentPage().getDots().keySet())
            if (p.getX() < field.getWidth() * 0.1 + field.getX()
                    || p.getX() > field.getWidth() * 0.9 + field.getX()) {
                crop = false;
                break;
            }
        if (Main.getCurrentPage().getTextPoint().getX() < field.getWidth() * 0.1 + field.getX()
                || Main.getCurrentPage().getTextPoint().getX() + 100 > field.getWidth() * 0.9 + field.getX())
            crop = false;

        float scale = 1.0f;
        if (crop)
            scale = 0.8f;

        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);
    } finally {
        if (doc != null)
            doc.close();
    }
}

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

License:Open Source License

/**
 * Prints every page to a pdf file/*from   w w  w.  j  a v  a  2 s  . com*/
 *
 * @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.fileOperations.ImageToPDF.java

/**
 * create PDF from image and scales it accordingly to fit in a single page
 *
 * @param folderPath String/*from   w w w.j  ava 2s. co m*/
 * @param imageFileName String
 * @return String new filename
 */
public static String createPDFFromImage(String folderPath, String imageFileName) {
    String pdfFile = FilenameUtils.removeExtension(imageFileName) + ".pdf";
    String image = folderPath + imageFileName;
    PDImageXObject pdImage = null;
    File imageFile = null;
    FileInputStream fileStream = null;
    BufferedImage bim = null;

    File attachmentLocation = new File(folderPath);
    if (!attachmentLocation.exists()) {
        attachmentLocation.mkdirs();
    }

    // the document
    PDDocument doc = null;
    PDPageContentStream contentStream = null;

    try {
        doc = new PDDocument();
        PDPage page = new PDPage(PDRectangle.LETTER);
        float margin = 72;
        float pageWidth = page.getMediaBox().getWidth() - 2 * margin;
        float pageHeight = page.getMediaBox().getHeight() - 2 * margin;

        if (image.toLowerCase().endsWith(".jpg")) {
            imageFile = new File(image);
            fileStream = new FileInputStream(image);
            pdImage = JPEGFactory.createFromStream(doc, fileStream);
            //            } else if ((image.toLowerCase().endsWith(".tif")
            //                    || image.toLowerCase().endsWith(".tiff"))
            //                    && TIFFCompression(image) == COMPRESSION_GROUP4) {
            //                imageFile = new File(image);
            //                pdImage = CCITTFactory.createFromFile(doc, imageFile);
        } else if (image.toLowerCase().endsWith(".gif") || image.toLowerCase().endsWith(".bmp")
                || image.toLowerCase().endsWith(".png")) {
            imageFile = new File(image);
            bim = ImageIO.read(imageFile);
            pdImage = LosslessFactory.createFromImage(doc, bim);
        }

        if (pdImage != null) {
            if (pdImage.getWidth() > pdImage.getHeight()) {
                page.setRotation(270);
                PDRectangle rotatedPage = new PDRectangle(page.getMediaBox().getHeight(),
                        page.getMediaBox().getWidth());
                page.setMediaBox(rotatedPage);
                pageWidth = page.getMediaBox().getWidth() - 2 * margin;
                pageHeight = page.getMediaBox().getHeight() - 2 * margin;
            }
            Dimension pageSize = new Dimension((int) pageWidth, (int) pageHeight);
            Dimension imageSize = new Dimension(pdImage.getWidth(), pdImage.getHeight());
            Dimension scaledDim = PDFBoxTools.getScaledDimension(imageSize, pageSize);
            float startX = page.getMediaBox().getLowerLeftX() + margin;
            float startY = page.getMediaBox().getUpperRightY() - margin - scaledDim.height;

            doc.addPage(page);
            contentStream = new PDPageContentStream(doc, page);
            contentStream.drawImage(pdImage, startX, startY, scaledDim.width, scaledDim.height);
            contentStream.close();
            doc.save(folderPath + pdfFile);
        }
    } catch (IOException ex) {
        ExceptionHandler.Handle(ex);
        return "";
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException ex) {
                ExceptionHandler.Handle(ex);
                return "";
            }
        }
    }
    if (fileStream != null) {
        try {
            fileStream.close();
        } catch (IOException ex) {
            ExceptionHandler.Handle(ex);
            return "";
        }
    }
    if (bim != null) {
        bim.flush();
    }
    if (imageFile != null) {
        imageFile.delete();
    }
    return pdfFile;
}

From source file:com.fileOperations.ImageToPDF.java

/**
 * create PDF from image and scales it accordingly to fit in a single page
 *
 * @param folderPath String/* w ww.j a  v a2 s  .  co  m*/
 * @param imageFileName String
 * @return String new filename
 */
public static String createPDFFromImageNoDelete(String folderPath, String imageFileName) {
    String pdfFile = FilenameUtils.removeExtension(imageFileName) + ".pdf";
    String image = folderPath + imageFileName;
    PDImageXObject pdImage = null;
    File imageFile = null;
    FileInputStream fileStream = null;
    BufferedImage bim = null;

    File attachmentLocation = new File(folderPath);
    if (!attachmentLocation.exists()) {
        attachmentLocation.mkdirs();
    }

    // the document
    PDDocument doc = null;
    PDPageContentStream contentStream = null;

    try {
        doc = new PDDocument();
        PDPage page = new PDPage(PDRectangle.LETTER);
        float margin = 72;
        float pageWidth = page.getMediaBox().getWidth() - 2 * margin;
        float pageHeight = page.getMediaBox().getHeight() - 2 * margin;

        if (image.toLowerCase().endsWith(".jpg")) {
            imageFile = new File(image);
            fileStream = new FileInputStream(image);
            pdImage = JPEGFactory.createFromStream(doc, fileStream);
            //            } else if ((image.toLowerCase().endsWith(".tif")
            //                    || image.toLowerCase().endsWith(".tiff"))
            //                    && TIFFCompression(image) == COMPRESSION_GROUP4) {
            //                imageFile = new File(image);
            //                pdImage = CCITTFactory.createFromFile(doc, imageFile);
        } else if (image.toLowerCase().endsWith(".gif") || image.toLowerCase().endsWith(".bmp")
                || image.toLowerCase().endsWith(".png")) {
            imageFile = new File(image);
            bim = ImageIO.read(imageFile);
            pdImage = LosslessFactory.createFromImage(doc, bim);
        }

        if (pdImage != null) {
            if (pdImage.getWidth() > pdImage.getHeight()) {
                page.setRotation(270);
                PDRectangle rotatedPage = new PDRectangle(page.getMediaBox().getHeight(),
                        page.getMediaBox().getWidth());
                page.setMediaBox(rotatedPage);
                pageWidth = page.getMediaBox().getWidth() - 2 * margin;
                pageHeight = page.getMediaBox().getHeight() - 2 * margin;
            }
            Dimension pageSize = new Dimension((int) pageWidth, (int) pageHeight);
            Dimension imageSize = new Dimension(pdImage.getWidth(), pdImage.getHeight());
            Dimension scaledDim = PDFBoxTools.getScaledDimension(imageSize, pageSize);
            float startX = page.getMediaBox().getLowerLeftX() + margin;
            float startY = page.getMediaBox().getUpperRightY() - margin - scaledDim.height;

            doc.addPage(page);
            contentStream = new PDPageContentStream(doc, page);
            contentStream.drawImage(pdImage, startX, startY, scaledDim.width, scaledDim.height);
            contentStream.close();
            doc.save(folderPath + pdfFile);
        }
    } catch (IOException ex) {
        ExceptionHandler.Handle(ex);
        return "";
    } finally {
        if (doc != null) {
            try {
                doc.close();
            } catch (IOException ex) {
                ExceptionHandler.Handle(ex);
                return "";
            }
        }
    }
    if (fileStream != null) {
        try {
            fileStream.close();
        } catch (IOException ex) {
            ExceptionHandler.Handle(ex);
            return "";
        }
    }
    if (bim != null) {
        bim.flush();
    }
    return pdfFile;
}

From source file:com.helger.pdflayout.spec.SizeSpec.java

License:Apache License

@Nonnull
public PDRectangle getAsRectangle() {
    return new PDRectangle(m_fWidth, m_fHeight);
}

From source file:com.jaromin.alfresco.repo.content.transform.CorelDrawContentTransformer.java

License:Apache License

/**
 * //  w w  w.  j a va  2s .  c o m
 * @param pageImages
 * @param out
 * @throws IOException
 * @throws FileNotFoundException
 * @throws COSVisitorException
 */
private void buildPdfFromImages(Map<File, Dimension> pageImages, OutputStream out)
        throws IOException, FileNotFoundException, COSVisitorException {
    PDDocument doc = new PDDocument();
    for (Map.Entry<File, Dimension> entry : pageImages.entrySet()) {
        File pFile = entry.getKey();
        Dimension d = entry.getValue();
        PDRectangle size = new PDRectangle(d.width, d.height);
        PDPage page = new PDPage(size);
        doc.addPage(page);

        PDXObjectImage ximage = new PDJpeg(doc, new FileInputStream(pFile));
        PDPageContentStream contentStream = new PDPageContentStream(doc, page);
        contentStream.drawImage(ximage, size.getLowerLeftX(), size.getLowerLeftY());
        contentStream.close();
    }
    doc.save(out);
}