API to take buffered image as input and convert to PDF

Hi,

Is there any API in Aspose.PDF, which takes buffered image as input and convert to PDF?
As of now , I have a buffered image, I write the same to a PNG and then convert that PNG to PDF.
I want to avoid this PNG step.

//pageImg is a buffered image object. Write the same to baos in PNG format.
ImageIO.write(pageImg,“PNG”,baos);
Convert baos to PDF using Aspose.PDF.

           page1.getResources().getImages().add(pageImg); //This is producing a blank page

Instead can I directly use pageImg and convert to PDF ?

I read about XImage but saw few posts that it does not work.

@sushma1509

Sadly, Aspose.PDF does not provide PngLoadOptions as an input option to create PDF directly out of it. You need to add the image inside PDF document. However, if you have an SVG image, you can use SvgLoadOptions with the image stream to load a Document and save it as PDF directly.

Thanks @asad.ali

Is there a plan to add this feature? Like PngLoad or BmpLoad?

With this additional conversion there’s a significant time involved in getting the resultant PDF.

Also, can you please respond to below queries?

  1. Even in Aspose Imaging, there is no API that takes BufferedImage directly right? It takes an input
    stream or RasterImage ?

    PngImage pngImage = new PngImage(is); //Takes only InputStream or File or RasterImage.
    PdfOptions exportOptions = new PdfOptions();
    exportOptions.setPdfDocumentInfo(new PdfDocumentInfo());
    pngImage.save(“C:/temp/converted.pdf”,exportOptions);

  2. Is there any API which takes in GraphicsObject and gives PDF ? (Across any Aspose packages)
    I know in AsposeWords there is a renderToScale or renderToSize API , but this works only when the document already has content. Like write from doc to Graphics.
    What we want is write from Graphics2d to PDF

We have a third party library which does that. We are trying to replace the same with Aspose. So, is there a way to convert graphics to PDF directly?

My concern is about the additional time and memory it takes in stream conversion to PNG and then to PDF. We may face OutOfMemory exceptions with huge documents.

@sushma1509

There has not been any planning in order to add some feature like this. However, we can investigate and look into it whether it can be added or not. We will be logging a ticket for it and sharing the ID here shortly.

@sushma1509

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-43593

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thanks @asad.ali

Also, could you please answer the first query ?

  1. Even in Aspose Imaging, there is no API that takes BufferedImage directly right? It takes an input
    stream or RasterImage ?PngImage pngImage = new PngImage(is); //Takes only InputStream or File or RasterImage.
    PdfOptions exportOptions = new PdfOptions();
    exportOptions.setPdfDocumentInfo(new PdfDocumentInfo());
    pngImage.save(“C:/temp/converted.pdf”,exportOptions);

Hi @asad.ali
As an alternative I am exploring Aspose words to set buffered image as below
Document doc = new Document()
DocumentBuilder docBuilder = new DocumentBuilder(doc);
Shape shape = docBuilder .insertImage(bufferedImage);
doc.save(“C:/temp/sample.pdf”

However, I am not able to change the orientation,papersize or set margin. I tried below:

docBuilder.getPageSetup().setPaperSize(PaperSize.A4);
docBuilder.getPageSetup().setOrientation(Orientation.POTRAIT)
docBuilder.getPageSetup()setRightMargin(0.0);
docBuilder.getPageSetup()setLeftMargin(0.0);
docBuilder.getPageSetup()setTopMargin(0.0);
docBuilder.getPageSetup()setBottomMargin(0.0);

Any input on why the orientation is not changing and also margins?
Please use same input document for trial.

@sushma1509

This forum category is dedicated for Aspose.PDF related inquiries. We request you please create your posts in respective forum categories i.e.