Кодеки: Как конвертировать PDF в SVG?
В этом разделе
VintaSoft Imaging .NET SDK может отрендерить PDF страницы в векторной форме и сохранить векторное содержимое в SVG файл.
VintaSoft Imaging .NET SDK позволяет конвертировать PDF документ в SVG-документ, используя класс
DocumentConverter или
ImageCollection. Использование класса
DocumentConverter обеспечивает наилучшую производительность, поскольку класс
DocumentConverter использует многопоточность.
Вот C#/VB.NET код, который демонстрирует, как преобразовать PDF документ в SVG файл(ы) с помощью класса
DocumentConverter:
/// <summary>
/// Converts PDF document to a SVG file(s) using Vintasoft.Imaging.DocumentConverter class.
/// </summary>
public static void ConvertPdfToSvg_DocumentConverter(string pdfFileName, string svgFileName)
{
// see GetSinglePageFilename method in DocumentConverter class for examples of available single page format names
string svgFormattedFileName = System.IO.Path.GetFileNameWithoutExtension(svgFileName) + "_{PageNumber}.svg";
Vintasoft.Imaging.DocumentConverter.Convert(pdfFileName, svgFormattedFileName);
}
''' <summary>
''' Converts PDF document to a SVG file(s) using Vintasoft.Imaging.DocumentConverter class.
''' </summary>
Public Shared Sub ConvertPdfToSvg_DocumentConverter(pdfFileName As String, svgFileName As String)
' see GetSinglePageFilename method in DocumentConverter class for examples of available single page format names
Dim svgFormattedFileName As String = System.IO.Path.GetFileNameWithoutExtension(svgFileName) & "_{PageNumber}.svg"
Vintasoft.Imaging.DocumentConverter.Convert(pdfFileName, svgFormattedFileName)
End Sub
Вот C#/VB. NET-код, показывающий, как преобразовать PDF документ в SVG файл(ы) с использованием класса
ImageCollection:
/// <summary>
/// Converts PDF document to SVG file(s) using ImageCollection and SvgEncoder classes.
/// </summary>
public static void ConvertPdfToSvg_ImageCollection(string pdfFileName, string svgFileName)
{
// create image collection
using (Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection())
{
// add DOCX document to the image collection
images.Add(pdfFileName);
// create SVG encoder
using (Vintasoft.Imaging.Codecs.Encoders.SvgEncoder svgEncoder =
new Vintasoft.Imaging.Codecs.Encoders.SvgEncoder())
{
// specify that SVG encoder should compress embedded image using PNG compression
svgEncoder.Settings.EmbeddedImageEncoder = new Vintasoft.Imaging.Codecs.Encoders.PngEncoder();
// if document contains only one page
if (images.Count == 1)
{
// save page to SVG file
images[0].Save(svgFileName, svgEncoder);
}
// if document contains several pages
else
{
// for each page in PDF document
for (int i = 0; i < images.Count; i++)
{
// save page to SVG file
images[i].Save(GetPageFilename(svgFileName, i + 1), svgEncoder);
}
}
}
// dispose images
images.ClearAndDisposeItems();
}
}
/// <summary>
/// Returns the filename for specified page number.
/// </summary>
/// <param name="fileName">The filename.</param>
/// <param name="pageNumber">The page number.</param>
/// <returns>The filename for specified page number.</returns>
private static string GetPageFilename(string fileName, int pageNumber)
{
string fileExtension = System.IO.Path.GetExtension(fileName);
string dirName = System.IO.Path.GetDirectoryName(fileName);
string filePathWithoutExtension = System.IO.Path.Combine(dirName, System.IO.Path.GetFileNameWithoutExtension(fileName));
return string.Format("{0}_{1}{2}", filePathWithoutExtension, pageNumber, fileExtension);
}
''' <summary>
''' Converts PDF document to SVG file(s) using ImageCollection and SvgEncoder classes.
''' </summary>
Public Shared Sub ConvertPdfToSvg_ImageCollection(pdfFileName As String, svgFileName As String)
' create image collection
Using images As New Vintasoft.Imaging.ImageCollection()
' add DOCX document to the image collection
images.Add(pdfFileName)
' create SVG encoder
Using svgEncoder As New Vintasoft.Imaging.Codecs.Encoders.SvgEncoder()
' specify that SVG encoder should compress embedded image using PNG compression
svgEncoder.Settings.EmbeddedImageEncoder = New Vintasoft.Imaging.Codecs.Encoders.PngEncoder()
' if document contains only one page
If images.Count = 1 Then
' save page to SVG file
images(0).Save(svgFileName, svgEncoder)
Else
' if document contains several pages
' for each page in PDF document
For i As Integer = 0 To images.Count - 1
' save page to SVG file
images(i).Save(GetPageFilename(svgFileName, i + 1), svgEncoder)
Next
End If
End Using
' dispose images
images.ClearAndDisposeItems()
End Using
End Sub
''' <summary>
''' Returns the filename for specified page number.
''' </summary>
''' <param name="fileName">The filename.</param>
''' <param name="pageNumber">The page number.</param>
''' <returns>The filename for specified page number.</returns>
Private Shared Function GetPageFilename(fileName As String, pageNumber As Integer) As String
Dim fileExtension As String = System.IO.Path.GetExtension(fileName)
Dim dirName As String = System.IO.Path.GetDirectoryName(fileName)
Dim filePathWithoutExtension As String = System.IO.Path.Combine(dirName, System.IO.Path.GetFileNameWithoutExtension(fileName))
Return String.Format("{0}_{1}{2}", filePathWithoutExtension, pageNumber, fileExtension)
End Function