บทแนะนำสั้น ๆ นี้จะแนะนำคุณเกี่ยวกับวิธี แปลง Word เป็น PDF ที่สแกนโดยใช้ C# ขณะใช้ C# แปลง Word เป็นสแกน PDF โดยใช้โค้ดไม่กี่บรรทัดและการเรียก API อย่างง่าย แอปพลิเคชันสามารถใช้ในสภาพแวดล้อมที่ใช้ .NET Core บนแพลตฟอร์มที่ใช้ Windows, macOS หรือ Linux
ขั้นตอนในการแปลง Word เป็น PDF ที่สแกนโดยใช้ C#
- กำหนดค่าโครงการเพื่อเพิ่มการอ้างอิงไปยัง Aspose.Words จากตัวจัดการแพ็คเกจ NuGet และ Systems.Drawing
- โหลดไฟล์ Word ต้นฉบับจากดิสก์โดยใช้วัตถุคลาส Document
- แปลงช่วงของหน้าจากไฟล์ Word และบันทึกเป็นรูปภาพใน Memory Stream โดยใช้ IPageSavingCallback
- โหลดสตรีมรูปภาพหน้าคำที่บันทึกไว้และเพิ่มเป็นภาพภายใน DocumentBuilder
- บันทึกเอกสารเป็น PDF ที่สแกนบนดิสก์
การใช้ C# สแกนเอกสาร DOC เป็น PDF ทำได้อย่างง่ายดายด้วยความช่วยเหลือจากลำดับการดำเนินการที่กล่าวถึงข้างต้น เราจะเริ่มกระบวนการด้วยการโหลดไฟล์ DOCX ต้นทางจากดิสก์ และใช้ IPageSavingCallback แต่ละหน้าที่เลือกในช่วงของหน้าจะถูกแปลงเป็นสตรีมหน่วยความจำของภาพ JPEG จากนั้นเราจะทำซ้ำสตรีมหน่วยความจำ JPEG แต่ละหน้าและต่อท้ายภายในหน้าโดยใช้คลาส DocumentBuilder สุดท้าย เอกสารจะถูกบันทึกเป็น PDF ที่สแกนแบบอ่านอย่างเดียวบนดิสก์
รหัสเพื่อแปลง Word เป็น PDF ที่สแกนโดยใช้ C
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); | |
} | |
} | |
} |
ในการ แปลง Word เป็น PDF โดยใช้ C# และรับไฟล์ PDF ที่สแกน เราได้ใช้วิธีการแปลงหน้าเอกสาร Word เป็นภาพ JPEG ก่อน และทำให้เป็นแบบอ่านอย่างเดียว จากนั้นใช้คลาส DocumentBuilder เราได้สร้างเอกสารใหม่โดยใช้อิมเมจที่สร้างในขั้นตอนแรกและบันทึกเป็น PDF บนดิสก์
ในบทช่วยสอนนี้ เราได้เรียนรู้ว่าในการพัฒนา Word เพื่อสแกน PDF converter C# ตาม API สามารถใช้ได้อย่างมีประสิทธิภาพ หากคุณต้องการลบหน้าว่างในไฟล์ Word โดยใช้ C# โปรดดูบทความ วิธีลบหน้าว่างใน Word โดยใช้ C#