Reputation: 33
I have some .jpg files that I'm using to create a pdf. I have been searching for hours upon hours now, without much luck in find any way of how to linearize the pdf! I'm having a hard time find any documentation or guides on how to do it, and am now hoping of getting some help here now. I have also looked into the pdfbox 2.0 API documentation, but could not find anything useful. I have used pdfbox on ocassion before, but mainly for splitting or merging pdf's. Here is what I have written so far:
private static void createPdf()
{
PDDocument doc = new PDDocument();
try
{
File images = new File("images/");
for (File image : images.listFiles())
{
PDPage page = new PDPage();
doc.addPage(page);
BufferedImage awtImage = ImageIO.read(image);
PDImageXObject pdImageXObject = LosslessFactory.createFromImage(doc, awtImage);
PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true);
contentStream.drawImage(pdImageXObject, 0, 0, (float) (awtImage.getWidth() / 5.4), (float) (awtImage.getHeight() / 5.9));
contentStream.close();
}
doc.setVersion(1.6f);
doc.save("pdf/images_v1.6.pdf");
}
catch (Exception io)
{
System.err.println(" -- fail --" + io);
}
finally
{
try
{
doc.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Any suggestions are greatly appreciated!
EDIT with solution: I've tried the qpdf as suggest by Tilman Hausherr and it solved my issue. Here's an example:
file 400dpi_90.pdf
qpdf --show-linearization 400dpi_90.pdf
qpdf --linearize --min-version=1.6 400dpi_90.pdf 400dpi_90-out.pdf
file 400dpi_90-out.pdf
qpdf --check-linearization 400dpi_90-out.pdf
The result of above is shown below:
400dpi_90.pdf: PDF document, version 1.4
400dpi_90.pdf is not linearized
400dpi_90-out.pdf: PDF document, version 1.6
400dpi_90-out.pdf: no linearization errors
Upvotes: 3
Views: 4068
Reputation: 59
Unfortunately, as I know, there is no open source library supporting generating a linearized PDF file.
The reason maybe: "Supporting linearized PDF will cost much work but it's useless most of the time now."
As we know, the network speed of the time (1996) at which the linearized PDF was designed, was very low. So the linearized PDF was necessary for a quick view of the first page and quick jumping to an arbitrary page of a PDF when is opened on web, although the implemention of generating a linearized PDF is very complicated. But the network speed is much quiker now. Sizes of most PDF files are less than 10 MB, and downloading one PDF file may take less than one second. You see, nowadays it is not important that whether one PDF file is linearized. As whether linearized is not necessary, and implementing linearized PDF will take much work, so there is no open source library that did it.
If you have to make a linearized PDF, I think you can only use Adobe Acrobat to do it.
PS: I am developing the feature of linearized PDF generating for the company in which I work for more than 2 months. So I can say that no open source developer will do it, because it is very boring and useless most of the time. The reason that I do it is: the company pay me a salary. π_π
Upvotes: 1
Reputation: 18851
Sorry to bring bad news, but it isn't available in PDFBox and won't be, see discussion here. I suggest you postprocess your file with qpdf instead.
Upvotes: 2