How to convert PDF to Image, SVG, XPS, HTML in C#

 

Documents at work come in a variety of formats, with different needs for different use cases, tools, and so on. This article mainly introduces the method of using C# to convert PDF to several file formats. The method needs to use Spire.PDF for .NET . To obtain this component, you can download the free version directly from the official website. After downloading and installing, add the reference dll file to the project and add the using directive.

 

 

1.PDF turning Image

C#

 

using Spire.Pdf;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertPDFToImage_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a PdfDocument class and load the document
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\test.pdf");
            // Traverse each page of the PDF
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                //Convert PDF pages to bitmap graphics
                System.Drawing.Image bmp = doc.SaveAsImage(i);

                //Save the bitmap graphic as a picture in png format (here, you can save the document in any picture format you want, here is a list of saving in png format)
                string fileName = string.Format("Page-{0}.png", i + 1);
                bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
    }
}

 

 

Test Results:



 

2. PDF to SVG

Here PDF to SVG can be divided into three cases

Test document:



 

2.1 Convert all pages to SVG

 

C#

 

using Spire.Pdf;

namespace PDFtoSVG_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new PdfDocument class object, load the sample, and save it as a file in SVG format
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
            document.SaveToFile("output.svg", FileFormat.SVG);
        }
    }
}

 

Test Results:



 




 
2.2 Convert PDF specified pages to SVG

C#

 

using Spire.Pdf;

namespace ConvertPDFPagetoSVG_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate a PdfDocument class object
            PdfDocument doc = new PdfDocument();
            //load PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
            //Call the method SaveToFile(string filename, int startIndex, int endIndex, FileFormat) to save the specified page of PDF as SVG format
            doc.SaveToFile("Result.svg", 1, 2, FileFormat.SVG);
        }
    }
}

 

 

 Test Results:



 

2.3 Convert PDF to SVG with specified width and height
C#

using Spire.Pdf;

namespace PDFtoSVG1_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument class object and load the PDF file
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
            //Call the method SetPdfToSvgOptions() to specify the width and height of the converted SVG
            document.ConvertOptions.SetPdfToSvgOptions(700f, 1000f);
            //Save to file, name the document, and set the save format
            document.SaveToFile("result.svg", FileFormat.SVG);
        }
    }
}

 Test Results:



 
 
3. PDF to XPS

C#

 

using Spire.Pdf;

namespace ConvertPDFToXPS_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of the PdfDocument class and load the document
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\test.pdf");

            //Save the file as XPS
            doc.SaveToFile("sample.xps", FileFormat.XPS);
            System.Diagnostics.Process.Start("sample.xps");
        }
    }
}

 

 

Test Results:



 

4. PDF conversion Html

C#

 

using Spire.Pdf;

namespace ConvertPDFToHTML
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of the PdfDocument class and load the document
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\test.pdf");
            //Save the document as HTML
            pdf.SaveToFile("Result.html", FileFormat.HTML);
        }
    }
}

 

 

Test Results:



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326021600&siteId=291194637