Convert various Images formats to PDF in .NET
Overview
This article explains how to convert various Images formats to PDF using C#. It covers these topics.
The following code snippet also work with Aspose.PDF.Drawing library.
Format: BMP
Format: CGM
Format: DICOM
Format: EMF
Format: GIF
Format: JPG
Format: PNG
Format: SVG
Format: TIFF
Format: CDR
Format: DJVU
Other topics covered by this article
C# Images to PDF Conversions
Aspose.PDF for .NET allows you to convert different formats of images to PDF files. Our library demonstrates code snippets for converting the most popular image formats, such as - BMP, CGM, DICOM, EMF, JPG, PNG, SVG and TIFF formats.
Convert BMP to PDF
Convert BMP files to PDF document using Aspose.PDF for .NET library.
BMP images are Files having extension. BMP represent Bitmap Image files that are used to store bitmap digital images. These images are independent of graphics adapter and are also called device independent bitmap (DIB) file format. You can convert BMP to PDF files with Aspose.PDF for .NET API. Therefore, you can follow the following steps to convert BMP images:
Steps: Convert BMP to PDF in C#
- Initialize a new Document class object.
- Load input BMP image.
- Finally, save the output PDF file.
So the following code snippet follows these steps and shows how to convert BMP to PDF using C#:
//Initialize empty PDF document
using (Document document = new Document())
{
document.Pages.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
// Load sample BMP image file
image.File = dataDir + "Sample.bmp";
document.Pages[1].Paragraphs.Add(image);
// Save output PDF document
document.Save(dataDir + "BMPtoPDF.pdf");
}
Try to convert BMP to PDF online
Aspose presents you online free application “BMP to PDF”, where you may try to investigate the functionality and quality it works.
Convert CGM to PDF
CGM is a file extension for a Computer Graphics Metafile format commonly used in CAD (computer-aided design) and presentation graphics applications. CGM is a vector graphics format that supports three different encoding methods: binary (best for program read speed), character-based (produces the smallest file size and allows for faster data transfers) or cleartext encoding (allows users to read and modify the file with a text editor).
Check next code snippet for converting CGM files to PDF format.
Steps: Convert CGM to PDF in C#
- Create an instance of CgmLoadOptions class.
- Create an instance of Document class with mention source filename and options.
- Save the document with the desired file name.
public static void ConvertCGMtoPDF()
{
CgmLoadOptions option = new CgmLoadOptions();
Document document= new Document(dataDir + "corvette.cgm", option);
document.Save(dataDir + "CGMtoPDF.pdf");
}
Convert DICOM to PDF
DICOM format is the medical industry standard for the creation, storage, transmission, and visualization of digital medical images and documents of examined patients.
Aspsoe.PDF for .NET allows you to convert DICOM and SVG images, but for technical reasons to add images you need to specify the type of file to be added to PDF:
Steps: Convert DICOM to PDF in C#
- Create an object of the Image class.
- Add the image to a page’s Paragraphs collection.
- Specify the FileType property.
- Specify the file’s path or source.
- If an image is at a location on the hard drive, specify the path location using the Image.File property.
- If an image is placed in a MemoryStream, pass the object holding the image to the Image.ImageStream property.
The following code snippet shows how to convert DICOM files to PDF format with Aspose.PDF. You should load DICOM image, place the image on a page in a PDF file and save the output as PDF.
private const string dataDir = "..\\..\\..\\..\\Samples\\";
// Convert DICOM images to PDF using Image class
public static void ConvertDICOMtoPDF()
{
// Instantiate Document Object
Document document = new Document();
// Add a page to pages collection of document
Page page = document.Pages.Add();
Image image = new Image
{
FileType = ImageFileType.Dicom,
File = dataDir + "bmode.dcm"
};
document.Pages[1].Paragraphs.Add(image);
// Save output as PDF format
document.Save(dataDir + "PDFWithDicomImage_out.pdf");
}
Try to convert DICOM to PDF online
Aspose presents you online free application “DICOM to PDF”, where you may try to investigate the functionality and quality it works.
Convert EMF to PDF
EMFEMF stores graphical images device-independently. Metafiles of EMF comprises of variable-length records in chronological order that can render the stored image after parsing on any output device. Furthermore, you can convert EMF to PDF image using the below steps:
Steps: Convert EMF to PDF in C#
- Firstly, initialize Document class object.
- Load EMF image file.
- Add the loaded EMF image to a Page.
- Save PDF document.
Moreover, the following code snippet shows how to convert an EMF to PDF with C# in your .NET code snippet:
// Initialize new PDF document
var document = new Document();
// Spcify path of input EMF image file
var imageFile = dataDir + "drawing.emf";
var page = document.Pages.Add();
string file = imageFile;
FileStream filestream = new FileStream(file, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(filestream);
long numBytes = new FileInfo(file).Length;
byte[] bytearray = reader.ReadBytes((int)numBytes);
Stream stream = new MemoryStream(bytearray);
var b = new Bitmap(stream);
// Specify page dimesion properties
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Width = b.Width;
page.PageInfo.Height = b.Height;
var image = new Aspose.Pdf.Image();
image.File = imageFile;
page.Paragraphs.Add(image);
//Save output PDF document
document.Save(dataDir + "EMFtoPDF.pdf");
Try to convert EMF to PDF online
Aspose presents you online free application “EMF to PDF”, where you may try to investigate the functionality and quality it works.
Convert GIF to PDF
Convert GIF files to PDF document using Aspose.PDF for .NET library.
GIF is able to store compressed data without loss of quality in a format of no more than 256 colors. The hardware-independent GIF format was developed in 1987 (GIF87a) by CompuServe for transmitting bitmap images over networks. You can convert GIF to PDF files with Aspose.PDF for .NET API. Therefore, you can follow the following steps to convert GIF images:
Steps: Convert GIF to PDF in C#
- Initialize a new Document class object.
- Load input GIF image.
- Finally, save the output PDF file.
So the following code snippet follows these steps and shows how to convert BMP to PDF using C#:
//Initialize empty PDF document
using (Document document = new Document())
{
document.Pages.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
// Load sample GIF image file
image.File = dataDir + "Sample.gif";
document.Pages[1].Paragraphs.Add(image);
// Save output PDF document
document.Save(dataDir + "GIFtoPDF.pdf");
}
Try to convert GIF to PDF online
Aspose presents you online free application “GIF to PDF”, where you may try to investigate the functionality and quality it works.
Convert JPG to PDF
No need to wonder how to convert JPG to PDF, because Apose.PDF for .NET library has best decision.
You can very easy convert a JPG images to PDF with Aspose.PDF for .NET by following steps:
Steps: Convert JPG to PDF in C#
- Initialize object of Document class.
- Add a new Page to PDF document.
- Load JPG image and add to paragraph.
- Save output PDF.
The code snippet below shows how to convert JPG Image to PDF using C#:
// Load input JPG file
String path = dataDir + "Aspose.jpg";
// Initialize new PDF document
Document document = new Document();
// Add empty page in empty document
Page page = document.Pages.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = (path);
// Add image on a page
page.Paragraphs.Add(image);
// Save output PDF file
document.Save(dataDir + "ImagetoPDF.pdf");
Then you can see how to convert an image to PDF with the same height and width of the page. We will be getting the image dimensions and accordingly set the page dimensions of PDF document with the below steps:
- Load input image file.
- Get the height and width of the image.
- Set height, width, and margins of a page.
- Save the output PDF file.
Following code snippet shows how to convert an Image to PDF with same page height and width using C#:
// Load input JPG image file
String path = dataDir + "Aspose.jpg";
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(path);
// Read Height of input image
int h = srcImage.Height;
// Read Height of input image
int w = srcImage.Width;
// Initialize a new PDF document
Document document = new Document();
// Add an empty page
Page page = document.Pages.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = (path);
// Set page dimensions and margins
page.PageInfo.Height = (h);
page.PageInfo.Width = (w);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
page.Paragraphs.Add(image);
// Save output PDF file
document.Save(dataDir + "ImagetoPDF_HeightWidth.pdf");
Try to convert JPG to PDF online
Aspose presents you online free application “JPG to PDF”, where you may try to investigate the functionality and quality it works.
Convert PNG to PDF
Aspose.PDF for .NET support feature to convert PNG images to PDF format. Check the next code snippet for realizing you task.
PNG refers to a type of raster image file format that use loseless compression, that makes it popular among its users.
You can convert PNG to PDF image using the below steps:
Steps: Convert PNG to PDF in C#
- Load input PNG image.
- Read height and width values.
- Create new Document object and add Page.
- Set page dimensions.
- Save output file.
Moreover, the code snippet below shows how to convert PNG to PDF with C# in your .NET applications:
// Load input PNG file
String path = dataDir + "Aspose.png";
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(path);
int h = srcImage.Height;
int w = srcImage.Width;
// Initialize new Document
Document document = new Document();
Page page = document.Pages.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = (path);
// Set page dimensions
page.PageInfo.Height = (h);
page.PageInfo.Width = (w);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Right = (0);
page.PageInfo.Margin.Left = (0);
page.Paragraphs.Add(image);
// Save output PDF
document.Save(dataDir + "ImagetoPDF.pdf");
Try to convert PNG to PDF online
Aspose presents you online free application “PNG to PDF”, where you may try to investigate the functionality and quality it works.
Convert SVG to PDF
Aspose.PDF for .NET explains how to convert SVG images to PDF format and how to get dimensions of the source SVG file.
Scalable Vector Graphics (SVG) is a family of specifications of an XML-based file format for two-dimensional vector graphics, both static and dynamic (interactive or animated). The SVG specification is an open standard that has been under development by the World Wide Web Consortium (W3C) since 1999.
SVG images and their behaviors are defined in XML text files. This means that they can be searched, indexed, scripted, and if required, compressed. As XML files, SVG images can be created and edited with any text editor, but it is often more convenient to create them with drawing programs such as Inkscape.
Try to convert SVG format to PDF online
Aspose.PDF for .NET presents you online free application “SVG to PDF”, where you may try to investigate the functionality and quality it works.
To convert SVG files to PDF, use the class named SvgLoadOptions which is used to initialize the LoadOptions
object. Later, this object is passed as an argument during the Document object initialization and helps the PDF rendering engine to determine the input format of the source document.
Steps: Convert SVG to PDF in C#
- Create an instance of
SvgLoadOptions
class. - Create an instance of
Document
class with mention source filename and options. - Save the document with the desired file name.
The following code snippet shows the process of converting SVG file into PDF format with Aspose.PDF for .NET.
public static void ConvertSVGtoPDF()
{
SvgLoadOptions option = new SvgLoadOptions();
Document document= new Document(dataDir + "car.svg", option);
document.Save(dataDir + "svgtest.pdf");
}
Get SVG dimensions
It is also possible to get the dimensions of the source SVG file. This information can be useful if we want the SVG to cover the entire page of the output PDF. The SvgLoadOption class’ AdjustPageSize property fulfills this requirement. The default value of this property is false. If the value is set to true, the output PDF will have the same size (dimensions) as the source SVG.
The following code snippet shows the process of getting the source SVG file’s dimensions and generating a PDF file.
public static void ConvertSVGtoPDF_Advanced()
{
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
var loadopt = new SvgLoadOptions();
loadopt.AdjustPageSize = true;
var svgDoc = new Document(dataDir + "GetSVGDimensions.svg", loadopt);
svgDoc.Pages[1].PageInfo.Margin.Top = 0;
svgDoc.Pages[1].PageInfo.Margin.Left = 0;
svgDoc.Pages[1].PageInfo.Margin.Bottom = 0;
svgDoc.Pages[1].PageInfo.Margin.Right = 0;
svgDoc.Save(dataDir + "GetSVGDimensions_out.pdf");
}
SVG Supported Features
SVG Tag |
Sample Use |
---|---|
circle |
|
defs |
<defs>
|
tref |
<defs> |
use |
<defs> |
ellipse |
<ellipse cx="2.5" cy="1.5" rx="2" ry="1" fill="red" /> |
g |
<g fill="none" stroke="dimgray" stroke-width="1.5" > |
image |
<image id="ShadedRelief" x="24" y="4" width="64" height="82" xlink:href="relief.jpg" /> |
line |
<line style="stroke:#eea;stroke-width:8" x1="10" y1="30" x2="260" y2="100"/> |
path |
<path style="fill:#daa;fill-rule:evenodd;stroke:red" d="M 230,150 C 290,30 10,255 110,140 z "/> |
style |
<path style="fill:#daa;fill-rule:evenodd;stroke:red" d="M 230,150 C 290,30 10,255 110,140 z "/> |
polygon |
<polygon style="stroke:#24a;stroke-width:1.5;fill:#eefefe" points="10,10 180,10 10,250 10,10" /> |
polyline |
<polyline fill="none" stroke="dimgray" stroke-width="1" points="-3,-6 3,-6 3,1 5,1 0,7 -5,1 -3,1 -3,-5"/> |
rect |
<rect x="0" y="0" width="400" height="600" stroke="none" fill="aliceblue" /> |
svg |
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="10cm" height="5cm" > |
text |
<text font-family="sans-serif" fill="dimgray" font-size="22px" font-weight="bold" x="58" y="30" pointer-events="none">Map Title</text> |
font |
<text x="10" y="100" font-size="15" fill="red" > |
tspan |
<tspan dy="25" x="25">six ink color input value. Here it will </tspan> |
Convert TIFF to PDF
Aspose.PDF file format supported, be it a single frame or multi-frame TIFF image. It means that you can convert the TIFF image to PDF in your .NET applications.
TIFF or TIF, Tagged Image File Format, represents raster images that are meant for usage on a variety of devices that comply with this file format standard. TIFF image can contain several frames with different images. Aspose.PDF file format is also supported, be it a single frame or multi-frame TIFF image.
You can convert TIFF to PDF in the same manner as the rest raster file formats graphics:
Steps: Convert TIFF to PDF in C#
- Create new Document class object and add Page.
- Load input TIFF image.
- Save PDF document.
Initialize empty PDF document
using (Document document = new Document())
{
document.Pages.Add();
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
// Load sample Tiff image file
image.File = dataDir + "sample.tiff";
document.Pages[1].Paragraphs.Add(image);
// Save output PDF document
document.Save(dataDir + "TIFFtoPDF.pdf");
}
In case you need to convert multi-page TIFF image to multi-page PDF document and control some params, i.g. width or aspect ratio, please follow these steps:
- Instantiate an instance of Document class.
- Load input TIFF image.
- Get FrameDimension of the frames.
- Add new page for each frame.
- Finally, save images to PDF pages.
The following code snippet shows how to convert multi-page or multi-frame TIFF image to PDF with C#:
public static void TiffToPDF2()
{
// Initalize new Document
Document pdf = new Document();
//Load TIFF image into stream
Bitmap bitmap = new Bitmap(File.OpenRead(dataDir + "multipage.tif"));
// Convert multi page or multi frame TIFF to PDF
FrameDimension dimension = new FrameDimension(bitmap.FrameDimensionsList[0]);
int frameCount = bitmap.GetFrameCount(dimension);
// Iterate through each frame
for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
{
Page page = pdf.Pages.Add();
bitmap.SelectActiveFrame(dimension, frameIdx);
MemoryStream currentImage = new MemoryStream();
bitmap.Save(currentImage, ImageFormat.Tiff);
Aspose.Pdf.Image imageht = new Aspose.Pdf.Image
{
ImageStream = currentImage,
//Apply some other options
//ImageScale = 0.5
};
page.Paragraphs.Add(imageht);
}
// Save output PDF file
pdf.Save(dataDir + "TifftoPDF.pdf");
}
Convert CDR to PDF
CDR is a file format that was developed by the Corel Corporation and is used mainly for vector graphic images and drawings. The CDR file format is recognized by the majority of image editing programs. The CDR format is the default format for Corel Draw Applications.
Check next code snippet for converting CDR files to PDF format.
Steps: Convert CDR to PDF in C#
- Create an instance of CdrLoadOptions class.
- Create an instance of Document class with mention source filename and options.
- Save the document with the desired file name.
var document = new Document("input.cdr", new CdrLoadOptions());
document.Save("out.pdf");
Convert DJVU to PDF
DjVu is a compressed image format which was developed by LizardTech. This file format was primarily designed to store different kinds of scanned documents; especially documents that contain a combination of text, pictures, indexed color images, and line drawings.
Check next code snippet for converting DJVU files to PDF format.
Steps: Convert DJVU to PDF in C#
- Create an instance of DjvuLoadOptions class.
- Create an instance of Document class with mention source filename and options.
- Save the document with the desired file name.
var document = new Document("input.djvu", new DjvuLoadOptions());
document.Save("out.pdf");
Convert HEIC to PDF
A HEIC file is a High-Efficiency Container Image file format that can store multiple images as a collection in a single file.
Convert HEIC images to PDF using Aspose.PDF:
using (var fs = new FileStream("iphone_photo.heic", FileMode.Open))
{
HeicImage image = HeicImage.Load(fs);
var pixels = image.GetByteArray(PixelFormat.Rgb24);
var width = (int)image.Width;
var height = (int)image.Height;
var document = new Document();
Page page = document.Pages.Add();
Aspose.Pdf.Image asposeImage = new Aspose.Pdf.Image();
asposeImage.BitmapInfo = new BitmapInfo(pixels, width, height, BitmapInfo.PixelFormat.Rgb24);
page.PageInfo.Height = height;
page.PageInfo.Width = width;
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Margin.Left = 0;
page.Paragraphs.Add(asposeImage);
document.Save("iphone_photo.pdf");
}
Applies to
Platform | Supported | Comments |
---|---|---|
Windows .NET Framework | 2.0-4.6 | |
Windows .NET Core | 2.0-3.1 | |
.NET 5 Windows | ||
Linux .NET Core | 2.0-3.1 | |
.NET 5 Linux |
See Also
This article also covers these topics. The codes are same as above.
Format: BMP
- C# BMP to PDF Code
- C# BMP to PDF API
- C# BMP to PDF Programmatically
- C# BMP to PDF Library
- C# Save BMP as PDF
- C# Generate PDF from BMP
- C# Create PDF from BMP
- C# BMP to PDF Converter
Format: CGM
- C# CGM to PDF Code
- C# CGM to PDF API
- C# CGM to PDF Programmatically
- C# CGM to PDF Library
- C# Save CGM as PDF
- C# Generate PDF from CGM
- C# Create PDF from CGM
- C# CGM to PDF Converter
Format: DICOM
- C# DICOM to PDF Code
- C# DICOM to PDF API
- C# DICOM to PDF Programmatically
- C# DICOM to PDF Library
- C# Save DICOM as PDF
- C# Generate PDF from DICOM
- C# Create PDF from DICOM
- C# DICOM to PDF Converter
Format: EMF
- C# EMF to PDF Code
- C# EMF to PDF API
- C# EMF to PDF Programmatically
- C# EMF to PDF Library
- C# Save EMF as PDF
- C# Generate PDF from EMF
- C# Create PDF from EMF
- C# EMF to PDF Converter
Format: DjVu
- C# DjVu to PDF Code
- C# DjVu to PDF API
- C# DjVu to PDF Programmatically
- C# DjVu to PDF Library
- C# Save DjVu as PDF
- C# Generate PDF from DjVu
- C# Create PDF from DjVu
- C# DjVu to PDF Converter
Format: CDR