Hello,
I’m trying aspose-pdf for java 17.2.0. I have the following source code to convert a png file to pdf:
byte[] imageBinary = IOUtils.toByteArray(new java.io.FileInputStream(filePath));
Document doc = getDocument(imageBinary);
doc.save(outputFile);
The getDocument api:
public static Document getDocument(byte[] imageBinary) {
Document doc = new Document();
Page page = doc.getPages().add();
Image image1 = new Image();
page.getParagraphs().add(image1);
page.setCropBox(new Rectangle(0, 0, 400, 400));
InputStream tmp = new ByteArrayInputStream(imageBinary);
image1.setImageStream(tmp);
return doc;
}
The issue happens as in the Subject. If I comment out “page.setCropBox”, it works fine. Can you please have a look at it?
Please note that the issue does not happen with aspose-pdf for java version 10.6.1.
Thank you.
Regards,
Tuyen
Hi Tuyen,
Thanks for contacting support.
Please use image1.setImageScale(0.5) for quick fix. The reason of the problem is setting the page size less than the image size. According to default behavior the image will try to placed into the next page, but all the next pages has the same size. In the result we cannot insert the image but just creating the new pages. Please use following sample code snippet, hopefully it will resolve your issue.
JAVA
String dataDir = “/Users/fahadadeel/Downloads/resources/”;
String imageName = “Sample_Png_Image_Circle.png”;
File file = new File(dataDir, imageName);
byte[] imageBinary = null;
try {
imageBinary = Files.readAllBytes(file.toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document doc = new Document();
Page page = doc.getPages().add();
Image image1 = new Image();
page.getParagraphs().add(image1);
//page.setCropBox(new Rectangle(0, 0, 400, 400));
InputStream tmp = new ByteArrayInputStream(imageBinary);
image1.setImageScale(0.5);
image1.setImageStream(tmp);
doc.save(dataDir + “outout.pdf”);
If you still face any issue, please feel free to contact us.
Best Regards,
Hi Fahad,
Thanks for your support, it worked.
If I use these 2 APIs:
image1.setHorizontalAlignment(HorizontalAlignment.Center);
image1.setVerticalAlignment(VerticalAlignment.Center);
it seems like the horizontal works but not the vertical one. Can you please take a look at it too?
Best Regards,
Tuyen
Hi Tuyen,
Thanks for your support, it worked.
Thanks for your feedback. It is good to know that suggested code worked for you.
I have tested the scenario and have managed to reproduce the problem that vertical alignment of the image is not working. For the sake of correction, I have logged it as PDFJAVA-36547 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction.
double image_height = doc.getPages().get_Item(1).getResources().getImages().get_Item(1).getHeight() / 2; // divided by 2 is as you have scaled it to 0.5
double page_height = page.getPageInfo().getHeight();
double top_margin = ((page_height - image_height) / 2)- page_margin;
MarginInfo marginInfo= new MarginInfo();
marginInfo.setTop(top_margin);
image1.setMargin(marginInfo);
Hi Fahad,
Thanks again, it worked.
Can you provide the code to convert the “Sample_Tif_Image.tif” in the attached? I can do it in old version of Aspose pdf but it’s not in the latest version. A blank page was created at my side saying that “Insufficient data for an image.” in a popup.
Best Regards,
Tuyen
Hi Tuyen,
Thanks again, it worked.
Thanks for your feedback. It is good to know that suggested code worked for you.
Can you provide the code to convert the "Sample_Tif_Image.tif" in the attached?
String imageName = "Sample_Tif_Image.tif";
byte[] bytes = null;
try {
bytes = Files.readAllBytes(file.toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
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/output_PDF.pdf");