Dear Support,
I’m looking for an example of how to convert a multi-page TIFF image into a multi-page PDF file using the latest Aspose.PDF version 24.6. Previously, I used version 20.12, and my code in Java was as follows:
import com.aspose.pdf.*;
import com.aspose.pdf.internal.imaging.Image;
import com.aspose.pdf.internal.imaging.fileformats.tiff.TiffFrame;
import com.aspose.pdf.internal.imaging.fileformats.tiff.TiffImage;
import com.aspose.pdf.internal.imaging.imageoptions.BmpOptions;
private void processSingleTiffDocument(Task task, List<String> docNames) throws NnpMigrationDocToolsException {
if (docNames.size() != 1) {
LOGGER.error("Document '{}': Expected exactly one document name for TYPE 2 processing, found: {}!", this.documentLogName, docNames.size());
throw new IllegalArgumentException("Expected exactly one document name for TYPE 2 processing, found: " + docNames.size());
}
Document pdfDocument = new Document();
try {
Path path = Paths.get(task.getDocDir().toString(), docNames.get(0));
this.addTiffToPdfDocument(pdfDocument, path);
this.savePdfDocument(pdfDocument, task, task.getDocDir().toPath());
} finally {
pdfDocument.close();
}
}
private void addTiffToPdfDocument(Document pdfDocument, Path tiffPath) throws NnpMigrationDocToolsException {
try (TiffImage tiffImage = (TiffImage) Image.load(tiffPath.toString())) {
for (TiffFrame tiffFrame : tiffImage.getFrames()) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
tiffFrame.save(outputStream, new BmpOptions());
outputStream.flush();
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray())) {
Page page = pdfDocument.getPages().add();
page.setPageSize(tiffFrame.getWidth(), tiffFrame.getHeight());
ImageStamp imageStamp = new ImageStamp(inputStream);
page.addStamp(imageStamp);
}
}
}
} catch (Exception e) {
LOGGER.error("Document '{}': Error adding TIFF to PDF document! {}", this.documentLogName, e.getMessage());
throw new MigrationException("Error adding TIFF to PDF document!", e);
}
}
Unfortunately, this method seems incompatible with the new API. Could you please assist me with this?
Thank you in advance!
@grigonaz
Is it possible if you could please share your sample TIFF for our reference in .zip format? We will use it to test the case and prepare a code sample for you to achieve the requirements. Also, please share if you have Aspose.Total license or only Aspose.PDF?
@asad.ali
Thank you for the prompt response!
Of course, here is multi-page TIFF sample in .zip format:
tiff_sample.zip (682.6 KB)
Currently, we are exclusively using Aspose.PDF, and our company has a license for version 20.12. I’m testing the latest API (trial version with watermark) to see if it meets our requirements before considering the purchase of a new license
@grigonaz
We tested and found that the code was creating a PDF including only the first page of the input TIF. Therefore, a task as PDFJAVA-44161 has been generated in our issue tracking system to investigate and fix this behavior of the API. We will investigate and definitely come up with a solution to share with you. Please be patient and spare us some time.
We are sorry for the inconvenience.
@grigonaz
The issue has been fixed, so multipage tiff is added in pdf with all frames.
Code 1 recommendation: Use aspose internal functionality to determine image width/height instead of using javax.imageio.ImageIO.
Code 2 enhancement: Saving TiffFrames in TiffCcittFax3/4 formats now preserves the 1bpp format (if present) for smaller PDFs and lower memory usage.
Use the following updated snippets:
Code 1:
Document doc = new Document();
// Add a page to the pages collection of the document
Page page = doc.getPages().add();
// Load the source image file into a Stream object
FileInputStream fs = new FileInputStream(dataDir + "file_000001.tif");
// Set margins so the image will fit, etc.
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);
Image image1 = new Image();
// Set the image file stream
image1.setImageStream(fs);
// Get the size of the image and set the page size accordingly
Rectangle r = image1.getBitmapSize();
page.getPageInfo().setHeight(r.getHeight());
page.getPageInfo().setWidth(r.getWidth());
// Add the image into the paragraphs collection of the page
page.getParagraphs().add(image1);
// Save the document
doc.save(dataDir + "output_24_7__code1_use_getBitmapSize.pdf");
Code 2:
Document pdfDocument = new Document();
TiffImage tiffImage = (TiffImage) com.aspose.imaging.Image.load(dataDir + "file_000001.tif");
for (TiffFrame tiffFrame : tiffImage.getFrames()) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
if (tiffFrame.getFrameOptions().getBitsPerPixel() == 1) {
tiffFrame.save(outputStream, new TiffOptions(TiffExpectedFormat.TiffCcittFax3));
// or
// tiffFrame.save(outputStream, new TiffOptions(TiffExpectedFormat.TiffCcittFax4));
}
outputStream.flush();
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray())) {
Page page = pdfDocument.getPages().add();
page.setPageSize(tiffFrame.getWidth(), tiffFrame.getHeight());
ImageStamp imageStamp = new ImageStamp(inputStream);
page.addStamp(imageStamp);
}
}
}
pdfDocument.save(dataDir + "output" + version + "_code2_TiffCcittFax3_.pdf");
output_24_7__code1_use_getBitmapSize.pdf (849.1 KB)
output_24_7__code2_TiffCcittFax3.pdf (994.8 KB)
1 Like