Dear all
We have some Tiff-Documents that we cannot convert to a PDF.
The exception is the following:
Caused by: com.aspose.pdf.internal.ms.System.l5if: Page have not room to place a paragraphs
at com.aspose.pdf.ADocument.processParagraphs(Unknown Source) ~[aspose-pdf-23.4.jar:23.4]
at com.aspose.pdf.Document.processParagraphs(Unknown Source) ~[aspose-pdf-23.4.jar:23.4]
…
e2641548-9776-4c39-912d-5070b82bfac3.tiff.zip (35.4 KB)
We are not doing anything special:
private void convertTiffImage(TiffImage tiffImage, OutputStream pdfStream) {
try (Document pdf = new Document()) {
for (TiffFrame frame : tiffImage.getFrames()) {
com.aspose.pdf.Image pdfImage = tiffFrameToPdfImage(frame);
addImageToPdfPage(pdf, pdfImage, frame);
}
pdf.convert(new PdfFormatConversionOptions(PdfFormat.PDF_A_1B));
pdf.save(pdfStream);
}
}
private com.aspose.pdf.Image tiffFrameToPdfImage(TiffFrame frame) {
com.aspose.pdf.Image pdfImage = new com.aspose.pdf.Image();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
frame.save(stream, frame.getFrameOptions());
// set blackAndWhite manually, there is no automatic detection
pdfImage.setBlackWhite(frame.getBitsPerPixel() == 1);
pdfImage.setImageStream(new ByteArrayInputStream(stream.toByteArray()));
return pdfImage;
}
private Page addImageToPdfPage(Document document, com.aspose.pdf.Image image, TiffFrame frame) {
Page page = document.getPages().add();
page.getPageInfo().setWidth(frame.getWidth());
page.getPageInfo().setHeight(frame.getHeight());
page.getParagraphs().add(image);
return page;
}
@stud0r
Have you tried using Aspose.PDF for Java 23.4? We used it to test the case and were unable to notice any exception. Attached is the generated output for your kind reference. output.pdf (19.9 KB)
Hi @asad.ali
This is very strange, we are using the latest version 23.4.
Here is the complete test we are using to reproduce it:
@Test
void convertPlainFailed() throws Exception {
String path = "src/test/resources/asposeBugs/";
String file = "e2641548-9776-4c39-912d-5070b82bfac3.tiff";
InputStream inputStream = new FileInputStream(path + file);
try (Document pdf = new Document()) {
try (TiffImage image = (TiffImage) Image.load(inputStream)) {
for (TiffFrame frame : image.getFrames()) {
com.aspose.pdf.Image pdfImage = tiffFrameToPdfImage(frame);
addImageToPdfPage(pdf, pdfImage, frame);
}
}
pdf.save("src/test/resources/asposeSave/df.pdf");
}
}
private com.aspose.pdf.Image tiffFrameToPdfImage(TiffFrame frame) {
com.aspose.pdf.Image pdfImage = new com.aspose.pdf.Image();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
frame.save(stream, frame.getFrameOptions());
// set blackAndWhite manually, there is no automatic detection
pdfImage.setBlackWhite(frame.getBitsPerPixel() == 1);
pdfImage.setImageStream(new ByteArrayInputStream(stream.toByteArray()));
return pdfImage;
}
private Page addImageToPdfPage(Document document, com.aspose.pdf.Image image, TiffFrame frame) {
Page page = document.getPages().add();
page.getPageInfo().setWidth(frame.getWidth());
page.getPageInfo().setHeight(frame.getHeight());
page.getParagraphs().add(image);
return page;
}
The test is running in Java17.
Best regards
Michael
@stud0r
We have tested in the same environment using same code snippet. Are you using the latest version of the Aspose.Imaging as well? Please try on another machine if possible and let us know about the results you get.
The issue was due to a margin, which is added by default.
We have therefore set this to 0 to solve the issue:
page.setPageSize(frame.getWidth(), frame.getHeight());
// 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);
@stud0r
It is nice to know that you were able to resolve your issue. Please keep using our API and feel free to create a new topic in case you face any issues.