How to convert multiple page TIFF to PDF

Hi,

I m using the code below to convert a Tiff file (attached) with multiple page into PDF. The resulting PDF contains only the first page in Tiff. Is this the right way of doing this? If not, could you please provide a code snippet explaining how to do it?

static private ByteArrayOutputStream convertTIFToPdf(byte[] inByteArray) {
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
ByteArrayInputStream srcStream = null;
try {
srcStream = new ByteArrayInputStream(inByteArray);
Pdf pdf = new Pdf();
// add a section
Section sec1 = pdf.getSections().add();
// create an image object
aspose.pdf.Image img1 = new aspose.pdf.Image(sec1);
// set image
img1.getImageInfo().setImageStream(srcStream);
// specify the image file type
img1.getImageInfo().setImageFileType(ImageFileType.Tiff);
img1.getImageInfo().setTiffFrame(-1);
//System.out.println(img1.getImageInfo());
// add image to paragraphs collection of Section object
sec1.getParagraphs().add(img1);
// specify the image height information equal to Section height
// minus Top and Bottom margin of page
img1.getImageInfo().setFixHeight(
sec1.getPageInfo().getPageHeight()
- sec1.getPageInfo().getMargin().getTop()
- sec1.getPageInfo().getMargin().getBottom());
// specify the image Width information equal to Section Width minus
// Left and Right margin of page
img1.getImageInfo().setFixWidth(
sec1.getPageInfo().getPageWidth()
- sec1.getPageInfo().getMargin().getLeft()
- sec1.getPageInfo().getMargin().getRight());
// save the pdf file
pdf.save(dstStream);
} catch (Exception e) {
e.printStackTrace();
}
return dstStream;
}

Thanks

Hi Ivona,


Thanks for your inquiry. Please check following code snippet to convert TIFF image to PDF using new DOM approach. It will help you to accomplish the task.

String outFile = myDir + “ImageToPDF.pdf”;<o:p></o:p>

String inFile = myDir + “Test.tif”;<o:p></o:p>

Document doc = new Document();<o:p></o:p>

Page page = doc.getPages().add();<o:p></o:p>

java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File(inFile));<o:p></o:p>

//Create an image object<o:p></o:p>

com.aspose.pdf.Image image1 = new com.aspose.pdf.Image();<o:p></o:p>

//Add the image into paragraphs collection of the section<o:p></o:p>

page.getParagraphs().add(image1);<o:p></o:p>

//Set the ImageStream to a MemoryStream object<o:p></o:p>

image1.setImageStream(imageStream);<o:p></o:p>

//Set margins so image will fit, etc.<o:p></o:p>

page.getPageInfo().getMargin().setBottom(0);<o:p></o:p>

page.getPageInfo().getMargin().setTop(0);<o:p></o:p>

page.getPageInfo().getMargin().setLeft(0);<o:p></o:p>

page.getPageInfo().getMargin().setRight(0);<o:p></o:p>

doc.save(outFile);<o:p></o:p>

<o:p> </o:p>

<o:p>Please feel free to contact us for any further assistance.</o:p>

<o:p>
</o:p>

<o:p>Best Regards,</o:p>

Thanks Tilal for the feedback;

How can I handle the case of multiple pages? I would like each page (not frame) in the Tiff file to go in a distinct PDF page (total pages in Tiff = total page in PDF resulting).

Please find attached an example of a Tiff file with 9 pages, could you please provide a code snippet to achieve this?

Regards,

Hi Ivona,


We are sorry for the inconvenience. I am afriad the multipage TIFF to PDF conversion is throwing exception at the moment. So we have logged an investigation ticket PDFNEWJAVA-34482 in our isuse trackign system for furthe investigation and resolution. We will notify you as soon as it is resolved.

Best Regards,

Hi Ivona,


Adding more to Tilal’s comments, the code snippet shared in post 577166 can be used to convert single page TIFF or multi-page TIFF image to PDF format. However, as stated by Tilal, the API is causing an exception when using the new Document Object Model to convert multi-page TIFF image to PDF format. As soon as the problem is resolved, we will update you within this forum thread.

The error might be occurring due to structure of input TIFF image. However, if you encounter similar problem with other TIFF images, please do share the resource files.

The issues you have found earlier (filed as PDFNEWJAVA-34482) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

I still have issue with this i get the PDF with same number of pages as tiff’s but everything is blank. what am i missing?


Page page = doc.getPages().add();
//Create an image object
com.aspose.pdf.Image image1 = new com.aspose.pdf.Image();
//Add the image into paragraphs collection of the section
page.getParagraphs().add(image1);
//Set the ImageStream to a MemoryStream object
image1.setImageStream(inStream);
//Set margins so image will fit, etc.
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);
doc.save(baos);

Hi Suresh,


Thanks for contacting support.

Can you please share the input TIFF image, so that we can test the scenario in our environment. We are sorry for this inconvenience.