Free Support Forum - aspose.com

Multiple PNG To Single PDF

Been trying to figure this out with no luck. I am just trying to combine multiple .png files into 1 large pdf. I can get 1 image to work, but not multiple. Do I need to put all of the images into a memory stream for this to work or how can I add each image individually. Thanks for your help! Here is the code I have:

        public void ImageToPDF(List<string> pngFiles, string cdPath, string pdfName, string masterPassword, string userPassword) {
        Aspose.Pdf.<SPAN style="COLOR: #2b91af">License</SPAN> imageListLicence = <SPAN style="COLOR: blue">new</SPAN> Aspose.Pdf.<SPAN style="COLOR: #2b91af">License</SPAN>();
        imageListLicence.SetLicense(<SPAN style="COLOR: #a31515">"Aspose.Total.lic"</SPAN>);

        Aspose.Pdf.Generator.<SPAN style="COLOR: #2b91af">Pdf</SPAN> pdf1 = <SPAN style="COLOR: blue">new</SPAN> Aspose.Pdf.Generator.<SPAN style="COLOR: #2b91af">Pdf</SPAN>();

        pdf1.Security = <SPAN style="COLOR: blue">new</SPAN> Aspose.Pdf.Generator.<SPAN style="COLOR: #2b91af">Security</SPAN>();
        pdf1.Security.MasterPassword = masterPassword;
        pdf1.Security.UserPassword = userPassword;

        Aspose.Pdf.Generator.<SPAN style="COLOR: #2b91af">Section</SPAN> sec1 = pdf1.Sections.Add();

        sec1.PageInfo.Margin.Top = 10;
        sec1.PageInfo.Margin.Bottom = 10;
        sec1.PageInfo.Margin.Left = 10;
        sec1.PageInfo.Margin.Right = 10;


        Aspose.Pdf.Generator.<SPAN style="COLOR: #2b91af">Image</SPAN> image1 = <SPAN style="COLOR: blue">new</SPAN> Aspose.Pdf.Generator.<SPAN style="COLOR: #2b91af">Image</SPAN>(sec1);


        <SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> i = 0; i < pngFiles.Count; i++) {
            <SPAN style="COLOR: green">//Add image object into the Paragraphs collection of the section</SPAN>
            sec1.Paragraphs.Add(image1);

            <SPAN style="COLOR: green">//Set the path of image file</SPAN>
            image1.ImageInfo.File = pngFiles[i];

            <SPAN style="COLOR: green">//Set the type of image using ImageFileType enumeration</SPAN>
            image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.<SPAN style="COLOR: #2b91af">ImageFileType</SPAN>.Png;
        }

        pdf1.Save(cdPath + <SPAN style="COLOR: #a31515">"\\"</SPAN> + pdfName + <SPAN style="COLOR: #a31515">".pdf"</SPAN>);
   }</PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"> </PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"> </PRE>

Hi Jesse,


Thanks for using our products.

In order to resolve the issue, please instantiate new Image object while adding new image i.e. move Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1); inside for loop.

In case the problem still persists, please feel free to contact.