ในบทช่วยสอนนี้ เราจะเข้าใจวิธี แปลง PUB) เป็น PNG โดยใช้ C+* คุณจะสังเกตเห็นว่าการแปลงเกิดขึ้นในสองขั้นตอน ประการแรก ไฟล์ PUB จะถูกแปลงเป็นไฟล์ PDF จากนั้นไฟล์ PDF ระดับกลางจะแสดงผลเป็นรูปแบบ PNG โดยใช้ C++ พร้อมการเรียกใช้ API อย่างง่าย
ขั้นตอนในการแปลง PUB เป็น PNG โดยใช้ C++
- ติดตั้ง Aspose.PUB for C++ และ Aspose.PDF for C++ จาก NuGet package Manager Tool
- เพิ่มการอ้างอิงถึงเนมสเปซ Aspose::Pub และ Aspose::Pdf
- โหลดไฟล์ PUB อินพุตด้วยอินสแตนซ์คลาส Document
- สร้างไฟล์ PDF ระดับกลางด้วยวิธี ConvertToPdf
- วนซ้ำแต่ละหน้าและสร้างอิมเมจ PNG ที่ส่งออก
คุณสามารถ ส่งออก PUB เป็น PNG ได้อย่างมีประสิทธิภาพโดยใช้ C++ ด้วยการเรียก API อย่างง่ายโดยใช้โค้ดไม่กี่บรรทัด คุณต้องระบุพาธของไฟล์และเรียกเมธอด จากนั้น API จะพิจารณารายละเอียดเล็กน้อยทั้งหมดของกระบวนการแปลงด้วยตัวเอง หลังจากประมวลผลแล้ว รูปภาพ PNG ที่ส่งออกจะถูกบันทึกตามความต้องการของคุณ
รหัสเพื่อแปลง PUB เป็น PNG โดยใช้ C ++
#pragma once | |
#include <stdio.h> | |
#include <tchar.h> | |
#include <cstdint> | |
#include <iostream> | |
// TODO: reference additional headers your program requires here | |
#include "system/console.h" | |
#include "system/string.h" | |
#include "system/io/directory.h" | |
#include <Aspose.Pub.Cpp/Properties/AssemblyInfo.h> | |
#include <Aspose.Pub.Cpp/PubFactory.h> | |
#include <Aspose.Pub.Cpp/Document.h> | |
#include <Aspose.Pub.Cpp/Interfaces.h> | |
#include <Aspose.Pub.Cpp/License.h> | |
#include <Aspose.Pub.Cpp/MetaInfo.h> | |
#include <Aspose.PDF.Cpp/Page.h> | |
#include <Aspose.PDF.Cpp/Document.h> | |
#include <Aspose.PDF.Cpp/PageCollection.h> | |
#include <Aspose.PDF.Cpp/Resources.h> | |
#include <Aspose.PDF.Cpp/XImageCollection.h> | |
#include <Aspose.PDF.Cpp/XImage.h> | |
#include <Aspose.PDF.Cpp/License.h> | |
#include <Aspose.PDF.Cpp/License.h> | |
#include <Aspose.PDF.Cpp/Devices/PngDevice.h> | |
#include <Aspose.PDF.Cpp/Devices/Resolution.h> | |
#include <Aspose.PUB.Cpp/License.h> | |
#include <Aspose.PUB.Cpp/PubFactory.h> | |
#include <Aspose.PUB.Cpp/Utils/UtilityObjects.h> | |
#include <system/smart_ptr.h> | |
using namespace System; | |
using namespace Aspose::Pdf; | |
using namespace Aspose::Pub; | |
class ConvertPUBtoPNGEx { | |
public: | |
static void PUBtoPNG() | |
{ | |
// Initialize license object of Aspose.PUB to convert PUB to PDF | |
auto PUBlicense = System::MakeObject<Aspose::Pub::License>(); | |
// Set license | |
PUBlicense->SetLicense(u"Aspose.Total.NET.lic"); | |
// Initialize license object of Aspose.PDF API to convert PDF to PNG | |
auto PDFlicense = System::MakeObject<Aspose::Pdf::License>(); | |
// Set license | |
PDFlicense->SetLicense(u"Aspose.Total.NET.lic"); | |
System::String filePub = u"Sample.pub"; | |
System::String filePdf = u"Test.pdf"; | |
System::SharedPtr<IPubParser> parser = PubFactory::CreateParser(filePub); | |
System::SharedPtr<Aspose::Pub::Document> document = parser->Parse(); | |
PubFactory::CreatePdfConverter()->ConvertToPdf(document, filePdf); | |
// Load the intermediate PDF file created from PUB file | |
System::SharedPtr<Aspose::Pdf::Document> pdfDocument = MakeObject<Aspose::Pdf::Document>(filePdf); | |
// Iterate throught the PDF pages for image conversion | |
for (System::SharedPtr<Page> page : pdfDocument->get_Pages()) | |
{ | |
// Create an instance of the FileStream class for output PNG image | |
System::SharedPtr<System::IO::FileStream> imageStream = System::IO::File::Create(String::Format(u"page_{0}.png", page->get_Number())); | |
// Create an instance of the Resolution class to set resolution of PNG | |
System::SharedPtr<Aspose::Pdf::Devices::Resolution> resolution = MakeObject<Aspose::Pdf::Devices::Resolution>(300); | |
// Create an instance of the PngDevice class to render images | |
System::SharedPtr<Aspose::Pdf::Devices::PngDevice> pngDevice = MakeObject<Aspose::Pdf::Devices::PngDevice>(500, 700, resolution); | |
// Save the page as PNG image output | |
pngDevice->Process(page, imageStream); | |
// Close the stream | |
imageStream->Close(); | |
} | |
} | |
}; |
ในหัวข้อที่แล้ว เราได้เรียนรู้เกี่ยวกับ วิธีอ่านไฟล์ PDF ใน C++ โดยที่ในบทความนี้เราได้กล่าวถึงการใช้ C++ สร้าง PNG จากไฟล์ PUB