Hi,
Just recently upgraded to aspose-pdf 21.1 which deprecated our code from a much older version. I am trying to figure out how to convert an image to a pdf in a similar manner as we used to. I have something “working” but the resulting pdf image is distorted badly. Do you have a code snippet or file in the samples I could use as a guide? I have looked at the samples, but not finding exactly what i need.
Here is the bad implementation I have right now:
public class ConvertImageToPdf extends ConvertToPdf {
private static final Logger LOGGER = LoggerFactory.getLogger(ConvertImageToPdf.class);
public ConvertImageToPdf() {
initAsposeLicense(com.aspose.pdf.License.class);
}
// old
//@Override
//public ByteArrayOutputStream convert(InputStream inStream) throws Exception {
// Pdf imagePdf = new Pdf();
// Create a section in the Pdf object
// Section section = imagePdf.getSections().add();
// Create an image object in the section
//aspose.pdf.Image image = new aspose.pdf.Image(section);
// Add image object into the Paragraphs collection of the section
//section.getParagraphs().add(image);
// Set the path of image file
//image.getImageInfo().setImageStream(inStream);
//image.setImageScale(100f);
// Save the Pdf
//ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
//imagePdf.save(dstStream);
//return dstStream;
//}
//
// New approach attempt
//
public ByteArrayOutputStream convert(InputStream inStream) throws Exception {
// Instantiate Document Object
Document doc = new Document();
// Add a page to pages collection of document
Page page = doc.getPages().add();
// 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);
//page.setCropBox(new Rectangle(0, 0, 400, 400));
// Create an image object
Image image1 = new Image();
// Add the image into paragraphs collection of the section
page.getParagraphs().add(image1);
// Set the image file stream
image1.setImageStream(inStream);
image1.setImageScale(100f);
// Save the Pdf
ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
doc.convert(new PdfFormatConversionOptions(PdfFormat.v_1_7));
doc.save(dstStream);
//doc.convert(new PdfFormatConversionOptions(PdfFormat.v_1_7));
return dstStream;
}
}
thanks,
Mike