JPG to PDF generates large size document

Hello

converting an image file (jpg) to PDF results in a large document. Is there any way to make this conversion without increase in size. For comparison please find attached the same image convert with imagemagik.

attached files:

input.jpg - test image (930KB)
result-aspose.pdf - Aspose generated PDF file (1,979KB)
result-imagemagick.pdf - pdf document generated with imagemagik (845KB)

Source code:

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.aspose.pdf.Image;
import com.aspose.pdf.MarginInfo;
import com.aspose.pdf.Page;

public class JpgConversionTest {
public static void main(String[] args) throws FileNotFoundException {
com.aspose.pdf.Document doc = new com.aspose.pdf.Document();

MarginInfo marginInfo = new MarginInfo();
marginInfo.setBottom(5);
marginInfo.setTop(5);
marginInfo.setLeft(5);
marginInfo.setRight(5);

FileInputStream inputStream = new FileInputStream(“input.jpg”);

Page page = doc.getPages().add();
page.getPageInfo().setMargin(marginInfo);
Image image = new Image();
image.setImageStream(inputStream);
page.getParagraphs().add(image);

doc.save(“result-aspose.pdf”);

}
}

imagemagik command line:

convert input.jpg input-imagemagick.pdf

Hi Dennis,


Thanks for contacting support.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}

I have tested the scenario and have managed to reproduce the problem that JPG to PDF generates large size PDF document. For the sake of correction, I have logged it as PDFJAVA-36564 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction.


Please be patient and spare us little time. We are sorry for this inconvenience.


Best Regards,



Hi Dennis,

In addition to above reply, please note you can optimize the PDF while converting Image to PDF as following. It will help you to reduce the file size.

com.aspose.pdf.Document doc = new com.aspose.pdf.Document();

MarginInfo marginInfo = new MarginInfo();

marginInfo.setBottom(5);

marginInfo.setTop(5);

marginInfo.setLeft(5);

marginInfo.setRight(5);

FileInputStream inputStream = new FileInputStream(“input.jpg”);

Page page = doc.getPages().add();

page.getPageInfo().setMargin(marginInfo);

Image image = new Image();

image.setImageStream(inputStream);

page.getParagraphs().add(image);

ByteArrayOutputStream out=new ByteArrayOutputStream();

doc.save(out);

doc = new Document(new ByteArrayInputStream(out.toByteArray()));

OptimizationOptions optimizeOptions = new OptimizationOptions();

optimizeOptions.setRemoveUnusedObjects(true);

optimizeOptions.setLinkDuplcateStreams(true);

optimizeOptions.setRemoveUnusedStreams(true);

optimizeOptions.setCompressImages(true);

optimizeOptions.setImageQuality(60);

optimizeOptions.setResolution(100);

doc.optimizeResources(optimizeOptions);

doc.save(“result - aspose.pdf”);

Best Regards,

Thank you,

it’s a bit confusing why we cannot apply PDF optimizations directly while we are creating the document, but instead we have to save the document to a temporary byte array.

With suggested parameters we reached resulting document to have 353KB (original image size 930KB)

Denis

Hi Denis,

Thanks for your feedback. Please note PDF document is created dynamically by API and in order to get real PDF document it needs to render its programming/dynamic model. So we can render PDF document model by calling save() or processParagraphs() method of Document class. So if you do not want to save document then you use processParagraphs() to render PDF document structure as following.

com.aspose.pdf.Document doc = new com.aspose.pdf.Document();

MarginInfo marginInfo = new MarginInfo();

marginInfo.setBottom(5);

marginInfo.setTop(5);

marginInfo.setLeft(5);

marginInfo.setRight(5);

FileInputStream inputStream = new FileInputStream("input.jpg");

Page page = doc.getPages().add();

page.getPageInfo().setMargin(marginInfo);

Image image = new Image();

image.setImageStream(inputStream);

page.getParagraphs().add(image);

doc.processParagraphs();

//ByteArrayOutputStream out=new ByteArrayOutputStream();

//doc.save(out);

//doc= new Document(new ByteArrayInputStream(out.toByteArray()));

OptimizationOptions optimizeOptions = new OptimizationOptions();

optimizeOptions.setRemoveUnusedObjects(true);

optimizeOptions.setLinkDuplcateStreams(true);

optimizeOptions.setRemoveUnusedStreams(true);

optimizeOptions.setCompressImages(true);

optimizeOptions.setImageQuality(60);

optimizeOptions.setResolution(100);

doc.optimizeResources(optimizeOptions);

doc.save("result-aspose.pdf");

Best Regards,

@denis.boghiu,

Thanks for your patience.

We are pleased to share that the issue reported earlier is resolved in latest release of Aspose.Pdf for Java 17.9.

Please try using the latest release version and in case you face any issue, please feel free to contact.