Home > Code > C# > How to Convert JPEG, PNG, BMP, EMF, WMF & GIF Images to PDF in .NET Apps

How to Convert JPEG, PNG, BMP, EMF, WMF & GIF Images to PDF in .NET Apps

by sher azam   on Mar 09, 2016   Category: C#   |  Views: 594    |  Points: 25   |  Gold 


This technical tip explains how developers can convert an image to PDF inside their .NET Applications. This article shows how to create a PDF document from an image using Aspose.Words. While converting images to PDF is not a main feature of Aspose.Words, this example shows how easy it is to do with Aspose.Words. To make this code work you need to add references to Aspose.Words and System.Drawing to your project. The code below allows converting single frame images, such as JPEG, PNG, BMP, EMF or WMF, as well as multi-frame TIF images and GIF to PDF.

Convert single frame JPEG, PNG, BMP & Multi-frame TIF or GIF images to PDF

//[C# Code Sample]

// // For complete examples and data files, please go to https://github.com/asposewords/Aspose_Words_NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();

ConvertImageToPdf(dataDir + "Test.jpg", dataDir + "TestJpg_out_.pdf");
ConvertImageToPdf(dataDir + "Test.png", dataDir + "TestPng_out_.pdf");
ConvertImageToPdf(dataDir + "Test.wmf", dataDir + "TestWmf_out_.pdf");
ConvertImageToPdf(dataDir + "Test.tiff", dataDir + "TestTif_out_.pdf");
ConvertImageToPdf(dataDir + "Test.gif", dataDir + "TestGif_out_.pdf");

//[VB Code Sample]

' For complete examples and data files, please go to https://github.com/asposewords/Aspose_Words_NET
' The path to the documents directory.
Dim dataDir As String = RunExamples.GetDataDir_LoadingAndSaving()

ConvertImageToPdf(dataDir & Convert.ToString("Test.jpg"), dataDir & Convert.ToString("TestJpg_out_.pdf"))
ConvertImageToPdf(dataDir & Convert.ToString("Test.png"), dataDir & Convert.ToString("TestPng_out_.pdf"))
ConvertImageToPdf(dataDir & Convert.ToString("Test.wmf"), dataDir & Convert.ToString("TestWmf_out_.pdf"))
ConvertImageToPdf(dataDir & Convert.ToString("Test.tiff"), dataDir & Convert.ToString("TestTiff_out_.pdf"))
ConvertImageToPdf(dataDir & Convert.ToString("Test.gif"), dataDir & Convert.ToString("TestGif_out_.pdf"))


//Using ConvertImageToPdf method

//[C# Code Sample]


// For complete examples and data files, please go to https://github.com/asposewords/Aspose_Words_NET
// Create Aspose.Words.Document and DocumentBuilder.
// The builder makes it simple to add content to the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Read the image from file, ensure it is disposed.
using (Image image = Image.FromFile(inputFileName))
{
// Find which dimension the frames in this image represent. For example
// the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);

// Get the number of frames in the image.
int framesCount = image.GetFrameCount(dimension);

// Loop through all frames.
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
// Insert a section break before each new page, in case of a multi-frame TIFF.
if (frameIdx != 0)
builder.InsertBreak(BreakType.SectionBreakNewPage);

// Select active frame.
image.SelectActiveFrame(dimension, frameIdx);

// We want the size of the page to be the same as the size of the image.
// Convert pixels to points to size the page to the actual image size.
PageSetup ps = builder.PageSetup;
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);

// Insert the image into the document and position it at the top left corner of the page.
builder.InsertImage(
image,
RelativeHorizontalPosition.Page,
0,
RelativeVerticalPosition.Page,
0,
ps.PageWidth,
ps.PageHeight,
WrapType.None);
}
}

// Save the document to PDF.
doc.Save(outputFileName);

//[VB.NET Code Sample]


' For complete examples and data files, please go to https://github.com/asposewords/Aspose_Words_NET
' Create Aspose.Words.Document and DocumentBuilder.
' The builder makes it simple to add content to the document.
Dim doc As New Document()
Dim builder As New DocumentBuilder(doc)

' Read the image from file, ensure it is disposed.
Using image__1 As Image = Image.FromFile(inputFileName)
' Find which dimension the frames in this image represent. For example
' the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
Dim dimension As New FrameDimension(image__1.FrameDimensionsList(0))

' Get the number of frames in the image.
Dim framesCount As Integer = image__1.GetFrameCount(dimension)

' Loop through all frames.
For frameIdx As Integer = 0 To framesCount - 1
' Insert a section break before each new page, in case of a multi-frame TIFF.
If frameIdx <> 0 Then
builder.InsertBreak(BreakType.SectionBreakNewPage)
End If

' Select active frame.
image__1.SelectActiveFrame(dimension, frameIdx)

' We want the size of the page to be the same as the size of the image.
' Convert pixels to points to size the page to the actual image size.
Dim ps As PageSetup = builder.PageSetup
ps.PageWidth = ConvertUtil.PixelToPoint(image__1.Width, image__1.HorizontalResolution)
ps.PageHeight = ConvertUtil.PixelToPoint(image__1.Height, image__1.VerticalResolution)

' Insert the image into the document and position it at the top left corner of the page.
builder.InsertImage(image__1, RelativeHorizontalPosition.Page, 0, RelativeVerticalPosition.Page, 0, ps.PageWidth, _
ps.PageHeight, WrapType.None)
Next
End Using

' Save the document to PDF.
doc.Save(outputFileName)



More about Aspose.Words for .NET

Aspose.Words is a word processing component that enables Java & .NET applications to read, write and modify Word documents without using Microsoft Word. Other useful features include document creation, content and formatting manipulation, mail merge abilities, reporting features, TOC updated/rebuilt, Embedded OOXML, Footnotes rendering and support of DOCX, DOC, WordprocessingML, HTML, XHTML, TXT and PDF formats (requires Aspose.Pdf). It supports both 32-bit and 64-bit operating systems. You can even use Aspose.Words to build applications with Mono.

- Homepage of Aspose.Words for .NET: http://www.aspose.com/.net/word-component.aspx

- Download Aspose.Words for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts