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

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory. 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: PDFCreator.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private static void createPDF(List<InputStream> inputImages, Path output) throws IOException {
	PDDocument document = new PDDocument();
	try {
		for (InputStream is : inputImages) {
			BufferedImage bimg = ImageIO.read(is);
			float width = bimg.getWidth();
			float height = bimg.getHeight();
			PDPage page = new PDPage(new PDRectangle(width, height));
			document.addPage(page);

			PDImageXObject img = LosslessFactory.createFromImage(document, bimg);
			try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
				contentStream.drawImage(img, 0, 0);
			}
		}
		document.save(output.toFile());
	} finally {
		document.close();
	}
}
 
Example #2
Source File: CompareResultImpl.java    From pdfcompare with Apache License 2.0 5 votes vote down vote up
protected void addPageToDocument(final PDDocument document, final ImageWithDimension image) throws IOException {
    PDPage page = new PDPage(new PDRectangle(image.width, image.height));
    document.addPage(page);
    final PDImageXObject imageXObject = LosslessFactory.createFromImage(document, image.bufferedImage);
    try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
        contentStream.drawImage(imageXObject, 0, 0, image.width, image.height);
    }
}
 
Example #3
Source File: CompatibilityHelper.java    From pdfbox-layout with MIT License 5 votes vote down vote up
private static synchronized PDImageXObject getCachedImage(
    final PDDocument document, final BufferedImage image)
    throws IOException {
Map<BufferedImage, PDImageXObject> imageCache = getImageCache(document);
PDImageXObject pdxObjectImage = imageCache.get(image);
if (pdxObjectImage == null) {
    pdxObjectImage = LosslessFactory.createFromImage(document, image);
    imageCache.put(image, pdxObjectImage);
}
return pdxObjectImage;
   }
 
Example #4
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public void createSignatureImage(PDDocument template, BufferedImage image) throws IOException
{
    pdfStructure.setImage(LosslessFactory.createFromImage(template, image));
    LOG.info("Visible Signature Image has been created");
}
 
Example #5
Source File: ReportImage.java    From cat-boot with Apache License 2.0 3 votes vote down vote up
/**
 * <p>Call this method to print images. <b>Make sure that the streams are closed before calling this method </b></p>
 * <p>Normal print method doesn't work since: http://stackoverflow.com/questions/9326245/how-to-exactly-position-an-image-inside-an-existing-pdf-page-using-pdfbox</p>
 *
 * @param document   the pdDocument.
 * @param pageNumber page of image
 * @param x          location of image
 * @param y          location of image
 * @throws java.io.IOException in case there are problems at reading or writing the image
 */
public void printImage(PDDocument document, int pageNumber, float x, float y) throws IOException {
    PDImageXObject obj = LosslessFactory.createFromImage(document, img);

    PDPageContentStream currentStream = new PDPageContentStream(document,
            document.getDocumentCatalog().getPages().get(pageNumber), PDPageContentStream.AppendMode.APPEND, false);

    currentStream.drawImage(obj, x, y - height, width, height);
    currentStream.close();
}