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);
}
}
}
以上內容爲PDF轉爲SVG的三種情況的方法介紹,可根據不同的辦公需求選擇相應的轉換方法。
如果喜歡,歡迎轉載(轉載請註明出處)