この簡単なチュートリアルでは、** C#を使用してWordをスキャンしたPDFに変換する方法について説明します。 ** C#を使用している間、数行のコードと単純なAPI呼び出しを使用して、WordをスキャンされたPDF**に変換します。このアプリケーションは、Windows、macOS、またはLinuxベースのプラットフォーム上の.NETCoreベースの環境で使用できます。
C#を使用してWordをスキャンされたPDFに変換する手順
- NuGetパッケージマネージャーとSystems.DrawingからAspose.Wordsへの参照を追加するようにプロジェクトを構成します
- Documentクラスオブジェクトを使用して、ディスクからソースWordファイルをロードします
- Wordファイルからページ範囲を変換し、IPageSavingCallbackを使用してメモリストリームに画像として保存します
- 保存したワードページの画像ストリームを読み込み、DocumentBuilder内に画像として追加します
- ドキュメントをスキャンしたPDFとしてディスクに保存します
- C#スキャン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の空白ページを削除する方法を参照してください。