ในหัวข้อทีละขั้นตอนนี้ คุณจะได้เรียนรู้วิธีแปลง PUB เป็น PNG ใน C# ความจำเป็นในการแปลงไฟล์ PUB เป็นรูปแบบ PNG ใน C# เกิดขึ้นเมื่อคุณต้องการแสดงหน้าของไฟล์ PUB เป็นรูปภาพในแอปพลิเคชันของคุณ บทช่วยสอนช่วยให้คุณบรรลุเป้าหมายนี้ได้ด้วยโค้ด C# ไม่กี่บรรทัด
ขั้นตอนในการแปลง PUB เป็น PNG ใน C#
- ติดตั้งแพ็คเกจ Aspose.PUB for .NET และ Aspose.PDF for .NET จาก NuGet.org
- อ้างอิงสี่เนมสเปซที่จำเป็นเพื่อให้รหัสทำงานได้
- ตั้งค่าใบอนุญาตสำหรับ API ทั้งสองแยกกันโดยใช้เมธอด SetLicense
- สร้างตัวแยกวิเคราะห์สำหรับไฟล์ PUB โดยใช้ PubFactory class
- โหลดไฟล์ PUB ที่แยกวิเคราะห์ลงใน Document object
- บันทึกไฟล์ PDF ระดับกลางโดยใช้ IPdfConverter interface
- สร้าง PDF ใหม่ Document object
- วนซ้ำทุกหน้าของไฟล์ PDF
- บันทึกหน้า PDF แต่ละหน้าเป็นภาพ PNG โดยใช้ PngDevice object
ในขั้นตอนข้างต้น เราใช้ Aspose.PUB สำหรับ .NET และ Aspose.PDF สำหรับ .NET API เพื่อแปลงไฟล์ PUB เป็นรูปแบบไฟล์ PDF ระดับกลางก่อน จากนั้นจึงแปลงไฟล์ PDF ระดับกลางนั้นเป็นไฟล์ภาพ PNG
รหัสเพื่อแปลง PUB เป็น PNG ใน C
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"); | |
} | |
} | |
} | |
} |
โค้ดด้านบนช่วยให้ convert PUB เป็น PNG ในโค้ด C# ในไม่กี่ขั้นตอน แต่ละหน้าของไฟล์ PDF จะถูกบันทึกเป็นภาพ PNG แยกต่างหาก การใช้รหัสนี้และทำตามขั้นตอนต่างๆ คุณสามารถสร้างตัวแปลง PUB เป็น PNG C# ของคุณเองในแอปพลิเคชัน .NET ของคุณ รวมถึง Windows, เว็บ, เดสก์ท็อป หรือบริการอื่นๆ โปรดทราบว่าการดำเนินการนี้ไม่จำเป็นต้องติดตั้ง Microsoft Publisher บนเครื่องหรือเซิร์ฟเวอร์ของคุณ .