Exception while converting Tiff bytes to PDF

Hi,



I am facing exception in reading Tiff file using Aspose Document object to create PDF file of it. Originally, I wanted to read Tiff bytes and write them into a PDF. Since I am getting “invalid PDF format” exception in original approach, I tried reading Tiff file which is giving me exception as below





com.aspose.pdf.internal.ms.System.z11: Specified argument was out of the range of valid values.





Code I am trying to use is





ByteArrayOutputStream baos=new ByteArrayOutputStream();


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


Page asposePage = asposeDoc.getPages().add();

asposePage.getParagraphs().add(immg);


BufferedImage bufImg = ImageIO.read(new File(“TiffFile.tif”));


ImageIO.write(bufImg, “TIF”, baos);


asposeDoc.save(baos, 40);








Exception occurred: class com.aspose.pdf.internal.ms.System.z11: Specified argument was out of the range of valid values.







Please let me know if further information needed





Thanks

Hello Edward,


Thanks for using our API’s.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000; min-height: 14.0px} span.s1 {font-kerning: none}

I will appreciate if you please share your tiff file. It will help us to understand your scenario and address it accordingly..


We are sorry for the inconvenience.


Best Regards,

Thank you for response Fahaad.


I did one change in my code as below

asposeDoc.save(baos, 40);

replace with

asposeDoc.save(baos);

Now, I am not getting exception in my console but I get error message on Adobe Reader with a pop saying "File is either not a supported file type or it is damaged"

I am attaching our sample PDF in this respose

Hello Edward,

Thanks for sharing the sample file.

I have again tested the scenario using Aspose.Pdf for Java 16.12.0 and I am unable to notice any issue when using valid license. For your reference, I have also attached the output generated over my end.

[Java]

// Load source PDF file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
com.aspose.pdf.Document asposeDoc = new com.aspose.pdf.Document();
Page asposePage = asposeDoc.getPages().add();
com.aspose.pdf.Image immg = new com.aspose.pdf.Image();
immg.setFile("c:/pdftest/SampleCorruptPDF.tif");
asposePage.getParagraphs().add(immg);
asposeDoc.save("c:/pdftest/SampleCorruptPDF.pdf");

Hello Nayyar,


Thank you for response. I made some changes according to your code, which worked for met. Let me test it with our applied license.

Thank you


Hi. After applying our license properly, I am able to read my TIFF file successfully. However, It is able to read only first page of the file. Below code is able to convert only first page to PDF. Rest of pages are truncated!


InputStream inputStrm = new ByteArrayInputStream(getBytes);
ImageInputStream imageStrm = ImageIO.createImageInputStream(inputStrm);

com.aspose.pdf.Document asposeDoc = new com.aspose.pdf.Document();
Page asposePage = asposeDoc.getPages().add();
com.aspose.pdf.Image immg = new com.aspose.pdf.Image();
immg.setInNewPage(true); // Added this line to previous code
immg.setBufferedImage(ImageIO.read(imageStrm));
asposePage.getParagraphs().add(immg);
asposeDoc.save(“c://pdftest/SampleCorruptPDF.pdf”);

Can you please guide me through that. Thanks

Hello Edward,


Thanks for using our API’s.

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}

I will appreciate if you please share your sample project folder here to test in your scenario.


InputStream inputStrm = new ByteArrayInputStream(getBytes);


In this code, Please explain what you are passing to ByteArrayInputStream.

Best Regards,

Sure. Will give project sample in next post. Meanwhile, Let me clarify about your question. “getBytes” is byte[], which we are reading from IBM content manager through Restlet webservice. This byte array is multipage TIFF file stored in IBM content manager. So my brief code for getBytes will be


byte[] getBytes = cmDocument.getBytes(); // Read byte array from cmDocument Object

InputStream inputStrm = new ByteArrayInputStream(getBytes);


// Rest of code


Thanks

Hello Edward,


Thanks for sharing the details.

At your convenience, please share the sample project as requested by Fahad, so that we can further look into this matter.

Hi,


Thank you for follow up. I tried, but sorry to say I cannot share sample project, the only reason is, we are reading tiff byte array from our internal webserivce. However, I already shared same tiff file earlier in this discussion, which we are reading it as byte array. Only difference is, instead of reading that file physically, we are trying to read that file through byte array. As I told earlier, I am successfully able to convert same file using above code, but it can create only first page in pdf file. You can use same file in your workspace and read it in bytes to try my code. That will work just like setup. Let me know if further clarification needed

Thanks

Hello Edward,


Thanks for giving further information. I have tested and manage to produce the correct output. Please check below sample code for reference. I have attached the PDF document also.

String dirName = “/Users/fahadadeel/Downloads/resources/”;

String imageName = “SampleCorruptPDF.tif”;

File file = new File(dirName, imageName);

byte[] bytes = null;

try {

bytes = Files.readAllBytes(file.toPath());

} catch (IOException e) {

e.printStackTrace();
}

InputStream inputStrm = new ByteArrayInputStream(bytes);

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

Page asposePage = asposeDoc.getPages().add();

com.aspose.pdf.Image immg = new com.aspose.pdf.Image();

immg.setInNewPage(true); // Added this line to previous code

immg.setImageStream(inputStrm);

asposePage.getParagraphs().add(immg);

asposeDoc.save("/Users/fahadadeel/Downloads/resources/SampleCorruptPDF.pdf");

If you still face any issue, please feel free to contact us.

Best Regards,

Thank you very much for response. immg.setImageStream(ImageIO.read(io)) was the main problem for my piece of code. Your solution works as expected with setInNewPage(true) method. Thanks again

Hello Edward,


p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; -webkit-text-stroke: #000000; min-height: 14.0px} span.s1 {font-kerning: none}

Thanks for your feedback. It is good to know that suggested code worked for you.


Please keep using our API and feel free to contact us for any question or concern, we will be more than happy to extend our support.


Best Regards,