PDF格式的文檔廣泛用於各種辦公場所,在工作中難免會有將PDF文檔轉換為其他文檔格式的需要。在本篇文檔中,將介紹PDF轉為SVG的方法。根據不同的轉換需求,這里分三種情況進行講述,即轉PDF所有頁為SVG、轉PDF指定頁為SVG和轉PDF到指定高度、寬度的SVG。以上三種情況,下面將作詳細介紹。
使用工具:Spire.PDF for .NET
提示:使用該組件需要先下載安裝,在項目程序中注意須添加引用Spire.PDF.dll文件(如下所示)

原PDF文檔:

1.將PDF所有頁轉為SVG
using Spire.Pdf;
namespace PDFtoSVG_PDF
{
class Program
{
static void Main(string[] args)
{
//新建一個PdfDocument類對象,加載sample,保存為SVG格式的文件
PdfDocument document = new PdfDocument();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
document.SaveToFile("output.svg", FileFormat.SVG);
}
}
}
復制代碼
調試運行該項目,生成文檔:


2.將指定PDF頁轉為SVG
using Spire.Pdf;
namespace ConvertPDFPagetoSVG_PDF
{
class Program
{
static void Main(string[] args)
{
//實例化一個PdfDocument類對象
PdfDocument doc = new PdfDocument();
//加載PDF文件
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
//調用方法SaveToFile(string filename, int startIndex, int endIndex, FileFormat )將PDF指定頁保存為SVG格式
doc.SaveToFile("Result.svg", 1, 2, FileFormat.SVG);
}
}
}
復制代碼
調試運行程序后,可查看成功轉換的SVG文檔
轉換后的文檔:

3.PDF轉指定寬度、高度的SVG
using Spire.Pdf;
namespace PDFtoSVG1_PDF
{
class Program
{
static void Main(string[] args)
{
//創建一個PdfDocument類對象,並加載PDF文件
PdfDocument document = new PdfDocument();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
//調用方法SetPdfToSvgOptions()指定轉換SVG的寬度和高度
document.ConvertOptions.SetPdfToSvgOptions(700f, 1000f);
//保存到文件,命名文檔,並設置保存格式
document.SaveToFile("result.svg", FileFormat.SVG);
}
}
}
復制代碼
(編輯:雷林鵬 來源:網絡)