Сохранить страницы документа как изображения с помощью C# и .NET.


Load a document and save all pages as separate PNG & Jpeg images.

Полный код

using System.IO;
using SautinSoft.Document;
using SkiaSharp;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            SeparateDocumentToImagePages();
        }
        /// <summary>
        /// Load a document and save all pages as separate PNG & Jpeg images.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/pagination-save-document-pages-as-png-jpg-jpeg-net-csharp-vb.php
        /// </remarks>
        static void SeparateDocumentToImagePages()
        {
            string filePath = @"..\..\..\example.docx";
            DocumentCore dc = DocumentCore.Load(filePath);
            string folderPath = Path.GetFullPath(@"Result-files");
            DocumentPaginator dp = dc.GetPaginator();
            for (int i = 0; i < dp.Pages.Count; i++)
            {
                DocumentPage page = dp.Pages[i];
                Directory.CreateDirectory(folderPath);

                // Save the each page as Bitmap.
                ImageSaveOptions op = new ImageSaveOptions();
                op.DpiX = 300;
                op.DpiY = 300;
                SKBitmap bmp = page.Rasterize(op, SautinSoft.Document.Color.White);
                // Save the bitmap to PNG and JPEG.
                bmp.Encode(new FileStream(folderPath + @"\Page (PNG) - " + (i + 1).ToString() + ".png", FileMode.Create), SKEncodedImageFormat.Png, 100);
                bmp.Encode(new FileStream(folderPath + @"\Page (Jpeg) - " + (i + 1).ToString() + ".jpg", FileMode.Create), SKEncodedImageFormat.Jpeg, 100);
            }
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(folderPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System.IO
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing
Imports SkiaSharp

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			SeparateDocumentToImagePages()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Load a document and save all pages as separate PNG & Jpeg images.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/pagination-save-document-pages-as-png-jpg-jpeg-net-csharp-vb.php
		''' </remarks>
		Private Shared Sub SeparateDocumentToImagePages()
			Dim filePath As String = "..\..\..\example.docx"
			Dim dc As DocumentCore = DocumentCore.Load(filePath)
			Dim folderPath As String = Path.GetFullPath("Result-files")
			Dim dp As DocumentPaginator = dc.GetPaginator()
			For i As Integer = 0 To dp.Pages.Count - 1
				Dim page As DocumentPage = dp.Pages(i)
				Directory.CreateDirectory(folderPath)

				' Save the each page as Bitmap.
				Dim dpi As ImageSaveOptions = New ImageSaveOptions
				dpi.DpiX = 300
				dpi.DpiY = 300

				Dim bmp As SKBitmap = page.Rasterize(dpi, SautinSoft.Document.Color.White)
				' Save the bitmap to PNG and JPEG.
				bmp.Encode(New FileStream(folderPath & "\Page (PNG) - " & (i + 1).ToString() & ".png", FileMode.Create), SKEncodedImageFormat.Png, 100)
				bmp.Encode(New FileStream(folderPath & "\Page (Jpeg) - " & (i + 1).ToString() & ".jpg", FileMode.Create), SKEncodedImageFormat.Jpeg, 100)
			Next i
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(folderPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.