Codecs: How to convert pages of DOCX document to the PNG files?
In This Topic
VintaSoft Imaging .NET SDK can convert pages of DOCX document to the raster images and save rendered images to the image files.
For executing code below it is necessary:
- VintaSoft Imaging .NET SDK (Vintasoft.Shared.dll; Vintasoft.Imaging.dll; Vintasoft.Imaging.Gdi.dll or Vintasoft.Imaging.Drawing.SkiaSharp.dll)
- VintaSoft Office .NET Plug-in (Vintasoft.Imaging.Office.OpenXml.dll)
- System.IO.Packaging nuget-package (nuget-package is used by Vintasoft.Imaging.Office.OpenXml.dll)
- System.Drawing.Common nuget-package (nuget-package is necessary if GDI+ should be used as the 2D drawing engine in VintaSoft Imaging .NET SDK) or
SkiaSharp nuget-package (nuget-package is necessary if SkiaSharp should be used as the 2D drawing engine in VintaSoft Imaging .NET SDK)
Here is C#/VB.NET code that shows how to convert pages of DOCX document to the PNG files:
/// <summary>
/// Converts pages of DOCX document to PNG files.
/// </summary>
public static void ConvertDocxToPng(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 PNG encoder
using (Vintasoft.Imaging.Codecs.Encoders.PngEncoder pngEncoder =
new Vintasoft.Imaging.Codecs.Encoders.PngEncoder())
{
// for each page in DOCX document
for (int i = 0; i < images.Count; i++)
{
// save rendered image to a PNG file
images[i].Save(string.Format("page{0}.png", i), pngEncoder);
}
}
// dispose images
images.ClearAndDisposeItems();
}
}
''' <summary>
''' Converts pages of DOCX document to PNG files.
''' </summary>
Public Shared Sub ConvertDocxToPng(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 PNG encoder
Using pngEncoder As New Vintasoft.Imaging.Codecs.Encoders.PngEncoder()
' for each page in DOCX document
For i As Integer = 0 To images.Count - 1
' save rendered image to a PNG file
images(i).Save(String.Format("page{0}.png", i), pngEncoder)
Next
End Using
' dispose images
images.ClearAndDisposeItems()
End Using
End Sub