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

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#

  1. Initialize a new Document class object.
  2. Load input BMP image.
  3. 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 pdfDocument = new Document())
{
    pdfDocument.Pages.Add();
    Aspose.Pdf.Image image = new Aspose.Pdf.Image();

    // Load sample BMP image file
    image.File = dataDir + "Sample.bmp";
    pdfDocument.Pages[1].Paragraphs.Add(image);

    // Save output PDF document
    pdfDocument.Save(dataDir + "BMPtoPDF.pdf");
}

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#

  1. Create an instance of CgmLoadOptions class.
  2. Create an instance of Document class with mention source filename and options.
  3. Save the document with the desired file name.
public static void ConvertCGMtoPDF()
{
    CgmLoadOptions option = new CgmLoadOptions();
    Document pdfDocument= new Document(_dataDir+"corvette.cgm", option);
    pdfDocument.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#

  1. Create an object of the Image class.
  2. Add the image to a page’s Paragraphs collection.
  3. Specify the FileType property.
  4. 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 pdfDocument = new Document();

    // Add a page to pages collection of document
    Page page = pdfDocument.Pages.Add();

    Image image = new Image
    {
        FileType = ImageFileType.Dicom,
        File = System.IO.Path.Combine(_dataDir,"bmode.dcm")
    };
    pdfDocument.Pages[1].Paragraphs.Add(image);
    // Save output as PDF format
    pdfDocument.Save(System.IO.Path.Combine(_dataDir,"PDFWithDicomImage_out.pdf"));
}

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#

  1. Firstly, initialize Document class object.
  2. Load EMF image file.
  3. Add the loaded EMF image to a Page.
  4. 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 doc = new Document();

// Spcify path of input EMF image file
var imageFile = dataDir + "drawing.emf";
var page = doc.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
doc.Save(dataDir + "EMFtoPDF.pdf");

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#

  1. Initialize a new Document class object.
  2. Load input GIF image.
  3. 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 pdfDocument = new Document())
{
    pdfDocument.Pages.Add();
    Aspose.Pdf.Image image = new Aspose.Pdf.Image();

    // Load sample GIF image file
    image.File = dataDir + "Sample.gif";
    pdfDocument.Pages[1].Paragraphs.Add(image);

    // Save output PDF document
    pdfDocument.Save(dataDir + "GIFtoPDF.pdf");
}

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#

  1. Initialize object of Document class.
  2. Add a new Page to PDF document.
  3. Load JPG image and add to paragraph.
  4. 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 doc = new Document();

// Add empty page in empty document
Page page = doc.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
doc.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:

  1. Load input image file
  2. Get the height and width of the image
  3. Set height, width, and margins of a page
  4. 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 doc = new Document();

// Add an empty page
Page page = doc.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
doc.Save(dataDir + "ImagetoPDF_HeightWidth.pdf");

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#

  1. Load input PNG image.
  2. Read height and width values.
  3. Create new Document object and add Page.
  4. Set page dimensions.
  5. 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 doc = new Document();
Page page = doc.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
doc.Save(dataDir + "ImagetoPDF.pdf");

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.

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#

  1. Create an instance of SvgLoadOptions class.
  2. Create an instance of Document class with mention source filename and options.
  3. 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 pdfDocument= new Document(_dataDir + "car.svg", option);
    pdfDocument.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 ScgLoadOption 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

< circle id="r2" cx="10" cy="10" r="10" stroke="blue" stroke-width="2"> 

defs

<defs> 
<rect id="r1" width="15" height="15" stroke="blue" stroke-width="2" /> 
<circle id="r2" cx="10" cy="10" r="10" stroke="blue" stroke-width="2"/> 
<circle id="r3" cx="10" cy="10" r="10" stroke="blue" stroke-width="3"/> 
</defs> 
<use x="25" y="40" xlink:href="#r1" fill="red"/> 
<use x="35" y="15" xlink:href="#r2" fill="green"/> 
<use x="58" y="50" xlink:href="#r3" fill="blue"/>

tref

<defs> 
    <text id="ReferencedText"> 
      Referenced character data 
    </text> 
</defs> 
<text x="10" y="100" font-size="15" fill="red" > 
    <tref xlink:href="#ReferencedText"/> 
</text>

use

<defs> 
    <text id="Text" x="400" y="200" 
          font-family="Verdana" font-size="100" text-anchor="middle" > 
      Masked text 
    </text> 
<use xlink:href="#Text" fill="blue"  />

ellipse 

<ellipse cx="2.5" cy="1.5" rx="2" ry="1" fill="red" />

<g fill="none" stroke="dimgray" stroke-width="1.5" > 
                <line x1="-7" y1="-7" x2="-3" y2="-3"/> 
                <line x1="7" y1="7" x2="3" y2="3"/> 
                <line x1="-7" y1="7" x2="-3" y2="3"/> 
                <line x1="7" y1="-7" x2="3" y2="-3"/> 
</g> 

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" > 
    Sample text 
</text>

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#

  1. Create new Document class object and add Page.
  2. Load input TIFF image.
  3. Save PDF document.
Initialize empty PDF document
using (Document pdfDocument = new Document())
{
    pdfDocument.Pages.Add();
    Aspose.Pdf.Image image = new Aspose.Pdf.Image();

    // Load sample Tiff image file
    image.File = dataDir + "sample.tiff";
    pdfDocument.Pages[1].Paragraphs.Add(image);

    // Save output PDF document
    pdfDocument.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:

  1. Instantiate an instance of Document class
  2. Load input TIFF image
  3. Get FrameDimension of the frames
  4. Add new page for each frame
  5. 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");
}

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

Format: CGM

Format: DICOM

Format: EMF