Cara Mengonversi PUB ke PNG di C#

Dalam topik langkah demi langkah ini, Anda akan mempelajari cara mengonversi PUB ke PNG di C#. Kebutuhan untuk mengonversi file PUB ke format PNG dalam C# terjadi ketika Anda perlu menampilkan halaman file PUB sebagai gambar dalam aplikasi Anda. Tutorial membantu Anda mencapai ini dengan beberapa baris kode C#.

Langkah-langkah untuk Mengkonversi PUB ke PNG di C#

  1. Instal paket Aspose.PUB for .NET dan Aspose.PDF for .NET dari NuGet.org
  2. Referensi empat ruang nama yang diperlukan untuk membuat kode berfungsi
  3. Tetapkan lisensi untuk kedua API secara terpisah dengan menggunakan metode SetLicense
  4. Buat parser untuk file PUB menggunakan PubFactory class
  5. Muat file PUB yang telah diurai ke dalam Document object
  6. Simpan file PDF perantara menggunakan IPdfConverter interface
  7. Buat PDF baru Document object
  8. Ulangi semua halaman file PDF
  9. Simpan setiap halaman PDF sebagai gambar PNG menggunakan PngDevice object

Pada langkah-langkah di atas, kami telah menggunakan Aspose.PUB untuk .NET, dan Aspose.PDF untuk .NET API untuk pertama-tama mengonversi file PUB menjadi format file PDF perantara, lalu mengonversi file PDF perantara tersebut ke gambar PNG.

Kode untuk Mengonversi PUB ke PNG di 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");
}
}
}
}

Kode di atas membantu convert PUB ke PNG dalam kode C# dengan beberapa langkah. Setiap halaman file PDF disimpan sebagai gambar PNG terpisah. Dengan menggunakan kode ini dan mengikuti langkah-langkahnya, Anda dapat membuat konverter PUB ke PNG C# Anda sendiri di aplikasi .NET Anda termasuk Windows, Web, Desktop, atau Layanan, dll. Perhatikan bahwa ini tidak memerlukan Microsoft Publisher untuk diinstal pada mesin atau server Anda .

 Indonesian