如何使用 C# 将 Word 转换为扫描的 PDF

这个简短的教程将指导您如何使用 C#** 将 Word 转换为扫描的 PDF。在使用 C# 将 Word 转换为扫描的 PDF 时,只需几行代码和简单的 API 调用。该应用程序可以在基于 Windows、macOS 或 Linux 的平台上的任何基于 .NET Core 的环境中使用。

使用 C# 将 Word 转换为扫描的 PDF 的步骤

  1. 配置项目以从 NuGet 包管理器和 Systems.Drawing 添加对 Aspose.Words 的引用
  2. 使用 Document 类对象从磁盘加载源 Word 文件
  3. 使用 IPageSavingCallback 转换 Word 文件中的页面范围并将它们保存为内存流中的图像
  4. 加载保存的word页面图片流,并作为图片添加到DocumentBuilder中
  5. 将文档另存为磁盘上的扫描 PDF

借助上述操作序列,可以轻松使用 C# scan DOC to PDF。我们将从磁盘加载源 DOCX 文件开始该过程,并使用 IPageSavingCallback 将页面范围内选择的每个页面转换为 JPEG 图像的内存流。然后我们遍历单个页面的 JPEG 内存流,并使用 DocumentBuilder 类追加内部页面。最后,文档在磁盘上保存为只读扫描 PDF。

使用 C# 将 Word 转换为扫描的 PDF 的代码

using System;
using System.Collections;
using System.IO;
using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Saving;
using SkiaSharp;
namespace WordKB
{
public class WordToPDFScanned
{
public static void ConvertWordToPDF()
{
// Applying product license to read the Barcodes from image
License WordToPdf = new License();
WordToPdf.SetLicense("Aspose.Total.lic");
string WordFilePath = "AsposeTest.docx";
string ScannedPdfFilePath = "ScannedOutput.pdf";
WordToPDFScanner(WordFilePath, ScannedPdfFilePath);
}
public static void WordToPDFScanner(string WordFile, string ScannedPDFFile)
{
// Load Word document from file on disk
Document TempDoc = new Document(WordFile);
ImageSaveOptions jpeg_Opts = new ImageSaveOptions(SaveFormat.Jpeg);
PageRange pageRange = new PageRange(0, TempDoc.PageCount - 1);
jpeg_Opts.PageSet = new PageSet(pageRange);
WordToJpegImages JpegHandler = new WordToJpegImages();
jpeg_Opts.PageSavingCallback = JpegHandler;
MemoryStream memoryStream = new MemoryStream();
TempDoc.Save(memoryStream, jpeg_Opts);
Document ScannedPdf = new Document();
ScannedPdf.RemoveAllChildren();
foreach (MemoryStream JpegStream in JpegHandler.JpegStreams)
{
JpegStream.Position = 0;
using (SKBitmap jpg_image = SKBitmap.Decode(JpegStream))
{
Document image_Doc = new Document();
DocumentBuilder pdf_builder = new DocumentBuilder(image_Doc);
PageSetup ps = pdf_builder.PageSetup;
ps.PageWidth = ConvertUtil.PixelToPoint(jpg_image.Width);
ps.PageHeight = ConvertUtil.PixelToPoint(jpg_image.Height);
// Insert JPEG image inside the document and position it at the top left corner of the page
pdf_builder.InsertImage(jpg_image, RelativeHorizontalPosition.Page, 0, RelativeVerticalPosition.Page,
0, ps.PageWidth, ps.PageHeight, Aspose.Words.Drawing.WrapType.None);
ScannedPdf.AppendDocument(image_Doc, ImportFormatMode.KeepSourceFormatting);
}
}
ScannedPdf.Save(ScannedPDFFile);
}
}
public class WordToJpegImages : IPageSavingCallback
{
public ArrayList JpegStreams = new ArrayList();
public void PageSaving(PageSavingArgs args)
{
args.PageStream = new MemoryStream();
args.KeepPageStreamOpen = true;
JpegStreams.Add(args.PageStream);
}
}
}

为了使用 C# 将 Word 转换为 PDF 并获得扫描的 PDF,我们采用了先将 Word 文档页面转换为 JPEG 图像并使其成为只读的方法。然后通过使用 DocumentBuilder 类,我们使用第一步中创建的图像创建了一个新文档,并将其作为 PDF 保存在磁盘上。

在本教程中,我们了解到,为了开发一个 Word 到扫描的 PDF 转换器,可以有效地使用基于 C# 的 API。如果要使用 C# 删除 Word 文件中的空白页,请参阅文章 如何使用C#删除Word中的空白页

 简体中文