Convert Image to PDF Without Using Aspose.Pdf.Generator


#1

I’m upgrading one of our applications from v11.1 to v17.4 of Aspose.Pdf. We have several methods which use Generator (e.g., Pdf, Section, Image). Can you point me to some documentation that would help me convert a method like the one below, given “Generator” has been removed between versions 11.1 and 17.4?


Thank you.

(here’s the method I referred to above, which receives an image byte[], and returns the PDF byte[])

public byte[] ConvertImageToPdf(byte[] imageFile, Aspose.Pdf.ImageFileType type)
{
var pdfFile = new MemoryStream();
Aspose.Pdf.Generator.Pdf pdfFilePdf = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section pdfSection = pdfFilePdf.Sections.Add();
Aspose.Pdf.Generator.Image objImage = new Aspose.Pdf.Generator.Image(pdfSection);
pdfSection.Paragraphs.Add(objImage);
objImage.ImageInfo.ImageStream = new MemoryStream(imageFile);
objImage.ImageInfo.ImageFileType = type;
pdfFilePdf.Save(pdfFile);
return pdfFile.ToArray();
}


#2

Hi There,


Thanks for contacting support.

The old legacy Aspose.Pdf.Generator approach has been obsoleted in the latest version of the API which is Aspose.Pdf for .NET 17.5. The new version of the API follows DOM (Document Object Model) approach and in order to work with DOM model, please refer to following links in API documentation.

- Introduction to DOM API
- Working with Aspose.Pdf
- Convert an Image to PDF

Please note that it is always recommended to use latest version of the API as it contains all fixes and enhancements. Please try using latest version of the API and in case if you face any issue, please feel free to let us know.


Best Regards,

#3

Hi, does the ‘convert an image to pdf’ link above produce a pdf that just contains an image, or does it produce a pdf with selectable text, but that looks exactly like the scanned image (including any images the scanned image might contain)?


#4

@Gleedo

Thanks for contacting support.

The inquired article is about adding an image inside PDF as content. The image added by this process, can only be retrieved as an image from PDF document. However, please share if you have some different requirements, so that we can share relevant information with you.


#5

Hi,
with the same line of code along with Generator(img to pdf) , I get a parameter not valid error while i do pdfFilePdf.Save(pdfFile);

any suggestions


#6

@PatrickCook

Could you please share your sample PDF document with us. We will test the scenario in our environment and address it accordingly.


#7

@asad.ali
hi , here is the code as PDF is never generated , i get an exception of Parameter is not valid while the save the doc

private byte[] ConvertImageToPdf(byte[] imageFile)
        {
            var pdfFile = new MemoryStream();
            Aspose.Pdf.Generator.Pdf pdfFilePdf = new Aspose.Pdf.Generator.Pdf();
            Aspose.Pdf.Generator.Section pdfSection = pdfFilePdf.Sections.Add();
            Aspose.Pdf.Generator.Image objImage = new 
 Aspose.Pdf.Generator.Image(pdfSection);
            pdfSection.Paragraphs.Add(objImage);
            objImage.ImageInfo.ImageStream = new MemoryStream(imageFile);
            pdfFilePdf.Save(pdfFile);

            return pdfFile.ToArray();
        }

#8

@asad.ali the same code works in MVC but it doesn;t work in ASP.net


#9

@PatrickCook

Thank you for contacting support.

We have addressed some concerns in another post created by you. Kindly follow up in respective thread.