Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 15388 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > Quickly Convert Image to PDF with C#,VB.NET ...
Lacy

Quickly Convert Image to PDF with C#,VB.NET

 Blog author: Lacy | Posted on: 5/27/2012 | Category: C# Blogs | Views: 1870 | Status: [Member] | Points: 75 | Alert Moderator   
Ads

Image, as is known to all, can just be converted by a few formats. But PDF Document can be created from any source document or application. Of course, PDF format has a lot of top advantages for using, such as compact & small, securable, avoid people from modifying & redistributing your work, easy and quick to create when using the right software and so on. Thus, any file formats converted to PDF file can benefit a lot.Images of jpg, png, bmp, tiff etc can all be converted to PDF document by using Spire.PDF which is a PDF creation component. The Conversion task of image to PDF can be realized in a few minutes, if you install Spire.PDF in your system. Please read the following procedure to give it a try.

Procedure

Step1. Create a new project.

1.Create a new project in Visual Studio. Please choose Console Application.
2.Set the Target framework to be .NET Framework 2 or above in Properties.



Step2. Add reference.

1.Add Spire.PDF Dll as reference.
2.Add using at the top of the method. See below:
C# Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace image_to_pdf
{
    class Program
    {
        static void Main(string[] args)
        {
         .
               }
     }
}
VB.NET Code:
Imports System;
Imports System.Collections.Generic;
Imports System.Linq;
Imports System.Text
Imports Spire.Pdf;
Imports System.Drawing.Imaging;
Imports Spire.Pdf.Graphics;
Namespace image_to_pdf
        Friend Class Program
               Shared Sub Main(ByVal args() As String)
                .
                End Sub
        End Class
End Namespace  

Step3.Convert image to PDF Document

C# Code:
            PdfDocument doc = new PdfDocument();
            PdfSection section = doc.Sections.Add();
            PdfPageBase page = doc.Pages.Add();
            PdfImage image = PdfImage.FromFile(@"C:\e-iceblue\Spire.Pdf\Demos\Data\smile.jpg");
            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, 0, 0, fitWidth, fitHeight);
            doc.SaveToFile("smile.pdf");
            doc.Close();
VB.NET Code:
            Dim doc As New PdfDocument()
            Dim section As PdfSection = doc.Sections.Add()
            Dim page As PdfPageBase = doc.Pages.Add()
            Dim image As PdfImage = PdfImage.FromFile(@"C:\e-iceblue\Spire.Pdf\Demos\Data\smile.jpg")
            Dim widthFitRate As Single = image.PhysicalDimension.Width \ page.Canvas.ClientSize.Width
            Dim heightFitRate As Single = image.PhysicalDimension.Height \ page.Canvas.ClientSize.Height
            Dim fitRate As Single = Math.Max(widthFitRate, heightFitRate)
            Dim fitWidth As Single = image.PhysicalDimension.Width / fitRate
            Dim fitHeight As Single = image.PhysicalDimension.Height / fitRate
            page.Canvas.DrawImage(image, 0, 0, fitWidth, fitHeight)
            doc.SaveToFile("smile.pdf")
            doc.Close()

Preview
                        

PDF can also be converted from many other file formats, such as Word,HTML,Excel,Text,XML,RTF and so on by using Spire.PDF.




Found interesting? Add this to:


About Keating Arly

Experience:1 year(s)
Home page:http://www.e-iceblue.com
Member since:Tuesday, March 27, 2012
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 6:11:06 PM