Come convertire PUB in PNG in C#

In questo argomento passo passo imparerai come convertire PUB in PNG in C#. La necessità di convertire il file PUB nel formato PNG in C# si verifica quando devi mostrare le pagine di un file PUB come immagini nella tua applicazione. Il tutorial ti aiuta a raggiungere questo obiettivo con poche righe di codice C#.

Passaggi per convertire PUB in PNG in C#

  1. Installa i pacchetti Aspose.PUB for .NET e Aspose.PDF for .NET da NuGet.org
  2. Fare riferimento a quattro spazi dei nomi richiesti per far funzionare il codice
  3. Imposta la licenza per entrambe le API separatamente usando il metodo SetLicense
  4. Crea un parser per il file PUB utilizzando PubFactory class
  5. Carica il file PUB analizzato in un Document object
  6. Salva il file PDF intermedio utilizzando IPdfConverter interface
  7. Crea un nuovo PDF Document object
  8. Scorri tutte le pagine del file PDF
  9. Salva ogni pagina PDF come immagine PNG utilizzando PngDevice object

Nei passaggi precedenti, abbiamo utilizzato Aspose.PUB per .NET e Aspose.PDF per .NET API per convertire prima un file PUB in un formato di file PDF intermedio, quindi convertire quel file PDF intermedio in immagini PNG.

Codice per convertire PUB in PNG in 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");
}
}
}
}

Il codice precedente aiuta a convert PUB in PNG nel codice C# con pochi passaggi. Ogni singola pagina del file PDF viene salvata come immagine PNG separata. Usando questo codice e seguendo i passaggi, puoi creare il tuo convertitore PUB in PNG C# nelle tue applicazioni .NET inclusi Windows, Web, Desktop o Servizi ecc. Tieni presente che ciò non richiede l’installazione di Microsoft Publisher sul tuo computer o server .

 Italiano