이 간단한 자습서는 C#을 사용하여 Word를 스캔한 PDF로 변환하는 방법에 대해 안내합니다. **C#을 사용하는 동안 몇 줄의 코드와 간단한 API 호출을 사용하여 Word를 스캔한 PDF**로 변환합니다. 애플리케이션은 Windows, macOS 또는 Linux 기반 플랫폼의 .NET Core 기반 환경에서 사용할 수 있습니다.
C#을 사용하여 Word를 스캔한 PDF로 변환하는 단계
- NuGet 패키지 관리자 및 Systems.Drawing에서 Aspose.Words에 대한 참조를 추가하도록 프로젝트를 구성합니다.
- Document 클래스 개체를 사용하여 디스크에서 소스 Word 파일 로드
- Word 파일에서 페이지 범위를 변환하고 IPageSavingCallback을 사용하여 메모리 스트림에 이미지로 저장합니다.
- 저장된 워드 페이지 이미지 스트림을 로드하고 DocumentBuilder 내부에 이미지로 추가합니다.
- 디스크에 문서를 스캔한 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 to scan PDF 변환기 C# 기반 API를 개발하기 위해 효과적으로 사용할 수 있음을 배웠습니다. C#을 사용하여 Word 파일에서 빈 페이지를 제거하려면 C#을 사용하여 Word에서 빈 페이지를 제거하는 방법 문서를 참조하세요.