Best approach to convert image to pdf

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

@mlittle4444

Can you please explain the issue in the form of source image file, generated PDF and desired output PDF file. We will try to reproduce the issue and assist you further based on that.

Attaching the original .jpg image along with the resulting .pdf file. I would like the pdf to result in an image that is similar scale and is readable in the pdf. Thank you.

ComcastCampus3Cropped.jpg (72.2 KB)
ComcastCampus3Cropped.pdf (149.8 KB)

@mlittle4444

I suggest you to please visit this this documentation link for your convenience to see how to get the PDF for image using Aspose.PDF.

Thank you! Below is what I came up with after using that page as a reference if it is useful for others. It is giving me the results I was looking for.

public ByteArrayOutputStream convert(InputStream inStream) throws Exception {

    // Create document and page
    Document doc = new Document();
    com.aspose.pdf.Page page = doc.getPages().add();

    //
    // Get images original height and width
    //
    BufferedImage origImage = ImageIO.read(inStream);
    int width = origImage.getWidth();
    int height = origImage.getHeight();
    inStream.reset();


    //
    // Create image object and add to a page sized to hold the image
    //
    com.aspose.pdf.Image img = new com.aspose.pdf.Image();
    img.setFileType(com.aspose.pdf.ImageFileType.Unknown);
    img.setImageStream(inStream);
    page.getParagraphs().add(img);
    page.getPageInfo().setWidth(width);
    page.getPageInfo().setHeight(height);

    //
    // Save the Pdf to a stream and return the stream
    //
    ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
    doc.convert(new PdfFormatConversionOptions(PdfFormat.v_1_7));
    doc.save(dstStream);

    return dstStream;
}

@mlittle4444

Thank you for your valuable suggestions.

1 Like