News Category

Conversion

Conversion (28)

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:

Covert PDF to EMF image file format in C#

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

2022-07-13 01:01:00 Written by support iceblue

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");
        }
    }
}

C#/VB.NET: Convert PDF to PDF/A

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

2023-06-09 08:21:00 Written by support iceblue

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();
        }
    }
}

C#/VB.NET: Convert PDF to Word

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();
        }
    }
}

C#/VB.NET: Convert PDF to Word

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.

Save PDF Document as tiff image

2014-07-11 09:10:07 Written by support iceblue

Tiff image as a graphics container that can store both raster and vector images; may contain high-quality graphics that support color depths from 1 to 24-bit; supports both lossy and lossless compression; also supports multiple layers and pages. If you want your documents can convert to the high-quality graphics and don’t lose image file information when they are saved during the compression process, the tiff image is your best choice.

This article introduces a detail method to save PDF document as tiff image via document.SaveAsImage() and JoinTiffImages() methods by using Spire.PDF for .NET which is a PDF component which contains an incredible wealth of features to create, read, edit and manipulate PDF documents on .NET, Silverlight and WPF Platform. The below screenshot presents result after saving PDF document as tiff image:

Save PDF Document as tiff image

The main steps of the method are:

Step 1: Create a new pdf-document and load it.

PdfDocument document = new PdfDocument();
document.LoadFromFile(@"sample.pdf");

Step 2: Using document.SaveAsImage() method to save the pdf document as image array.

  private static Image[] SaveAsImage(PdfDocument document)
          {
            Image[] images = new Image[document.Pages.Count];
            for (int i = 0; i < document.Pages.Count; i++)
              {
//use the document.SaveAsImage() method save the pdf as image
                images[i] = document.SaveAsImage(i);
           }
            return images;
          }

Step 3: Using JoinTiffImages() method to save the images from pdf pages to tiff image type, with the specified encoder and image-encoder parameters.

   public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
        {
            //use the save encoder
            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);
            Image pages = images[0];
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;
                    //save the first frame
                    pages.Save(outFile, info, ep);
                }
                else
                {
                    //save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                    pages.SaveAdd(img, ep);
               }
                if (frame == images.Length - 1)
                {
                    //flush and close.
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }

Download and install Spire.Pdf for .NET and use below code to experience this method to save pdf document as tiff image.

The full code:

[C#]
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Pdf;
namespace SavePdfAsTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"01.pdf");
    JoinTiffImages(SaveAsImage(document),"result.tiff",EncoderValue.CompressionLZW);
            System.Diagnostics.Process.Start("result.tiff");
        }
        private static Image[] SaveAsImage(PdfDocument document)
        {
            Image[] images = new Image[document.Pages.Count];
            for (int i = 0; i < document.Pages.Count; i++)
            {
                images[i] = document.SaveAsImage(i);
            }
            return images;
        }

        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                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)
        {
            //use the save encoder
            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);
            Image pages = images[0];
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;
                    //save the first frame
                    pages.Save(outFile, info, ep);
                }

                else
                {
                    //save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }
                if (frame == images.Length - 1)
                {
                    //flush and close.
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}
[VB.NET]
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Spire.Pdf
Namespace SavePdfAsTiff
    Class Program
        Sub Main()
            Dim document As PdfDocument = New PdfDocument()
            document.LoadFromFile("01.pdf")
            JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW)
            System.Diagnostics.Process.Start("result.tiff")
        End Sub
        Private Shared Function SaveAsImage(ByVal document As PdfDocument)
            Dim images() As Image = New Image(document.Pages.Count-1) {}
            Dim i As Integer
            For i = 0 To document.Pages.Count - 1 Step i + 1
                images(i) = document.SaveAsImage(i)
            Next
            Return images
        End Function
        Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
            Dim encoders() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders()
            Dim j As Integer
            For j = 0 To encoders.Length - 1 Step j + 1
                If encoders(j).MimeType = mimeType Then
                    Return encoders(j)
                End If
            Next
            Throw New Exception(mimeType + " mime type not found in ImageCodecInfo")
        End Function
        Public Shared Sub JoinTiffImages(ByVal images() As Image, ByVal outFile As String, ByVal compressEncoder As EncoderValue)
            'use the save encoder
            Dim enc As Encoder = Encoder.SaveFlag
            Dim ep As EncoderParameters = New EncoderParameters(2)
            ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.MultiFrame, Long))
            ep.Param(1) = New EncoderParameter(Encoder.Compression, CType(compressEncoder, Long))
            Dim pages As Image = images(0)
            Dim frame As Integer = 0
            Dim info As ImageCodecInfo = GetEncoderInfo("image/tiff")
            Dim img As Image
            For Each img In images
                If frame = 0 Then
                    pages = img
                    'save the first frame
                    pages.Save(outFile, info, ep)
                Else
                    'save the intermediate frames
                    ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.FrameDimensionPage, Long))
                    pages.SaveAdd(img, ep)
                End If

                If frame = images.Length - 1 Then
                    'flush and close.
                    ep.Param(0) = New EncoderParameter(enc, CType(EncoderValue.Flush, Long))
                    pages.SaveAdd(ep)
                End If
                frame = frame + 1
            Next
        End Sub
    End Class
End Namespace

Besides convert HTML URL to PDF and HTML file to PDF, now Spire.PDF starts to support converting HTML string to PDF. This article will show you how to convert HTML string into PDF file in C#. We support tables, text and Hyperlinks in the HTML strings. Please check the steps as below:

  • Download Spire.PDF for .NET (Version 3.0.27 above) and install it correctly. The Spire.PDF installation is clean, professional and wrapped up in a MSI installer.
  • Add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll".
  • Here comes to the codes:

Step 1: Create a new PDF document.

PdfDocument pdf = new PdfDocument();

Step 2: Set the layout and page setting

PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
//webBrowser load html whether Waiting
htmlLayoutFormat.IsWaiting = false;
//page setting
PdfPageSettings setting = new PdfPageSettings();
setting.Size = PdfPageSize.A4;

Step 3: Load the HTML string code and generate the PDF file.

string htmlCode = File.ReadAllText("..\\..\\2.html");

//use single thread to generate the pdf from above html code
Thread thread = new Thread(() =>
{ pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat);});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();

Step 4: Save the file to PDF and preview it.

pdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");

Please check the effective screenshot:

Convert HTML String to PDF in C#

Full codes:

using Spire.Pdf;
using Spire.Pdf.HtmlConverter;
using System.IO;
using System.Threading;

namespace LoadFromHTML
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
            htmlLayoutFormat.IsWaiting = false;
            PdfPageSettings setting = new PdfPageSettings();
            setting.Size = PdfPageSize.A4;
            string htmlCode = File.ReadAllText("..\\..\\2.html");

            Thread thread = new Thread(() =>
            { pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();

            pdf.SaveToFile("output.pdf");
            System.Diagnostics.Process.Start("output.pdf");
        }
    }
}

Spire.PDF is an easy-to-use and powerful .NET PDF library. It can do a lot of conversions, and one of them is converting PDF page to image. As to converting PDF page to image, it works conveniently and flexibly. It has 6 overloaded functions named SaveAsImage that can make sure you find one meeting your need.

You can use Spire.PDF to convert any specific page of PDF document to BMP and Metafile image. Check it here.

In this article, we will discuss conversion with specified resolution.

[C#]
public Image SaveAsImage(int pageIndex, int dpiX, int dpiY)
  • pageIndex: specify which page to convert, 0 indicates the first page.
  • dpiX: specify the resolution of x coordinate axis in PDF page when converting.
  • dpiX: specify the resolution of y coordinate axis in PDF page when converting.
[C#]
Image image = documemt.SaveAsImage(0, PdfImageType.Bitmap, false, 400, 400)

In the sample code, the size of PDF page is Width = 612.0, Height = 792.0. We set the resolution to 400, 400. And we will get an image with width = 3400, height = 4400.

Here is sample code:

[C#]
PdfDocument documemt = new PdfDocument();
documemt.LoadFromFile(@"..\..\EnglishText.pdf");
Image image = documemt.SaveAsImage(0, PdfImageType.Bitmap, false, 400, 400);
image.Save(@"..\..\result.jpg");
documemt.Close();

Effect Screentshot:

image with specified resolution

For the function of converting image to PDF, Spire.PDF can handle it quickly and effectively. This .NET PDF library can not only convert images of commonly used formats to PDF document such as jpg, bmp, png, but also convert gif, tif and ico images to PDF. Just download it here.

To convert multipage image to a PDF file with Spire.PDF, just copy the following code to your application and call method ConvertImagetoPDF and you will get it done.

Step 1: Method to split multipage image

Spire.Pdf has a method called DrawImage to convert image to PDF. But it cannot handle multipage image directly. So before conversion, multipage image need to be split into several one-page images.

[C#]
Guid guid = image.FrameDimensionsList[0];
FrameDimension dimension = new FrameDimension(guid);
int pageCount = image.GetFrameCount(dimension);

This step is to get the total number of frames (pages) in the multipage image.

[C#]
image.SelectActiveFrame(dimension, i);

And this step is to select one frame of frames within this image object.

[C#]
image.Save(buffer, format);

Save the selected frame to the buffer.

Step 2: Convert image to PDF

After splitting multipage image, Spire.Pdf can draw these split images directly to PDF using method DrawImage.

[C#]
PdfImage pdfImg = PdfImage.FromImage(img[i])

Load image file as PdfImage.

[C#]
page.Canvas.DrawImage(pdfImg, x, 0, width, height);

Draw PdfImage to PDF. The only thing to do is to specify the location of image on PDF. Width and height is the size of area that image will be drawn on. Sometimes we need to scale up or down the size of the original size of image until it fit the PDF page. x and 0 locate the coordinate.

Check the effective screenshots for the original TIF file.

multi_images

The target PDF file:

multi_images_to_pdf

Full demo:

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ConvertMultipageImagetoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                ConvertImagetoPDF(@"..\..\Chapter1.tif");
            }
        }      

        public static void ConvertImagetoPDF(String ImageFilename)
        {
            using (PdfDocument pdfDoc = new PdfDocument())
            {
                Image image = Image.FromFile(ImageFilename);

                Image[] img = SplitImages(image, ImageFormat.Png);

                for (int i = 0; i < img.Length; i++)
                {
                    PdfImage pdfImg = PdfImage.FromImage(img[i]);
                    PdfPageBase page = pdfDoc.Pages.Add();
                    float width = pdfImg.Width * 0.3f;
                    float height = pdfImg.Height * 0.3f;
                    float x = (page.Canvas.ClientSize.Width - width) / 2;

                    page.Canvas.DrawImage(pdfImg, x, 0, width, height);
                }

                string PdfFilename = "result.pdf";
                pdfDoc.SaveToFile(PdfFilename);
                System.Diagnostics.Process.Start(PdfFilename);
            }
        }

        public static Image[] SplitImages(Image image, ImageFormat format)
        {
            Guid guid = image.FrameDimensionsList[0];
            FrameDimension dimension = new FrameDimension(guid);
            int pageCount = image.GetFrameCount(dimension);

            Image[] frames = new Image[pageCount];

            for (int i = 0; i < pageCount; i++)
            {
                using (MemoryStream buffer = new MemoryStream())
                {
                    image.SelectActiveFrame(dimension, i);
                    image.Save(buffer, format);
                    frames[i] = Image.FromStream(buffer);
                }
            }
            return frames;        
        }
    }
}

Converting HTML to PDF with C# PDF component is so important that we always try our best to improve our Spire.PDF better and better. We aim to make it is much more convenient for our developers to use. Now besides the previous method of converting HTML to PDF offered by Spire.PDF, we have a new plugin for html conversion to PDF. This section will focus on the new plugin of convert HTML to PDF. With this new plugin, we support to convert the HTML page with rich elements, such as HTTPS, CSS3, HTML5, JavaScript.

You need to download Spire.PDF and install it on your system, add Spire.PDF.dll as reference in the downloaded Bin folder thought the below path '..\Spire.PDF\Bin\NET4.0\Spire.PDF.dll'. And for gain the new plugin, you could get the new plugin from the download file directly: windows-x86.zip windows-x64.zip macosx_x64.zip linux_x64.tar.gz .

On Windows system, you need to unzip the convertor plugin package and copy the folder 'plugins' under the same folder of Spire.Pdf.dll. Before you use QT plugin for converting HTML to PDF, please ensure you have installed Microsoft Visual C++ 2015 Redistributable on your computer.

On Mac and Linux system, you need to copy the zip file under the system and then unzip the convertor plugin package there to use the plugins successfully.

C#  HtmlToPdf.zip and VB.NET  HtmlToPdfVB.zip, you could download and try it.

Calling the plugins is very simple, please check the below C# code for convert HTML to PDF.

[C#]
using System.Drawing;
using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter.Qt;

namespace SPIREPDF_HTMLtoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            HtmlConverter.Convert("http://www.wikipedia.org/", "HTMLtoPDF.pdf",
                
                //enable javascript
                true,

                //load timeout
                100 * 1000,

                //page size
                new SizeF(612, 792),

                //page margins
                new PdfMargins(0, 0));
            System.Diagnostics.Process.Start("HTMLtoPDF.pdf");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Pdf.Graphics
Imports Spire.Pdf.HtmlConverter.Qt

Module Module1

    Sub Main()
        HtmlConverter.Convert("http://www.wikipedia.org/", "HTMLtoPDF.pdf", True, 100 * 1000, New SizeF(612, 792), New PdfMargins(0, 0))
        System.Diagnostics.Process.Start("HTMLtoPDF.pdf")
    End Sub

End Module

Please check the effective screenshot as below:

HTML_to_PDF_c#

The following sample will focus on the new plugin of convert HTML string to PDF.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Pdf;
using System.IO;
using Spire.Pdf.HtmlConverter;
using System.Drawing;
namespace HTMLToPDFwithPlugins
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =@"<strong>This is a test for converting HTML string to PDF </strong>
                 <ul><li>Spire.PDF supports to convert HTML in URL into PDF</li>
                 <li>Spire.PDF supports to convert HTML string into PDF</li>
                 <li>With the new plugin</li></ul>";
         
            string outputFile = "ToPDF.pdf";

            Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert(input,

            outputFile,
            //enable javascript
            true,
            //load timeout
            10 * 1000,
            //page size
            new SizeF(612, 792),
            //page margins
            new Spire.Pdf.Graphics.PdfMargins(0),
            //load from content type
            LoadHtmlType.SourceCode
            );
            System.Diagnostics.Process.Start(outputFile);
        }
    }
}

Effective screenshot:

HTML_to_PDF_c#

XPS is a format similar to PDF but uses XML in layout, appearance and printing information of a file. XPS format was developed by Microsoft and it is natively supported by the Windows operating systems. If you want to work with your PDF files on a Windows computer without installing other software, you can convert it to XPS format. Likewise, if you need to share a XPS file with a Mac user or use it on various devices, it is more recommended to convert it to PDF. This article will demonstrate how to programmatically convert PDF to XPS or XPS to 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 DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF 

Convert PDF to XPS in C# and VB.NET

Spire.PDF for .NET supports converting PDF to various file formats, and to achieve the PDF to XPS conversion, you just need three lines of core code. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Convert the PDF document to an XPS file using PdfDocument.SaveToFile (string filename, FileFormat.XPS) method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace ConvertPdfToXps
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load sample PDF document 
            pdf.LoadFromFile("sample.pdf");

            //Save it to XPS format 
            pdf.SaveToFile("ToXPS.xps", FileFormat.XPS);
            pdf.Close();
        }
    }
}

C#/VB.NET: Convert PDF to XPS or XPS to PDF

Convert XPS to PDF in C# and VB.NET

Conversion from XPS to PDF can also be achieved with Spire.PDF for .NET. While converting, you can set to keep high quality image on the generated PDF file by using the PdfDocument.ConvertOptions.SetXpsToPdfOptions() method. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load an XPS file using PdfDocument.LoadFromFile(string filename, FileFormat.XPS) method or PdfDocument.LoadFromXPS() method.
  • While conversion, set the XPS to PDF convert options to keep high quality images using PdfDocument.ConvertOptions.SetXpsToPdfOptions() method.
  • Save the XPS file to a PDF file using PdfDocument.SaveToFile(string filename, FileFormat.PDF) method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace ConvertXPStoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample XPS file
            pdf.LoadFromFile("Sample.xps", FileFormat.XPS);
            //pdf.LoadFromXPS("Sample.xps");

            //Keep high quality images when converting XPS to PDF
            pdf.ConvertOptions.SetXpsToPdfOptions(true);

            //Save the XPS file to PDF
            pdf.SaveToFile("XPStoPDF.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Convert PDF to XPS or XPS to 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.

PDF files have the advantage of being highly interactive and easy to transfer, but in certain cases, it is also necessary to convert PDF to images for embedding in web pages or displaying on some platforms that do not support PDF format. In this article, you will learn how to convert PDF to JPG, PNG or BMP image formats 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 

Convert a Specific PDF Page to an Image in C# and VB.NET

Spire.PDF for .NET offers the PdfDocument.SaveAsImage() method to convert a particular page in PDF to an image. Then, you can save the image as a JPEG, PNG, BMP, EMF, GIF or WMF file. The following are the detailed steps.

  • Create a Document instance.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Convert a specific page to an image and set the image Dpi using PdfDocument.SaveAsImage(int pageIndex, PdfImageType type, int dpiX, int dpiY) method.
  • Save the image as a PNG, JPG or BMP file using Image.Save(string filename, ImageFormat format) method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
using System.Drawing.Imaging;

namespace PDFtoImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF document
            pdf.LoadFromFile("E:\\Files\\input.pdf");

            //Convert the first page to an image and set the image Dpi
            Image image = pdf.SaveAsImage(0, PdfImageType.Bitmap, 500, 500);

            //Save the image as a JPG file
            image.Save("ToJPG.jpg", ImageFormat.Jpeg);

            //Save the image as a PNG file
            //image.Save("ToPNG.png", ImageFormat.Png);

            //Save the image as a BMP file
            //image.Save("ToBMP.bmp", ImageFormat.Bmp);
        }
    }
}

C#/VB.NET: Convert PDF to Images (JPG, PNG, BMP)

Convert an Entire PDF Document to Multiple Images in C# and VB.NET

If you want to convert the whole PDF document into multiple individual images, you can loop through all the pages in the PDF and then save them as JPG, PNG or BMP images. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF document using PdfDocument.LoadFromFile() method.
  • Loop through all pages of the document and set the image Dpi when converting them to images using PdfDocument.SaveAsImage(int pageIndex, PdfImageType type, int dpiX, int dpiY) method.
  • Save images as PNG files using Image.Save() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace PDFtoImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            //Load a sample PDF document
            pdf.LoadFromFile("input.pdf");

            //Loop through each page in the PDF
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                //Convert all pages to images and set the image Dpi
                Image image = pdf.SaveAsImage(i, PdfImageType.Bitmap, 500, 500);

                //Save images as PNG format to a specified folder 
                String file = String.Format("Image\\ToImage-{0}.png", i);
                image.Save(file, ImageFormat.Png);
              
            }
        }
    }
}

C#/VB.NET: Convert PDF to Images (JPG, PNG, BMP)

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.

Page 2 of 2