Convert PNG to PDF

I am having trouble converting a set of PNG's to one pdf document. Right now I am using PDFSharp and the code works but it is inefficient. I want to use Aspose because I use it for other things and it is way faster but I cannot get it working. Here is the code I am currently using:

public class PdfCreator {

///

/// Creates a PDF document of a presentation’s images.

///

/// The output path.

/// The image paths.

public void CreatePdfOfImages(string outputPath, string[] imagePaths) {

//creates a new pdf document

var pdf = new PdfDocument( );

//sets the title and author for this pdf

pdf.Info.Title = “Presentation”;

pdf.Info.Author = “Me”;
const double dx = 800, dy = 600;

//loops through each img in the presentation

for (int i=0; i<imagePaths.Length; ++i) {

//adds a new page to the pdf document

var page = pdf.AddPage();

//sets the page orientation to landscape

page.Orientation = PdfSharp.PageOrientation.Landscape;

XGraphics gfx = XGraphics.FromPdfPage(page);
using (PdfSharp.Drawing.XImage image = PdfSharp.Drawing.XImage.FromFile(imagePaths[i])) {

double width = image.PixelWidth * 48 / image.HorizontalResolution;

double height = image.PixelHeight * 48 / image.HorizontalResolution;

image.Interpolate = false;
gfx.DrawImage(image, (dx - width) / 2, 36, width, height);

}

}

pdf.Save(outputPath);

}

}

Hi Andrew,

Thank you for considering Aspose.Pdf for .NET

Please see the following documentation link regarding converting an image file to a PDF file using Aspose.Pdf for .NET.

https://tutorials.aspose.com/pdf/net/programming-with-images/image-to-pdf/

If you face any issue or need any further assistance, please feel free to contact support.

Thank You & Best Regards,

I tried that method and the PDF has nothing in it. Attached is the pdf that came out of the conversion I did:

//Instantiate a Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();

        //Create a section in the Pdf object
        Section sec1 = pdf1.Sections.Add();

        //Create an image object in the section
        Image image1 = new Image(sec1);

        //Add image object into the Paragraphs collection of the section
        sec1.Paragraphs.Add(image1);

        //Set the path of image file
        image1.ImageInfo.File = "C:/Downloads/books.png";

        //Set the type of image using ImageFileType enumeration
        image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;

        //Set image title
        image1.ImageInfo.Title = "PNG image";

        //Save the Pdf
        pdf1.Save("C:\\temp\\books.pdf");

Hi Andrew,

I tested the sample code shared by you (as in the documentation) and it works fine. Please share your PNG image file with us for testing the issue. We will check it and get back to you.

Also, I would suggest you get a 30 day free temporary license to evaluate your required features of Aspose.Pdf in licensed mode. Please use the below link to acquire a temporary license (choose the “Get a Temporary License” option).

http://www.aspose.com/purchase/default.aspx

Please feel free to contact support in case you need any further assistance.

Sorry for the inconvenience

I tried putting the image in the temp folder but then it said "C:/temp/books.png is not a recognized imageformat"

attached is the png file

Got it working for that one photo. Had to put it as:

image1.ImageInfo.File = C:/books.png"

Now if I can get it working on my app we will see, I might be back on if I have troubles thanks.

Hi Andrew,


Thanks for using our products.

I have tested the scenario while using Aspose.Pdf for .NET 6.9.0 where I have placed the image file in C:\Downloads and the image seems to be properly appearing in resultant document. Can you please share which version of product you are using . We are sorry for this inconvenience.

For your reference, I have also attached the resultant PDF that I have generated. Please take a look.
Recently, I needed to convert PNG images to PDF, and I successfully managed to do so. Thought I'd share because it turned out to be quite convenient. In the end, the PDF format allows you to merge multiple images into one file, which can be handy for creating reports, presentations, or just for convenient storage. Moreover, the PDF format seems to maintain the image quality quite well during conversion, which is essential for preserving details. I found this particularly useful when dealing with high-resolution graphics or charts that needed to be included in a document.