DOCX: Рендеринг страниц DOCX-документа
В этом разделе
Вот C#/VB Код .NET, который показывает, как открыть DOCX-документ с помощью класса
DocxDecoder, отрендерить все страницы DOCX-документа и сохранить отрендеренные изображения в PNG-файлы:
/// <summary>
/// Converts pages of DOCX document to PNG files.
/// </summary>
public static void ConvertDocxToPng(string docxFileName)
{
// 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)
' 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
Вот C#/VB.NET код, который демонстрирует, как открыть DOCX-документ с помощью класса
DocxDocument, отрендерить все страницы DOCX-документа и сохранить отрендеренные изображения в PNG-файлы:
/// <summary>
/// Renders all pages of DOCX document and saves rendered images to PNG files.
/// </summary>
/// <param name="filename">A name of DOCX file.</param>
public static void RenderAndSaveDocxPages(string filename)
{
// open DOCX document
using (Vintasoft.Imaging.Office.OpenXml.Docx.DocxDocument docxDocument =
new Vintasoft.Imaging.Office.OpenXml.Docx.DocxDocument(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
// for each DOCX page
for (int i = 0; i < docxDocument.Pages.Count; i++)
{
// render DOCX page
using (Vintasoft.Imaging.VintasoftImage renderedPage = docxDocument.Pages[i].Render())
{
// save rendered image to a PNG file
renderedPage.Save(string.Format("page{0}.png", i));
}
}
}
}
''' <summary>
''' Renders all pages of DOCX document and saves rendered images to PNG files.
''' </summary>
''' <param name="filename">A name of DOCX file.</param>
Public Shared Sub RenderAndSaveDocxPages(filename As String)
' open DOCX document
Using docxDocument As New Vintasoft.Imaging.Office.OpenXml.Docx.DocxDocument(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read)
' for each DOCX page
For i As Integer = 0 To docxDocument.Pages.Count - 1
' render DOCX page
Using renderedPage As Vintasoft.Imaging.VintasoftImage = docxDocument.Pages(i).Render()
' save rendered image to a PNG file
renderedPage.Save(String.Format("page{0}.png", i))
End Using
Next
End Using
End Sub