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

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

Introduction

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

Prototype

PDRectangle LETTER

To view the source code for org.apache.pdfbox.pdmodel.common PDRectangle LETTER.

Click Source Link

Document

A rectangle the size of U.S.

Usage

From source file:ambroafb.general.PDFHelper.java

public void replace(BufferedImage img, int index) throws IOException {
    PDPage old = doc.getPage(index);//from  www  .j a  v a 2  s .  c  om
    float width, height;
    if (img.getWidth() > img.getHeight()) {
        width = PDRectangle.LETTER.getHeight();
        height = PDRectangle.LETTER.getWidth();
    } else {
        height = PDRectangle.LETTER.getHeight();
        width = PDRectangle.LETTER.getWidth();
    }
    PDPage newPage = new PDPage(new PDRectangle(width, height));
    doc.getPages().insertAfter(newPage, old);
    doc.removePage(old);
    PDImageXObject pdImage = JPEGFactory.createFromImage(doc, img);
    try (PDPageContentStream contents = new PDPageContentStream(doc, newPage)) {
        contents.drawImage(pdImage, 0, 0, width, height);
    }
}

From source file:com.baseprogramming.pdwriter.model.PageMetadata.java

License:Apache License

public PageMetadata(Margin margin) {
    this.margin = margin;
    this.pageFormat = PDRectangle.LETTER;
}

From source file:com.baseprogramming.pdwriter.PdWriter.java

License:Apache License

public PdWriter(PDDocument document, Margin margin) {
    this(new PageMetadata(PDRectangle.LETTER, margin), document);
}

From source file:com.fileOperations.ImageToPDF.java

/**
 * create PDF from image and scales it accordingly to fit in a single page
 *
 * @param folderPath String/*ww  w  . ja v a 2 s .  c o  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//from w  ww.  j a va 2  s.  c  o  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.truckzoo.test.pdf.SuperimposePage.java

License:Apache License

public static void main(String[] args) throws IOException {
    /*if (args.length != 2)
    {//from  w w w.j  av  a2s . c o  m
    System.err.println("usage: " + SuperimposePage.class.getName() +
            " <source-pdf> <dest-pdf>");
    System.exit(1);
    }*/
    String sourcePath = args[0];
    String destPath = args[1];

    PDDocument sourceDoc = null;
    try {
        // load the source PDF
        sourceDoc = PDDocument.load(new File(sourcePath));
        int sourcePage = 1;

        // create a new PDF and add a blank page
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);

        // write some sample text to the new page
        PDPageContentStream contents = new PDPageContentStream(doc, page);
        contents.beginText();
        contents.setFont(PDType1Font.HELVETICA_BOLD, 12);
        contents.newLineAtOffset(2, PDRectangle.LETTER.getHeight() - 12);
        contents.showText("Sample text");
        contents.endText();

        // Create a Form XObject from the source document using LayerUtility
        LayerUtility layerUtility = new LayerUtility(doc);
        PDFormXObject form = layerUtility.importPageAsForm(sourceDoc, sourcePage - 1);

        // draw the full form
        contents.drawForm(form);

        // draw a scaled form
        contents.saveGraphicsState();
        Matrix matrix = Matrix.getScaleInstance(0.5f, 0.5f);
        contents.transform(matrix);
        contents.drawForm(form);
        contents.restoreGraphicsState();

        // draw a scaled and rotated form
        contents.saveGraphicsState();
        matrix.rotate(1.8 * Math.PI); // radians
        contents.transform(matrix);
        contents.drawForm(form);
        contents.restoreGraphicsState();

        contents.close();
        doc.save(destPath);
        doc.close();
    } finally {
        if (sourceDoc != null) {
            sourceDoc.close();
        }
    }
}

From source file:org.dspace.disseminate.CitationDocumentServiceImpl.java

License:BSD License

@Override
public File makeCitedDocument(Context context, Bitstream bitstream)
        throws IOException, SQLException, AuthorizeException {
    PDDocument document = new PDDocument();
    PDDocument sourceDocument = new PDDocument();
    try {//from www .  ja  va2s.c o m
        Item item = (Item) bitstreamService.getParentObject(context, bitstream);
        sourceDocument = sourceDocument.load(bitstreamService.retrieve(context, bitstream));
        PDPage coverPage = new PDPage(PDRectangle.LETTER); // TODO: needs to be configurable
        generateCoverPage(context, document, coverPage, item);
        addCoverPageToDocument(document, sourceDocument, coverPage);

        document.save(tempDir.getAbsolutePath() + "/bitstream.cover.pdf");
        return new File(tempDir.getAbsolutePath() + "/bitstream.cover.pdf");
    } finally {
        sourceDocument.close();
        document.close();
    }
}

From source file:org.fit.cssbox.render.PDFRenderer.java

License:Open Source License

/**
 * Constructor//from   www .j  ava2 s. co  m
 * 
 * initialize the variables
 */
public PDFRenderer(int rootWidth, int rootHeight, OutputStream out, String pageFormat) {
    this.rootHeight = rootHeight;
    this.pathToSave = out;
    this.pageCount = 0;

    switch (pageFormat) {
    case "A0":
        this.pageFormat = PDRectangle.A0;
        break;
    case "A1":
        this.pageFormat = PDRectangle.A1;
        break;
    case "A2":
        this.pageFormat = PDRectangle.A2;
        break;
    case "A3":
        this.pageFormat = PDRectangle.A3;
        break;
    case "A4":
        this.pageFormat = PDRectangle.A4;
        break;
    case "A5":
        this.pageFormat = PDRectangle.A5;
        break;
    case "A6":
        this.pageFormat = PDRectangle.A6;
        break;
    case "LETTER":
        this.pageFormat = PDRectangle.LETTER;
        break;
    default:
        this.pageFormat = PDRectangle.A4;
        break;
    }

    initSettings(rootWidth);
}

From source file:org.nmrfx.processor.gui.graphicsio.PDFWriter.java

License:Open Source License

public void create(boolean landScape, double width, double height, String fileName) throws GraphicsIOException {
    // the document
    this.landScape = landScape;
    this.fileName = fileName;
    doc = new PDDocument();
    try {//from  ww  w .  jav a2  s .com
        PDPage page = new PDPage(PDRectangle.LETTER);
        doc.addPage(page);
        PDRectangle pageSize = page.getMediaBox();
        pageWidth = pageSize.getWidth();
        pageHeight = pageSize.getHeight();
        contentStream = new PDPageContentStream(doc, page, false, false);
        // add the rotation using the current transformation matrix
        // including a translation of pageWidth to use the lower left corner as 0,0 reference
        if (landScape) {
            page.setRotation(90);
            contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0));
        }
    } catch (IOException ioE) {
        throw new GraphicsIOException(ioE.getMessage());
    }
}