Hello,
We are using Aspose PDF for Java version 22.8 . When we try to convert TIFF to PDF, the generated PDF is much larger than the original tiff. Could you please check? Below is the sample code and the input and output files.
InputOutput.zip (309.3 KB)
private static ByteArrayOutputStream convertImageToPdf(InputStream imageStream) {
// System.out.println("[ Method :: convertImageToPdf :: STRAT ]");
ByteArrayOutputStream outByteStream = null;
PdfSaveOptions saveOptions = null;
InputStream pdfInputStream = null;
long lambdaStartTime = System.currentTimeMillis();
try {
com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
outByteStream = new java.io.ByteArrayOutputStream();
// Add a page to pages collection of document
Page page = doc.getPages().add();
// Create an image object
Image image = new Image();
image.setInNewPage(true);
// Add the image into paragraphs collection of the section
page.getParagraphs().add(image);
// Set the image file stream
image.setImageStream(imageStream);
doc.save(outByteStream);
pdfInputStream = new ByteArrayInputStream(outByteStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("[ Method :: convertImageToPdf :: convert-Excep :: " + e.getStackTrace() + "]");
}
finally {
try {
imageStream.close();
outByteStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return outByteStream;
}
Thanks,
B
@judiciary
We tested the scenario in our environment using Aspose.PDF for Java 23.3 and optimized the PDF document after getting converted from TIFF using below code snippet. The size of output PDF was reduced to KBs:
com.aspose.pdf.optimization.OptimizationOptions optimizeOptions = new com.aspose.pdf.optimization.OptimizationOptions();
optimizeOptions.setRemoveUnusedObjects(true);
//optimizeOptions.setSubsetFonts(true);
optimizeOptions.setLinkDuplcateStreams(true);
optimizeOptions.getImageCompressionOptions().setCompressImages(true);
optimizeOptions.getImageCompressionOptions().setImageQuality(50);
optimizeOptions.getImageCompressionOptions().setResizeImages(true);
// Optimize PDF document using OptimizationOptions
document.optimizeResources(optimizeOptions);
output.pdf (148.3 KB)
Please try to optimize the PDF document after conversion and let us know in case you still face any issues.
Thank you for the quick response. The original file size was 11KB and the file you shared after optimizing was 149 kb. That is still significantly higher. We are looking fo the converted file size closer to the original tiff.
However, when weI try the same code with 23.2 version at our end, we still see the same size we were getting before making the change. With or without the optimization change, we are getting the same output. This is the new code.
private static ByteArrayOutputStream convertImageToPdf(InputStream imageStream) {
// System.out.println("[ Method :: convertImageToPdf :: STRAT ]");
ByteArrayOutputStream outByteStream = null;
PdfSaveOptions saveOptions = null;
InputStream pdfInputStream = null;
long lambdaStartTime = System.currentTimeMillis();
try {
com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
outByteStream = new java.io.ByteArrayOutputStream();
// Add a page to pages collection of document
Page page = doc.getPages().add();
// Create an image object
Image image = new Image();
image.setInNewPage(true);
// Add the image into paragraphs collection of the section
page.getParagraphs().add(image);
// Set the image file stream
image.setImageStream(imageStream);
com.aspose.pdf.optimization.OptimizationOptions optimizeOptions = new com.aspose.pdf.optimization.OptimizationOptions();
optimizeOptions.setRemoveUnusedObjects(true);
//optimizeOptions.setSubsetFonts(true);
optimizeOptions.setLinkDuplcateStreams(true);
optimizeOptions.getImageCompressionOptions().setCompressImages(true);
optimizeOptions.getImageCompressionOptions().setImageQuality(50);
optimizeOptions.getImageCompressionOptions().setResizeImages(true);
// Optimize PDF document using OptimizationOptions
doc.optimizeResources(optimizeOptions);
doc.save(outByteStream);
pdfInputStream = new ByteArrayInputStream(outByteStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("[ Method :: convertImageToPdf :: convert-Excep :: " + e.getStackTrace() + "]");
}
finally {
try {
imageStream.close();
outByteStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return outByteStream;
}
Thanks,
B
@judiciary
Do you have Aspose.Total License? Please confirm it and we will share our feedback with you further.
Yes we do. We have been using aspose licensed version for many years now. This is kind of high priority item for us. If we could expedite, that will be great.
When we set image.setBlackWhite(true), we see the file size being reduced a lot and closer to the original tiff. However, we are not sure if this is the right approach, and this may not work when the input tiff is a colored document. We are also noticing that the clarity is a little less when we set this flag which may be an acceptable solution for the business users. Please advise.
Thanks,
B
@judiciary
Below is the complete code snippet that we used in our environment to test the scenario and with which we obtained a PDF with smaller sized (that we shared before with you):
com.aspose.imaging.fileformats.tiff.TiffImage image = (com.aspose.imaging.fileformats.tiff.TiffImage)com.aspose.imaging.Image.load(new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(dataDir + "Input.tiff"))));
Document document = new Document();
for (com.aspose.imaging.fileformats.tiff.TiffFrame frame : image.getFrames()) {
com.aspose.pdf.Image pdfImage = frameToImage(frame);
addImageToPage(document, pdfImage, frame.getWidth(), frame.getHeight(), frame.getHorizontalResolution(), frame.getVerticalResolution());
}
document.processParagraphs();
// Set RemoveUsedObject option
com.aspose.pdf.optimization.OptimizationOptions optimizeOptions = new com.aspose.pdf.optimization.OptimizationOptions();
optimizeOptions.setRemoveUnusedObjects(true);
//optimizeOptions.setSubsetFonts(true);
optimizeOptions.setLinkDuplcateStreams(true);
optimizeOptions.getImageCompressionOptions().setCompressImages(true);
optimizeOptions.getImageCompressionOptions().setImageQuality(50);
optimizeOptions.getImageCompressionOptions().setResizeImages(true);
// Optimize PDF document using OptimizationOptions
document.optimizeResources(optimizeOptions);
document.save(dataDir + "output.pdf");
private com.aspose.pdf.Image frameToImage(com.aspose.imaging.fileformats.tiff.TiffFrame frame) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
com.aspose.imaging.imageoptions.TiffOptions options = new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.TiffNoCompressionRgb);
frame.save(stream, options);
com.aspose.pdf.Image image = new com.aspose.pdf.Image();
image.setImageStream(new ByteArrayInputStream(stream.toByteArray()));
return image;
}
private Page addImageToPage(Document document, com.aspose.pdf.Image image, int width, int height, double horizontalResolution, double verticalResolution) {
Page page = document.getPages().add();
width = (int) (width * (72.0 / horizontalResolution));
height = (int) (height * (72.0 / verticalResolution));
page.getPageInfo().setMargin(new MarginInfo(0,0,0,0));
page.getPageInfo().setWidth(width);
page.getPageInfo().setHeight(height);
page.getParagraphs().add(image);
return page;
}
You can change the optimization options like image quality until you get a satisfactory reduced PDF file size. In case you are still not satisfied with the output, please let us know. We will log an investigation ticket in our issue tracking system and share the ID with you.