Blog author:
Lacy | Posted on: 5/27/2012 | Category:
C# Blogs | Views: 1870 | Status:
[Member] |
Points: 75
|
Alert Moderator
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.