Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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, CDR, HEIC and TIFF formats.
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:
So the following code snippet follows these steps and shows how to convert BMP to PDF using C#:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertBMPtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
var image = new Aspose.Pdf.Image();
// Load BMP file
image.File = dataDir + "BMPtoPDF.bmp";
page.Paragraphs.Add(image);
// Save PDF document
document.Save(dataDir + "BMPtoPDF_out.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.
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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertCGMtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
var option = new Aspose.Pdf.CgmLoadOptions();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "CGMtoPDF.cgm", option))
{
// Save PDF document
document.Save(dataDir + "CGMtoPDF_out.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:
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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertDICOMtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
var image = new Aspose.Pdf.Image
{
FileType = ImageFileType.Dicom,
File = dataDir + "DICOMtoPDF.dcm"
};
page.Paragraphs.Add(image);
// Save PDF document
document.Save(dataDir + "DICOMtoPDF_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.
EMF 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:
Moreover, the following code snippet shows how to convert an EMF to PDF with C# in your .NET code snippet:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertEMFtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
var image = new Aspose.Pdf.Image();
// Load EMF file
image.File = dataDir + "EMFtoPDF.emf";
// Specify page dimension properties
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Width = image.BitmapSize.Width;
page.PageInfo.Height = image.BitmapSize.Height;
page.Paragraphs.Add(image);
// Save PDF document
document.Save(dataDir + "EMFtoPDF_out.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 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:
So the following code snippet follows these steps and shows how to convert BMP to PDF using C#:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertGIFtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
var image = new Aspose.Pdf.Image();
// Load sample GIF image file
image.File = dataDir + "GIFtoPDF.gif";
page.Paragraphs.Add(image);
// Save PDF document
document.Save(dataDir + "GIFtoPDF_out.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.
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:
The code snippet below shows how to convert JPG Image to PDF using C#:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertJPGtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
var image = new Aspose.Pdf.Image();
// Load input JPG file
image.File = dataDir + "JPGtoPDF.jpg";
// Add image on a page
page.Paragraphs.Add(image);
// Save PDF document
document.Save(dataDir + "JPGtoPDF_out.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:
Following code snippet shows how to convert an Image to PDF with same page height and width using C#:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertJPGtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
var image = new Aspose.Pdf.Image();
// Load JPEG file
image.File = dataDir + "JPGtoPDF.jpg";
// Read Height of input image
page.PageInfo.Height = image.BitmapSize.Height;
// Read Width of input image
page.PageInfo.Width = image.BitmapSize.Width;
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 PDF document
document.Save(dataDir + "JPGtoPDF_out.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.
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:
Moreover, the code snippet below shows how to convert PNG to PDF with C# in your .NET applications:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPNGtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
var image = new Aspose.Pdf.Image();
// Load PNG file
image.File = dataDir + "PNGtoPDF.png";
// Read Height of input image
page.PageInfo.Height = image.BitmapSize.Height;
// Read Width of input image
page.PageInfo.Width = image.BitmapSize.Width;
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 PDF document
document.Save(dataDir + "PNGtoPDF_out.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.
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.
SvgLoadOptions
class.Document
class with mention source filename and options.The following code snippet shows the process of converting SVG file into PDF format with Aspose.PDF for .NET.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertSVGtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
var option = new Aspose.Pdf.SvgLoadOptions();
// Open SVG file
using (var document = new Aspose.Pdf.Document(dataDir + "SVGtoPDF.svg", option))
{
// Save PDF document
document.Save(dataDir + "SVGtoPDF_out.pdf");
}
}
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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertSVGtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
var loadopt = new Aspose.Pdf.SvgLoadOptions();
loadopt.AdjustPageSize = true;
// Open SVG file
using (var document = new Aspose.Pdf.Document(dataDir + "SVGtoPDF.svg", loadopt))
{
document.Pages[1].PageInfo.Margin.Top = 0;
document.Pages[1].PageInfo.Margin.Left = 0;
document.Pages[1].PageInfo.Margin.Bottom = 0;
document.Pages[1].PageInfo.Margin.Right = 0;
// Save PDF document
document.Save(dataDir + "SVGtoPDF_out.pdf");
}
}
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> |
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:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertTIFFtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
document.Pages.Add();
var image = new Aspose.Pdf.Image();
// Load sample Tiff image file
image.File = dataDir + "TIFFtoPDF.tiff";
document.Pages[1].Paragraphs.Add(image);
// Save PDF document
document.Save(dataDir + "TIFFtoPDF_out.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:
The following code snippet shows how to convert multi-page or multi-frame TIFF image to PDF with C#:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertTIFFtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
using (var bitmap = new System.Drawing.Bitmap(File.OpenRead(dataDir + "TIFFtoPDF.tif")))
{
// Convert multi page or multi frame TIFF to PDF
var dimension = new FrameDimension(bitmap.FrameDimensionsList[0]);
var frameCount = bitmap.GetFrameCount(dimension);
// Iterate through each frame
for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
{
var page = document.Pages.Add();
bitmap.SelectActiveFrame(dimension, frameIdx);
using (var currentImage = new MemoryStream())
{
bitmap.Save(currentImage, ImageFormat.Tiff);
var imageht = new Aspose.Pdf.Image
{
ImageStream = currentImage,
//Apply some other options
//ImageScale = 0.5
};
page.Paragraphs.Add(imageht);
}
}
}
// Save PDF document
document.Save(dataDir + "TIFFtoPDF_out.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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertCDRtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open CDR file
using (var document = new Aspose.Pdf.Document(dataDir + "CDRtoPDF.cdr", new CdrLoadOptions()))
{
// Save PDF document
document.Save(dataDir + "CDRtoPDF_out.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.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertDJVUtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open DJVU file
using (var document = new Aspose.Pdf.Document(dataDir + "CDRtoPDF.djvu", new DjvuLoadOptions()))
{
// Save PDF document
document.Save(dataDir + "CDRtoPDF_out.pdf");
}
}
A HEIC file is a High-Efficiency Container Image file format that can store multiple images as a collection in a single file. For loading heic images you need to add a reference to the https://www.nuget.org/packages/FileFormat.Heic/ nuget package. Convert HEIC images to PDF using Aspose.PDF:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHEICtoPDF()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open HEIC file
using (var fs = new FileStream(dataDir + "HEICtoPDF.heic", FileMode.Open))
{
var image = FileFormat.Heic.Decoder.HeicImage.Load(fs);
var pixels = image.GetByteArray(PixelFormat.Rgb24);
var width = (int)image.Width;
var height = (int)image.Height;
using (var document = new Aspose.Pdf.Document())
{
var page = document.Pages.Add();
var asposeImage = new Aspose.Pdf.Image();
asposeImage.BitmapInfo = new Aspose.Pdf.BitmapInfo(pixels, width, height, Aspose.Pdf.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);
// Save PDF document
document.Save(dataDir + "HEICtoPDF_out.pdf");
}
}
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.