Convert the PDF to word, HTML, SVG, XPS and save them to stream
This article we will demonstrate how to convert the PDF pages to HTML, Word, SVG, XPS, PDF and save them to stream by calling the method PdfDocument.SaveToStream() offered by Spire.PDF. And starts from Spire.PDF version 4.3, it newly supports to convert the defined range of PDF pages and save them to stream.
Save the PDF to stream
Step 1: Create a new PdfDocument instance and load the sample document from file.
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Sample.pdf");
Step 2: Save the document to stream.
MemoryStream ms=new MemoryStream (); pdf.SaveToStream(ms);
Save the PDF to stream and defined the file format to HTML, Word, SVG, XPS and PDF
Step 1: Create a new PdfDocument instance and load the sample document from file.
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Sample.pdf");
Step 2: Save the document to stream and use FileFormat format to define the format.
MemoryStream ms=new MemoryStream (); pdf.SaveToStream(ms, FileFormat.HTML);
Convert the defined range of PDF pages to HTML, word, SVG, XPS and save them to stream
Step 1: Create a new PdfDocument instance and load the sample document from file.
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Sample.pdf");
Step 2: Only save some PDF pages to stream by using pdf.SaveToStream(int startIndex, int endIndex, FileFormat format) method; and FileFormat.PDF is not supported.
pdf.SaveToStream(1, 2, FileFormat.SVG);
Full codes of save PDF to stream:
using Spire.Pdf; using System.IO; namespace SavePDFToStream { class Program { static void Main(string[] args) { PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("Sample.pdf"); MemoryStream ms = new MemoryStream(); pdf.SaveToStream(ms); pdf.SaveToStream(ms, FileFormat.HTML); pdf.SaveToStream(1, 2, FileFormat.SVG); } } }
Convert PDF to SVG with Custom Width and Height in C#, VB.NET
We've have demonstrated how to convert PDF page to SVG file format in the previous post. This guidance shows you how we can specify the width and height of output file using the latest version of Spire.PDF with C# and VB.NET.
Step 1: Load a sample PDF document to PdfDocument instance.
PdfDocument document = new PdfDocument(); document.LoadFromFile("pdf-sample.pdf");
Step 2: Specify the output file size through ConvertOptions.SetPdfToSvgOptions() method.
document.ConvertOptions.SetPdfToSvgOptions(800f, 1200f);
Step 3: Save PDF to SVG file format.
document.SaveToFile("result.svg", FileFormat.SVG);
Output:
Full Code:
using Spire.Pdf; namespace ConvertPDFtoSVG { class Program { static void Main(string[] args) { PdfDocument document = new PdfDocument(); document.LoadFromFile("pdf-sample.pdf"); document.ConvertOptions.SetPdfToSvgOptions(800f, 1200f); document.SaveToFile("result.svg", FileFormat.SVG); } } }
Imports Spire.Pdf Namespace ConvertPDFtoSVG Class Program Private Shared Sub Main(args As String()) Dim document As New PdfDocument() document.LoadFromFile("pdf-sample.pdf") document.ConvertOptions.SetPdfToSvgOptions(800F, 1200F) document.SaveToFile("result.svg", FileFormat.SVG) End Sub End Class End Namespace
C#: Convert PDF to HTML
PDF documents have been a popular choice for sharing information due to their cross-platform compatibility and ability to preserve the original layout and formatting. However, as the web continues to evolve, there is an increasing demand for content that can be easily integrated into websites and other online platforms. In this context, converting PDF to HTML format has become highly valuable. By converting PDF files to more flexible and accessible HTML, users gain the ability to better utilize, share, and reuse PDF-based information within the web environment. In this article, we will demonstrate how to convert PDF files to HTML format in C# using Spire.PDF for .NET.
- Convert PDF to HTML in C#
- Set Conversion Options When Converting PDF to HTML in C#
- Convert PDF to HTML Stream in C#
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Convert PDF to HTML in C#
To convert a PDF document to HTML format, you can use the PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method provided by Spire.PDF for .NET. The detailed steps are as follows.
- Create an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
- Save the PDF document to HTML format using the PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method.
- C#
using Spire.Pdf; namespace ConvertPdfToHtml { internal class Program { static void Main(string[] args) { // Create an instance of the PdfDocument class PdfDocument doc = new PdfDocument(); // Load a PDF document doc.LoadFromFile("Sample.pdf"); // Save the PDF document to HTML format doc.SaveToFile("PdfToHtml.html", FileFormat.HTML); doc.Close(); } } }
Set Conversion Options When Converting PDF to HTML in C#
The PdfConvertOptions.SetPdfToHtmlOptions() method allows you to customize the conversion options when transforming PDF files to HTML. This method takes several parameters that you can use to configure the conversion process, such as:
- useEmbeddedSvg (bool): Indicates whether to embed SVG in the resulting HTML file.
- useEmbeddedImg (bool): Indicates whether to embed images in the resulting HTML file. This option is applicable only when useEmbeddedSvg is set to false.
- maxPageOneFile (int): Specifies the maximum number of pages to be included per HTML file. This option is applicable only when useEmbeddedSvg is set to false.
- useHighQualityEmbeddedSvg (bool): Indicates whether to use high-quality embedded SVG in the resulting HTML file. This option is applicable when useEmbeddedSvg is set to true.
The following steps explain how to customize the conversion options when transforming a PDF to HTML using Spire.PDF for .NET.
- Create an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
- Get the PdfConvertOptions object using the PdfDocument.ConvertOptions property.
- Set the PDF to HTML conversion options using PdfConvertOptions.SetPdfToHtmlOptions(bool useEmbeddedSvg, bool useEmbeddedImg, int maxPageOneFile, bool useHighQualityEmbeddedSvg) method.
- Save the PDF document to HTML format using PdfDocument.SaveToFile(string fileName, FileFormat.HTML) method.
- C#
using Spire.Pdf; namespace ConvertPdfToHtmlWithCustomOptions { internal class Program { static void Main(string[] args) { // Create an instance of the PdfDocument class PdfDocument doc = new PdfDocument(); // Load a PDF document doc.LoadFromFile("Sample.pdf"); // Set the conversion options to embed images in the resulting HTML and limit one page per HTML file PdfConvertOptions pdfToHtmlOptions = doc.ConvertOptions; pdfToHtmlOptions.SetPdfToHtmlOptions(false, true, 1, false); // Save the PDF document to HTML format doc.SaveToFile("PdfToHtmlWithCustomOptions.html", FileFormat.HTML); doc.Close(); } } }
Convert PDF to HTML Stream in C#
Instead of saving a PDF document to an HTML file, you can save it to an HTML stream by using the PdfDocument.SaveToStream(Stream stream, FileFormat.HTML) method. The detailed steps are as follows.
- Create an instance of the PdfDocument class.
- Load a PDF document using the PdfDocument.LoadFromFile(string fileName) method.
- Create an instance of the MemoryStream class.
- Save the PDF document to an HTML stream using the PdfDocument.SaveToStream(Stream stream, FileFormat.HTML) method.
- C#
using Spire.Pdf; using System.IO; namespace ConvertPdfToHtmlStream { internal class Program { static void Main(string[] args) { // Create an instance of the PdfDocument class PdfDocument doc = new PdfDocument(); // Load a PDF document doc.LoadFromFile("Sample.pdf"); // Save the PDF document to HTML stream using (var fileStream = new MemoryStream()) { doc.SaveToStream(fileStream, FileFormat.HTML); // You can now do something with the HTML stream, such as Write it to a file using (var outputFile = new FileStream("PdfToHtmlStream.html", FileMode.Create)) { fileStream.Seek(0, SeekOrigin.Begin); fileStream.CopyTo(outputFile); } } doc.Close(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Convert PDF Page to SVG in C#/VB.NET
Spire.PDF allows us to convert a PDF document, a single page or a range of pages in a PDF document to SVG file format. We have introduced how to convert a PDF document to SVG, in this tutorial, we are going to demonstrate how to convert a PDF page to SVG.
Below is the source PDF file we used in this example:
Code snippets:
Step 1: Instantiate an object of PdfDocument class and load the PDF document.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile("Test.pdf");
Step 2: Convert the first page of the PDF document to SVG using the SaveToFile(string filename, int startIndex, int endIndex, FileFormat fileFormat) method.
doc.SaveToFile("Result.svg", 0, 0, FileFormat.SVG);
The resultant SVG file looks as follows:
Full code:
using Spire.Pdf; namespace PDF_Page_to_SVG { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile("Test.pdf"); doc.SaveToFile("Result.svg", 0, 0, FileFormat.SVG); } } }
Imports Spire.Pdf Namespace PDF_Page_to_SVG Class Program Private Shared Sub Main(args As String()) Dim doc As New PdfDocument() doc.LoadFromFile("Test.pdf") doc.SaveToFile("Result.svg", 0, 0, FileFormat.SVG) End Sub End Class End Namespace
How to keep high quality image when convert XPS to PDF in C#
We have already had an article of showing how to convert the XPS files into PDF file. Starts from Spire.PDF V3.8.68, it newly supports to keep the high quality image on the resulted PDF file from the XPS document. Spire.PDF offers a property of pdf.UseHighQualityImage to set the image quality before loading the original XPS file. This article will focus on demonstrate how to save image with high quality for the conversion from XPS to PDF.
Here comes to the code snippets:
Step 1: Create a PDF instance.
PdfDocument pdf = new PdfDocument();
Step 2: Set the value of UseHighQualityImage as true to use the high quality image when convert XPS to PDF.
pdf.UseHighQualityImage = true;
Step 3: Load the XPS document by use either the method of pdf.LoadFromFile() or pdf.LoadFromXPS().
pdf.LoadFromFile("Sample.xps",FileFormat.XPS);
Step 4: Save the document to file.
pdf.SaveToFile("result.pdf");
Effective screenshot of the high quality image after saving XPS as PDF file format:
Full codes:
using Spire.Pdf; namespace ConvertXPStoPDF { class Program { static void Main(string[] args) { PdfDocument pdf = new PdfDocument(); pdf.UseHighQualityImage = true; pdf.LoadFromFile("Sample.xps", FileFormat.XPS); //pdf.LoadFromXPS("Sample.xps"); pdf.SaveToFile("result.pdf", FileFormat.PDF); } } }
C#/VB.NET: Convert PDF to SVG
SVG (Scalable Vector Graphics) is an image file format used for rendering two-dimensional images on the web. Comparing with other image file formats, SVG has many advantages such as supporting interactivity and animation, allowing users to search, index, script, and compress/enlarge images without losing quality. Occasionally, you may need to convert PDF files to SVG file format, and this article will demonstrate how to accomplish this task using Spire.PDF for .NET.
- Convert a PDF File to SVG in C#/VB.NET
- Convert Selected PDF Pages to SVG in C#/VB.NET
- Convert a PDF File to SVG with Custom Width and Height in C#/VB.NET
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Convert a PDF File to SVG in C#/VB.NET
Spire.PDF for .NET offers the PdfDocument.SaveToFile(String, FileFormat) method to convert each page in a PDF file to a single SVG file. The detailed steps are as follows.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Convert the PDF file to SVG using PdfDocument.SaveToFile(String, FileFormat) method.
- C#
- VB.NET
using Spire.Pdf; namespace ConvertPDFtoSVG { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument document = new PdfDocument(); //Load a sample PDF file document.LoadFromFile("input.pdf"); //Convert PDF to SVG document.SaveToFile("PDFtoSVG.svg", FileFormat.SVG); } } }
Convert Selected PDF Pages to SVG in C#/VB.NET
The PdfDocument.SaveToFile(String, Int32, Int32, FileFormat) method allows you to convert the specified pages in a PDF file to SVG files. The detailed steps are as follows.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Convert selected PDF pages to SVG using PdfDocument.SaveToFile(String, Int32, Int32, FileFormat) method.
- C#
- VB.NET
using Spire.Pdf; namespace PDFPagetoSVG { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a sample PDF file doc.LoadFromFile("input.pdf"); //Convert selected PDF pages to SVG doc.SaveToFile("PDFPagetoSVG.svg", 1, 2, FileFormat.SVG); } } }
Convert a PDF File to SVG with Custom Width and Height in C#/VB.NET
The PdfConvertOptions.SetPdfToSvgOptions() method offered by Spire.PDF for .NET allows you to specify the width and height of output SVG file. The detailed steps are as follows.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Set PDF convert options using PdfDocument.ConvertOptions property.
- Specify the width and height of output SVG file using PdfConvertOptions.SetPdfToSvgOptions() method.
- Convert the PDF file to SVG using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace PDFtoSVG { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument document = new PdfDocument(); //Load a sample PDF file document.LoadFromFile("input.pdf"); //Specify the width and height of output SVG file document.ConvertOptions.SetPdfToSvgOptions(800f, 1200f); //Convert PDF to SVG document.SaveToFile("result.svg", FileFormat.SVG); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Covert PDF to EMF image file format in C#
Spire.PDF supports to save the PDF files into different image file formats, such as BMP, JPG, PNG, GIF and TIFF. It also supports to save the PDF files as the Enhanced Metafile (EMF) image file format. This article will demonstrate how to save the PDF file as the EMF image file format in C#. With the help of Spire.PDF, we only need three lines of codes to finish the conversion function.
Note: Before Start, please download the latest version of Spire.PDF and add Spire.Pdf.dll in the bin folder as the reference of Visual Studio.
Here comes to the steps of how to export the PDF file to EMF in C#:
Step 1: Create a new PDF document and load from file.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf");
Step 2: Call to use the SaveAsImage method to save all the PDF pages as System.Drawing.Imaging.ImageFormat.Emf file format.
for (int i = 0; i < doc.Pages.Count; i++) { String fileName = String.Format("Sample-img-{0}.emf", i); using (Image image = doc.SaveAsImage(i, Spire.Pdf.Graphics.PdfImageType.Metafile, 300, 300)) { image.Save(fileName, System.Drawing.Imaging.ImageFormat.Emf); } }
Effective screenshot:
Full codes:
using Spire.Pdf; using System; using System.Drawing; namespace ConvertPDFtoEMF { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); for (int i = 0; i < doc.Pages.Count; i++) { String fileName = String.Format("Sample-img-{0}.emf", i); using (Image image = doc.SaveAsImage(i, Spire.Pdf.Graphics.PdfImageType.Metafile, 300, 300)) { image.Save(fileName, System.Drawing.Imaging.ImageFormat.Emf); } } } } }
C#/VB.NET: Convert PDF to PDF/A
PDF/A is an ISO-standardized version of PDF that supports archiving of files for future use. Documents in PDF/A format can be reproduced in exactly the same way regardless of the software used. Due to its advantages in long-term preservation of digital documents, it may sometimes be necessary to convert PDF to PDF/A. In this article, you will learn how to programmatically convert PDF to PDF/A-1A, 2A, 3A, 1B, 2B and 3B compliant PDF using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Convert PDF to PDF/A
The detailed steps are as follows.
- Specify the input file path and output folder
- Create a PdfStandardsConverter instance and pass in the input file as a parameter.
- Convert the input file to PdfA1A conformance level using PdfStandardsConverter.ToPdfA1A() method.
- Convert the input file to PdfA1B conformance level using PdfStandardsConverter.ToPdfA1B() method.
- Convert the input file to PdfA2A conformance level using PdfStandardsConverter.ToPdfA2A() method.
- Convert the input file to PdfA2B conformance level using PdfStandardsConverter.ToPdfA2B() method.
- Convert the input file to PdfA3A conformance level using PdfStandardsConverter.ToPdfA3A() method.
- Convert the input file to PdfA3B conformance level using PdfStandardsConverter.ToPdfA3B() method.
- C#
- VB.NET
using System; using Spire.Pdf.Conversion; namespace ConvertPdf2Pdfa { class Program { static void Main(string[] args) { //Specify input file path String inputFile = @"C:\Users\Administrator\Desktop\sample.pdf"; //Specify output folder String outputFolder = @"C:\Users\Administrator\Desktop\Output\"; //Create a PdfStandardsConverter instance, passing in the input file as a parameter PdfStandardsConverter converter = new PdfStandardsConverter(inputFile); //Convert to PdfA1A converter.ToPdfA1A(outputFolder + "ToPdfA1A.pdf"); //Convert to PdfA1B converter.ToPdfA1B(outputFolder + "ToPdfA1B.pdf"); //Convert to PdfA2A converter.ToPdfA2A(outputFolder + "ToPdfA2A.pdf"); //Convert to PdfA2B converter.ToPdfA2B(outputFolder + "ToPdfA2B.pdf"); //Convert to PdfA3A converter.ToPdfA3A(outputFolder + "ToPdfA3A.pdf"); //Convert to PdfA3B converter.ToPdfA3B(outputFolder + "ToPdfA3B.pdf"); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
C#/VB.NET: Convert PDF to Word
PDF format is the best choice in many cases, but Word is more flexible when editing or modification is needed. PDF files are typically used for online sharing, printing and archiving, while Word documents are used for creating, editing and formatting documents. Converting a PDF to Word is a good option if you want to re-edit the PDF document. In this article, you will learn how to programmatically convert PDF to Word in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Background Knowledge
Spire.PDF for .NET provides two modes of conversion. The advantages and disadvantages of these two modes are as follows:
- Fixed Layout Mode: The fixed layout mode has fast conversion speed and is conducive to maintaining the original appearance of PDF files to the greatest extent. However, the editability of the resulting document will be limited since each line of text in PDF will be presented in a separate frame in the generated Word document.
- Flow Recognition Mode: The flow recognition mode is a full recognition mode. The converted content will not be presented in frames, and the structure of the resulting document is flowable. The generated Word document is easy to re-edit but may look different from the original PDF file.
Convert PDF to Fixed-Layout Doc/Docx in C#, VB.NET
By default, the PdfDcoument.SaveToFile() method will convert PDF to Word with fixed layout. The following are the detailed steps.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Convert the PDF document to a Doc or Docx format file using PdfDocument.SaveToFile(String fileName, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Pdf; namespace ConvertPdfToFixedLayoutWord { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); //Convert PDF to Doc and save it to a specified path doc.SaveToFile("output/ToDoc.doc", FileFormat.DOC); //Convert PDF to Docx and save it to a specified path doc.SaveToFile("output/ToDocx.docx", FileFormat.DOCX); doc.Close(); } } }
Convert PDF to Flexible-Structured Doc/Docx in C#, VB.NET
In addition to the default conversion engine, Spire.PDF for .NET provides another engine called Ps mode, which works better with the flow recognition mode. To enable Ps conversion engine and flow recognition mode, pass (true, true) as the parameters of the PdfDocument.ConvertOptions.SetPdfToDocOptions(bool usePsMode, bool useFlowRecognitionMode) method. The entire steps are as follows.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Enable Ps conversion engine and flow recognition mode using PdfDocument.ConvertOptions.SetPdfToDocOptions(true, true) method.
- Convert the PDF document to a Doc or Docx format file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace ConvertPdfToFlexibleLayoutWord { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); //Specify the PDF to Word conversion options doc.ConvertOptions.SetPdfToDocOptions(true, true); //Convert PDF to Doc doc.SaveToFile("output/ToDoc.doc", FileFormat.DOC); //Convert PDF to Docx doc.SaveToFile("output/ToDocx.docx", FileFormat.DOCX); doc.Close(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
C#: Convert PDF to TIFF or TIFF to PDF
PDF files are widely used for sharing and viewing documents across different platforms, while TIFF files are preferred for storing high-quality images with detailed graphics or photographs. Converting a PDF file to TIFF can maintain the quality of images within the file. Similarly, converting a TIFF image to PDF ensures that the image can be easily viewed, shared, and printed without compatibility issues. In this article, you will learn how to programmatically convert PDF to TIFF or TIFF to PDF in C# using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Convert PDF to TIFF in C#
The TIFF format allows multiple images to be stored in a single file. With Spire.PDF for .NET, you can convert each page of a PDF file into a separate image, and then call the custom method JoinTiffImages() to combine these images and save them as a single TIFF image.
The following are the steps to convert a PDF into a multi-page TIFF file using C#.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Call custom method SaveAsImage() to convert each page of the PDF to a separate image.
- Call custom method JoinTiffImages() to merge the converted images into a multi-page TIFF image.
- C#
using System; using System.Drawing; using System.Drawing.Imaging; using Spire.Pdf; namespace SavePdfAsTiff { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.LoadFromFile("Report.pdf"); //Convert PDF pages to images Image[] images = SaveAsImage(pdf); //Combine the images and save them as a multi-page TIFF file JoinTiffImages(images, "result.tiff", EncoderValue.CompressionLZW); } private static Image[] SaveAsImage(PdfDocument document) { //Create a new image array Image[] images = new Image[document.Pages.Count]; //Iterate through all pages in the document for (int i = 0; i < document.Pages.Count; i++) { //Convert a specific page to an image images[i] = document.SaveAsImage(i); } return images; } private static ImageCodecInfo GetEncoderInfo(string mimeType) { //Get the image encoders ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders(); for (int j = 0; j < encoders.Length; j++) { //Find the encoder that matches the specified MIME type if (encoders[j].MimeType == mimeType) return encoders[j]; } throw new Exception(mimeType + " mime type not found in ImageCodecInfo"); } public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder) { //Set the encoder parameters Encoder enc = Encoder.SaveFlag; EncoderParameters ep = new EncoderParameters(2); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder); //Get the first image Image pages = images[0]; //Initialize a frame int frame = 0; //Get an ImageCodecInfo object for processing TIFF image codec information ImageCodecInfo info = GetEncoderInfo("image/tiff"); //Iterate through each Image foreach (Image img in images) { //If it's the first frame, save it to the output file with specified encoder parameters if (frame == 0) { pages = img; pages.Save(outFile, info, ep); } else { //Save the intermediate frames ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); pages.SaveAdd(img, ep); } //If it's the last frame, flush the encoder parameters and close the file if (frame == images.Length - 1) { ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); pages.SaveAdd(ep); } frame++; } } } }
Convert TIFF to PDF in C#
To convert a multi-page TIFF image to a PDF file, you need to convert each frame of the TIFF image to a separate PDF image. Then draw each image at a specified location on a PDF page through the PdfPageBase.Canvas.DrawImage() method.
The following are the steps to convert a TIFF image to a PDF file using C#.
- Create a PdfDocument object.
- Load a TIFF image using Image.FromFile() method.
- Call custom method SplitTiffImage() to split the TIFF image into separate images.
- Iterate through the split images, and then convert each into a PDF image.
- Add a page to the PDF document using PdfDocument.Pages.Add() method.
- Draw the PDF image at a specified location on the page using PdfPageBase.Canvas.DrawImage() method.
- Save the result PDF file using PdfDocument.SaveToFile() method.
- C#
using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using Spire.Pdf; using Spire.Pdf.Graphics; namespace TiffToPdf { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a TIFF image Image tiffImage = Image.FromFile("result.tiff"); //Split the Tiff image into separate images Image[] images = SplitTiffImage(tiffImage); //Iterate through the images for (int i = 0; i < images.Length; i++) { //Convert a specified image into a PDF image PdfImage pdfImg = PdfImage.FromImage(images[i]); //Get image width and height float width = pdfImg.Width; float height = pdfImg.Height; //Add a page with the same size as the image SizeF size = new SizeF(width, height); PdfPageBase page = pdf.Pages.Add(size); //Draw the image at a specified location on the page page.Canvas.DrawImage(pdfImg, 0, 0, width, height); } //Save the result file pdf.SaveToFile("TiffToPdf.pdf"); } public static Image[] SplitTiffImage(Image tiffImage) { //Get the number of frames in the Tiff image int frameCount = tiffImage.GetFrameCount(FrameDimension.Page); //Create an image array to store the split tiff images Image[] images = new Image[frameCount]; //Gets the GUID of the first frame dimension Guid objGuid = tiffImage.FrameDimensionsList[0]; //Create a FrameDimension object FrameDimension objDimension = new FrameDimension(objGuid); //Iterate through each frame for (int i = 0; i < frameCount; i++) { //Select a specified frame tiffImage.SelectActiveFrame(objDimension, i); //Save the frame in TIFF format to a memory stream MemoryStream ms = new MemoryStream(); tiffImage.Save(ms, ImageFormat.Tiff); //Load an image from memory stream images[i] = Image.FromStream(ms); } return images; } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.