If the scanner output is a buffered image, and these are of type bitonal
(very important!), then save them as TIF files. You can do this with
java ImageIO, or (better) with ImageIOUtil.writeImage(). You will need
the jai_imageio jar. Then load the with PDCcitt.
JBIG2 encoding isn't supported (only decoding). The format is somewhat
risky anyway. (google for Xerox jbig2)
Here's an excerpt or our test code:
String tiffG3Path =
"src/test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/ccittg3.tif";
String tiffG4Path =
"src/test/resources/org/apache/pdfbox/pdmodel/graphics/xobject/ccittg4.tif";
PDDocument document = new PDDocument();
RandomAccess reader = new RandomAccessFile(new
File(tiffG3Path), "r");
PDXObjectImage ximage3 = new PDCcitt(document, reader);
validate(ximage3, 1, 344, 287, "tiff", PDDeviceGray.NAME);
BufferedImage bim3 = ImageIO.read(new File(tiffG3Path));
checkIdent(bim3, ximage3.getRGBImage());
PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
PDPageContentStream contentStream = new
PDPageContentStream(document, page, true, false);
contentStream.drawXObject(ximage3, 0, 0, ximage3.getWidth(),
ximage3.getHeight());
contentStream.close();
reader = new RandomAccessFile(new File(tiffG4Path), "r");
PDXObjectImage ximage4 = new PDCcitt(document, reader);
validate(ximage4, 1, 344, 287, "tiff", PDDeviceGray.NAME);
BufferedImage bim4 = ImageIO.read(new File(tiffG3Path));
checkIdent(bim4, ximage4.getRGBImage());
page = new PDPage(PDPage.PAGE_SIZE_A4);
document.addPage(page);
contentStream = new PDPageContentStream(document, page, true,
false);
contentStream.drawXObject(ximage4, 0, 0, ximage4.getWidth(),
ximage4.getHeight());
contentStream.close();
document.save(testResultsDir + "/singletiff.pdf");
document.close();
document = PDDocument.loadNonSeq(new File(testResultsDir,
"singletiff.pdf"), null);
List pages = document.getDocumentCatalog().getAllPages();
assertEquals(2, pages.size());
document.close();
Tilman
Am 12.04.2015 um 17:29 schrieb Dario Novakovic:
Hi all,
I want to create PDf from scanned documents. I receive scanner output as
BufferedImage and I am able to produce multipage PDF by adding JPEG images.
Most of my scanned images are text, black and white. JPEG compression is good,
but final PDF size could be smaller.
Now I am interested, is there any way to apply CCIT or JBIG2 compression for
bitonal images? As I can see, there aren't too many options available when it
comes to codecs in java. Has anybody got any experience or recommendation how
to apply bitonal compression to BufferedImage?
I think right way to do this would be to supply encoded stream instead of "???":
PDXObjectImage ximage = new PDCcitt(new PDStream(doc, ???));
I wasn't able to find single example of bitonal image encoding in Java, has
anybody else got any advices, links, anything?
Thanks!
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]