org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PdfGenerator.java    From blog-tutorials with MIT License 6 votes vote down vote up
public byte[] createPdf() throws IOException {
  try (PDDocument document = new PDDocument()) {
    PDPage page = new PDPage(PDRectangle.A4);
    page.setRotation(90);

    float pageWidth = page.getMediaBox().getWidth();
    float pageHeight = page.getMediaBox().getHeight();

    PDPageContentStream contentStream = new PDPageContentStream(document, page);

    PDImageXObject chartImage = JPEGFactory.createFromImage(document,
      createChart((int) pageHeight, (int) pageWidth));

    contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0));
    contentStream.drawImage(chartImage, 0, 0);
    contentStream.close();

    document.addPage(page);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    document.save(byteArrayOutputStream);
    return byteArrayOutputStream.toByteArray();
  }

}
 
Example #2
Source File: DrawImage.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/58606529/pdf-size-too-large-generating-through-android-pdfdocument-and-while-using-pdfbo">
 * PDF size too large generating through Android PDFDocument. And while using pdfbox it is cutting image in output
 * </a>
 * <p>
 * This code shows how to draw an image onto a page with
 * the image "default size".
 * </p>
 */
@Test
public void testDrawImageToFitPage() throws IOException {
    try (   InputStream imageResource = getClass().getResourceAsStream("Willi-1.jpg")) {
        PDDocument document = new PDDocument();

        PDImageXObject ximage = JPEGFactory.createFromStream(document,imageResource);

        PDPage page = new PDPage(new PDRectangle(ximage.getWidth(), ximage.getHeight()));
        document.addPage(page);

        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        contentStream.drawImage(ximage, 0, 0);
        contentStream.close();

        document.save(new File(RESULT_FOLDER, "Willi-1.pdf"));
        document.close();
    }
}
 
Example #3
Source File: PdfCompressImagesBatchController.java    From MyBox with Apache License 2.0 4 votes vote down vote up
@Override
public int handleCurrentPage() {
    int count = 0;
    try {
        PDPage sourcePage = doc.getPage(currentParameters.currentPage - 1);  // 0-based
        PDResources pdResources = sourcePage.getResources();
        pdResources.getXObjectNames();
        Iterable<COSName> iterable = pdResources.getXObjectNames();
        if (iterable == null) {
            return 0;
        }
        Iterator<COSName> pageIterator = iterable.iterator();
        while (pageIterator.hasNext()) {
            if (task.isCancelled()) {
                break;
            }
            COSName cosName = pageIterator.next();
            if (!pdResources.isImageXObject(cosName)) {
                continue;
            }
            PDImageXObject pdxObject = (PDImageXObject) pdResources.getXObject(cosName);
            BufferedImage sourceImage = pdxObject.getImage();
            PDImageXObject newObject = null;
            if (format == PdfImageFormat.Tiff) {
                ImageBinary imageBinary = new ImageBinary(sourceImage, threshold);
                imageBinary.setIsDithering(ditherCheck.isSelected());
                BufferedImage newImage = imageBinary.operate();
                newImage = ImageBinary.byteBinary(newImage);
                newObject = CCITTFactory.createFromImage(doc, newImage);

            } else if (format == PdfImageFormat.Jpeg) {
                newObject = JPEGFactory.createFromImage(doc, sourceImage, jpegQuality / 100f);
            }
            if (newObject != null) {
                pdResources.put(cosName, newObject);
                count++;
            }
            if (isPreview) {
                break;
            }
        }
        if (copyAllCheck.isSelected()) {
            targetDoc.getPage(currentParameters.currentPage - 1).setResources(pdResources);
        } else {
            targetDoc.addPage(sourcePage);
        }
    } catch (Exception e) {
        logger.error(e.toString());
    }
    return count;

}