Кодеки: Как сконвертировать DOCX в SVG?
В этом разделе
VintaSoft Imaging .NET SDK может отрендерить DOCX-страницы в векторной форме и сохранить векторное содержимое в SVG-файл.
Для выполнения приведенного ниже кода необходимо:
- VintaSoft Imaging .NET SDK (Vintasoft.Shared.dll; Vintasoft.Imaging.dll; Vintasoft.Imaging.Gdi.dll или Vintasoft.Imaging.Drawing.SkiaSharp.dll)
- VintaSoft Office .NET Plug-in (Vintasoft.Imaging.Office.OpenXml.dll)
- System.IO.Packaging nuget-пакет (nuget-пакет используется Vintasoft.Imaging.Office.OpenXml.dll)
-
System.Drawing.Common nuget-пакет (nuget-пакет необходим, если GDI+ следует использовать в качестве движка для 2D-рисования в VintaSoft Imaging .NET SDK) или
SkiaSharp nuget-пакет (nuget-пакет необходим, если SkiaSharp следует использовать в качестве движка для 2D-рисования в VintaSoft Imaging .NET SDK)
Вот C#/VB.NET код, который демонстрирует, как сконвертировать DOCX-документ в SVG-файл(ы) с использованием классов
ImageCollection и
SvgEncoder:
/// <summary>
/// Converts DOCX document to the SVG files using ImageCollection and SvgEncoder classes.
/// </summary>
public static void ConvertDocxToSvg(string docxFileName)
{
// specify that VintaSoft Imaging .NET SDK should use GDI+ for drawing of 2D graphics
Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault();
// specify that VintaSoft Imaging .NET SDK should use SkiaSharp for drawing of 2D graphics
//Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault();
// create image collection
using (Vintasoft.Imaging.ImageCollection images = new Vintasoft.Imaging.ImageCollection())
{
// add DOCX document to the image collection
images.Add(docxFileName);
// 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();
// for each page in DOCX document
for (int i = 0; i < images.Count; i++)
{
// save page to SVG file
images[i].Save(string.Format("page{0}.svg", i), svgEncoder);
}
}
// dispose images
images.ClearAndDisposeItems();
}
}
''' <summary>
''' Converts DOCX document to the SVG files using ImageCollection and SvgEncoder classes.
''' </summary>
Public Shared Sub ConvertDocxToSvg(docxFileName As String)
' specify that VintaSoft Imaging .NET SDK should use GDI+ for drawing of 2D graphics
Vintasoft.Imaging.Drawing.Gdi.GdiGraphicsFactory.SetAsDefault()
' specify that VintaSoft Imaging .NET SDK should use SkiaSharp for drawing of 2D graphics
'Vintasoft.Imaging.Drawing.SkiaSharp.SkiaSharpDrawingFactory.SetAsDefault()
' create image collection
Using images As New Vintasoft.Imaging.ImageCollection()
' add DOCX document to the image collection
images.Add(docxFileName)
' 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()
' for each page in DOCX document
For i As Integer = 0 To images.Count - 1
' save page to SVG file
images(i).Save(String.Format("page{0}.svg", i), svgEncoder)
Next
End Using
' dispose images
images.ClearAndDisposeItems()
End Using
End Sub