이 단계별 주제에서는 C#에서 PUB를 PNG로 변환하는 방법을 배웁니다. PUB 파일을 C#에서 PNG 형식으로 변환해야 하는 것은 애플리케이션에서 PUB 파일의 페이지를 이미지로 표시해야 할 때 발생합니다. 이 자습서는 몇 줄의 C# 코드로 이를 달성하는 데 도움이 됩니다.
C#에서 PUB를 PNG로 변환하는 단계
- NuGet.org에서 Aspose.PUB for .NET 및 Aspose.PDF for .NET 패키지 설치
- 코드가 작동하도록 4개의 필수 네임스페이스 참조
- SetLicense 메서드를 사용하여 두 API에 대한 라이선스를 별도로 설정
- PubFactory class를 사용하여 PUB 파일용 파서 생성
- 파싱된 PUB 파일을 Document object에 로드
- IPdfConverter interface를 사용하여 중간 PDF 파일 저장
- 새 PDF 만들기 Document object
- PDF 파일의 모든 페이지 반복
- PngDevice object를 사용하여 각 PDF 페이지를 PNG 이미지로 저장
위 단계에서 .NET용 Aspose.PUB 및 .NET API용 Aspose.PDF를 사용하여 먼저 PUB 파일을 중간 PDF 파일 형식으로 변환한 다음 해당 중간 PDF 파일을 PNG 이미지로 변환합니다.
C#에서 PUB를 PNG로 변환하는 코드
using System; | |
//Add reference to Aspose.PUB for .NET API | |
//Use following namespaces to convert PUB to PNG image format | |
using Aspose.Pub; | |
using PDF = Aspose.Pdf; | |
using Aspose.Pdf.Facades; | |
using Aspose.Pdf.Devices; | |
namespace ConvertPUBToPNG | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before converting PUB to PNG format | |
//using Aspose.PUB for .NET | |
Aspose.Pub.License AsposePUBLicense = new Aspose.Pub.License(); | |
AsposePUBLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
Aspose.Pdf.License AsposePDFLicense = new Aspose.Pdf.License(); | |
AsposePDFLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Load a parsed version of Pub file to Document object | |
IPubParser PubFileParser = PubFactory.CreateParser("InputPUBFileToConvert.pub"); | |
Document PubDocument = PubFileParser.Parse(); | |
//convert to PDF using PDFConvert object | |
IPdfConverter PDFConverter = PubFactory.CreatePdfConverter(); | |
PDFConverter.ConvertToPdf(PubDocument, "IntermediatePDFFile.pdf"); | |
//create a PDF document | |
PDF.Document PDFDocument = new PDF.Document("IntermediatePDFFile.pdf"); | |
PdfFileInfo PDFFileInfo = new PdfFileInfo(PDFDocument); | |
//loop through each page and save it as PNG | |
foreach (PDF.Page PDFPage in PDFDocument.Pages) | |
{ | |
PDF.PageSize PDFPageSize = new PDF.PageSize( | |
Convert.ToInt32(PDFFileInfo.GetPageWidth(PDFPage.Number)), | |
Convert.ToInt32(PDFFileInfo.GetPageHeight(PDFPage.Number))); | |
PDF.Devices.PngDevice PNGDevice = new PDF.Devices.PngDevice(PDFPageSize); | |
PNGDevice.Process(PDFPage, "Page" + PDFPage.Number + ".png"); | |
} | |
} | |
} | |
} |
위의 코드는 몇 단계를 거쳐 C# 코드에서 PNG를 convert PUB하는 데 도움이 됩니다. PDF 파일의 각 개별 페이지는 별도의 PNG 이미지로 저장됩니다. 이 코드를 사용하고 단계를 따르면 Windows, 웹, 데스크톱 또는 서비스 등을 포함한 .NET 응용 프로그램에서 고유한 PUB에서 PNG C# 변환기를 만들 수 있습니다. 이 작업을 수행하려면 컴퓨터나 서버에 Microsoft Publisher를 설치할 필요가 없습니다. .