Hi,
I am using aspose PDF and aspose imaging to convert a multi page Tiff files into PDF files. I am Using following approach…
- Load the Tiff file using aspose imaging
- get each frame of tiff file
- add each image frame in to a PDF
- save the pdf
The Tiff file has a transparent background. When i convert it to PDF the transparent background is converted into black color background,… i have attached sampled input and output files for reference.
Following is the code
com.aspose.pdf.Document lDocument = new com.aspose.pdf.Document();
FileInputStream pInputStream = new FileInputStream(new File(“strike.tif”));
FileOutputStream pOutputStream = new FileOutputStream(new File(“Strike.tif.pdf”));
TiffImage multiImage = (TiffImage) Image.load(pInputStream);
for (TiffFrame tiffFrame : multiImage.getFrames())
{
OutputStream lOutputStream = new ByteArrayOutputStream();
// If i save this png file on disk, the transparency is maintained.
tiffFrame.save(lOutputStream,new PngOptions());
byte[] lImageBytes = ((ByteArrayOutputStream)lOutputStream).toByteArray();
lOutputStream.close();
// Select active frame.
InputStream in = new ByteArrayInputStream(lImageBytes);
Page page = lDocument.getPages().add();
BufferedImage lBImage = ImageIO.read(in);
com.aspose.pdf.Image image1 = new com.aspose.pdf.Image();
// Set the image file stream
image1.setBufferedImage(lBImage);
// 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);
double currentImageHeight = ConvertUtil.pixelToPoint(lBImage.getHeight());
double currentImageWidth = ConvertUtil.pixelToPoint(lBImage.getWidth());
page.getPageInfo().setHeight(currentImageHeight);
page.getPageInfo().setWidth(currentImageWidth);
// Add the image into paragraphs collection of the section
page.getParagraphs().add(image1);
}
lDocument.processParagraphs();
lDocument.save(pOutputStream);