Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Fri Dec 01, 2017 8:53 pm

How to convert some png images to PDF

jjgonzalezpandaid
 
Posts: 3
Joined: Tue Nov 21, 2017 7:57 pm

Mon Dec 04, 2017 2:28 am

Hello,

Thanks for your inquiry. Please refer to the below code snippet to accomplish your task.
Code: Select all
            // Create a pdf document with a section
            PdfDocument doc = new PdfDocument();
            PdfSection section = doc.Sections.Add();
            string[] images = { "sample.png", "sample1.png", "sample2.png" };
            foreach(String imageFile in images)
            {       
                //add new page
                PdfPageBase page = doc.Pages.Add();
                //Load a png image from system
                PdfImage image = PdfImage.FromFile(imageFile);
                //Set image display location and size in PDF
                float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
                float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
                float fitRate = Math.Max(widthFitRate, heightFitRate);
                float fitWidth = image.PhysicalDimension.Width / fitRate;
                float fitHeight = image.PhysicalDimension.Height / fitRate;
                page.Canvas.DrawImage(image, 10, 10, fitWidth, fitHeight);
            }
            //save the file
            doc.SaveToFile("image to pdf.pdf");
            doc.Close();


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Thu Dec 07, 2017 9:45 am

Hello,

Greeting from E-iceblue.
Did you try the code snippet I provided?
Your feedback will be greatly appreciated.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.PDF