Print PDF with JBIG2 Images

Please offer me some libraries that will help me print PDF files containing JBIG2-encoded images. PDFRenderer , PDFBox do not help me. These libraries can print a simple PDF, but not a PDF containing JBIG2 images. PDFRenderer trying to fix it (according to the error issue in the tracker by PDFRedndrer), but some pages still (especially where barcodes exist) do not want to print.

PS I am using javax.print API in applet

Thanks!

UPDATE : I also tried ICEPdf, I also do not want to work.

I came to the conclusion that all these libraries (PDFRenderer, ICEPdf, PDFBox) use JPedals jbig2 decoder . Error (some pages did not print) come from this decoder library. The open source version of this decoder (which is used in PDFRenderer, ICEPdf, PDFBox) is no longer supported, but JPedal has a new commercial branch of the project, and they wrote that the error was fixed in a new commercial release, which costs $ 9 thousand.

Any ideas?

UPDATE 2 : yesterday I tried replacing the free JPedal library with another open source jbig2-imageio . But I did not get any successful results, so I created a new topic on my project page (google-code forum - here ). I would be grateful for any help.

I also found helpful discussions on the Apache PDFBox bug-tracker: here and here .

+8
java printing pdf applet jbig2
source share
5 answers

There is a fork of the JPedal library from Borisvl located in

https://github.com/Borisvl/JBIG2-Image-Decoder#readme

which contains speed improvements, and I believe that it should also fix your mistake.

EDIT: error related to simple range checking. Basically, you need to prevent GetPixel from accessing x, y values ​​outside raster extents.

You need to make sure that before calling getPixel

the following conditions are met:

col> = 0 and col <bitmap.width string> = 0 and string <bitmap.height

Here is Delphi code with a few range checks. I can’t check the Java code myself, but you need to make changes to src / org / jpedal / jbig2 / image / JBIG2Bitmap.java

 procedure TJBIG2Bitmap.combine(bitmap: TJBIG2Bitmap; x, y: Integer; combOp: Int64); ... ... var begin srcWidth := bitmap.width; srcHeight := bitmap.height; srcRow := 0; srcCol := 0; if (x < 0) then x := 0; if (y < 0) then y := 0; for row := y to Min(y + srcHeight - 1, Self.height - 1) do // <<<<<<<< HERE begin for col := x to x + srcWidth - 1 do begin srcPixel := bitmap.getPixel(srcCol, srcRow); 

Andrey.

+1
source share

As your comment in response to yms , i.e. "but which library can I use to extract images and (more importantly) return them to PDF?"

Here is a simple demo 1) Extract jbig2 or you can tell all the images from pdf .
2) Convert the jbig2 image to any other format, in my case its jpeg .
3) Create a new pdf containing jpeg .

Using jbig2-imageio and itext libraries ,

In the example below, change the resources and directory path to suit your needs.
To do this, I had to go through several resources that I will attach at the end. Hope this helps.

 import com.itextpdf.text.Document; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.parser.*; import com.levigo.jbig2.JBIG2ImageReader; import com.levigo.jbig2.JBIG2ImageReaderSpi; import com.levigo.jbig2.JBIG2ReadParam; import com.levigo.jbig2.io.DefaultInputStreamFactory; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.imageio.stream.ImageInputStream; public class JBig2Image { private String filepath; private int imageIndex; public JBig2Image() { this.filepath = "/home/blackadmin/Desktop/pdf/demo18.jbig2"; this.imageIndex = 0; extractImgFromPdf(); convertJBig2ToJpeg(); createPDF(); } private void extractImgFromPdf() { try { /////////// Extract all Images from pdf ///////////////////////// PdfReader reader = new PdfReader("/home/blackadmin/Desktop/pdf/orig.pdf"); PdfReaderContentParser parser = new PdfReaderContentParser(reader); MyImageRenderListener listener = new MyImageRenderListener("/home/blackadmin/Desktop/pdf/demo%s.%s"); for (int i = 1; i <= reader.getNumberOfPages(); i++) { parser.processContent(i, listener); } } catch (IOException ex) { System.out.println(ex); } } private void convertJBig2ToJpeg() { InputStream inputStream = null; try { ///////// Read jbig2 image //////////////////////////////////////// inputStream = new FileInputStream(new File(filepath)); DefaultInputStreamFactory disf = new DefaultInputStreamFactory(); ImageInputStream imageInputStream = disf.getInputStream(inputStream); JBIG2ImageReader imageReader = new JBIG2ImageReader(new JBIG2ImageReaderSpi()); imageReader.setInput(imageInputStream); JBIG2ReadParam param = imageReader.getDefaultReadParam(); BufferedImage bufferedImage = imageReader.read(imageIndex, param); ////////// jbig2 to jpeg /////////////////////////////////////////// ImageIO.write(bufferedImage, "jpeg", new File("/home/blackadmin/Desktop/pdf/demo18.jpeg")); } catch (IOException ex) { System.out.println(ex); } finally { try { inputStream.close(); } catch (IOException ex) { System.out.println(ex); } } } public void createPDF() { Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("/home/blackadmin/Desktop/pdf/output.pdf")); document.open(); PdfPTable table = new PdfPTable(1); //1 column. Image image = Image.getInstance("/home/blackadmin/Desktop/pdf/demo18.jpeg"); image.scaleToFit(800f, 600f); image.scaleAbsolute(800f, 600f); // Give the size of image you want to print on pdf PdfPCell nestedImgCell = new PdfPCell(image); table.addCell(nestedImgCell); document.add(table); document.close(); System.out.println( "======== PDF Created Successfully ========="); } catch (Exception e) { System.out.println(e); } } public static void main(String[] args) throws IOException { new JBig2Image(); } } class MyImageRenderListener implements RenderListener { /** * The new document to which we've added a border rectangle. */ protected String path = ""; /** * Creates a RenderListener that will look for images. */ public MyImageRenderListener(String path) { this.path = path; } /** * @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock() */ public void beginTextBlock() { } /** * @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock() */ public void endTextBlock() { } /** * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage( * com.itextpdf.text.pdf.parser.ImageRenderInfo) */ public void renderImage(ImageRenderInfo renderInfo) { try { String filename; FileOutputStream os; PdfImageObject image = renderInfo.getImage(); if (image == null) { return; } filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType()); os = new FileOutputStream(filename); os.write(image.getImageAsBytes()); os.flush(); os.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } /** * @see com.itextpdf.text.pdf.parser.RenderListener#renderText( * com.itextpdf.text.pdf.parser.TextRenderInfo) */ public void renderText(TextRenderInfo renderInfo) { } } 

References:
1) Extract jbig2 from pdf ( extract images ) ( MyImageRenderListener ).
2) Convert jbig2 ( JBIG2ImageReaderDemo )

+3
source share

How about using AcrobatReader itself? It's a little messy to make it work, not a reliable solution, I think. But, probably, all this will deduce. And be free

Some information about this route;

http://vineetreynolds.blogspot.nl/2005/12/silent-print-pdf-print-pdf.html http://www.codeproject.com/questions/98586/Programmatically-print-PDF-documents http: // forums.adobe.com/message/2336723

+1
source share

Alternatively, you can try to do this on the server side:

Approach 1:
Convert PDF files to bitmaps using an external application and print it.

Approach 2:
Correct PDF files by recompressing JBIG2 images:

1- Extracting images compressed as JBIG2 from your files.

2- Re-compile them using another algorithm (jpeg, png, etc.). To do this, you may need to go beyond Java using a JNI or calling an external application . You can try with jbig2dec or ImageMagic, for example, if the GPL lincense meets your needs. 3- Place the re-compressed images back into the PDF file.


This approach implies a loss of quality in these images, but at least you can print the files.

You can do this in Java using iText, there is a chapter on resizing images in the iText book in action (with sample code). The idea is to extract the image, resize it (including recompression) and put it back. You can use this as a starting point. Keep in mind that iText is an AGPL project, so you cannot use it for free in commercial closed source applications.

If you use a Windows-based server and can provide a commercial tool, you can also achieve this using Amyuni PDF Creator with C # / VB.Net or C ++ (the usual disclaimer is used for this sentence). You just need to go, although all objects are of type acObjectTypePicture and set the Compress to acJPegHigh attribute , this approach does not require an external JBIG2 decoder (I can include sample code here if you are interested).

If you only use the applet to print your PDF files, you can also try creating a PDF file that displays a print dialog when you open it

+1
source share

You have tools like ImageMagick that process images and convert them to a variety of formats. I used it a few years ago, so I can’t tell you if the jbig2 format is processed correctly by default, or if you need to install some kind of plugin. You can try the following so that the list of supported formats starts with J, like the JBIG2 you are looking for:

 $ convert -list format | grep -i J 

Indeed, you can convert to pdf using the tool also in combination with the gs tool aka GhostScript .

If the fact doesn’t stop you from displaying the PNG / JPEG image version and providing a download link to the JBIG2 source file with its own metadata.

+1
source share

All Articles