이 튜토리얼에서는 C++를 사용하여 **PUB)를 PNG로 변환하는 방법을 이해합니다. 변환이 두 단계로 수행됨을 알 수 있습니다. 먼저 PUB 파일을 PDF 파일로 변환한 다음 간단한 API 호출로 C++를 사용하여 중간 PDF 파일을 PNG 형식으로 렌더링합니다.
C++를 사용하여 PUB를 PNG로 변환하는 단계
- NuGet 패키지 관리자 도구에서 Aspose.PUB for C++ 및 Aspose.PDF for C++ 설치
- Aspose::Pub 및 Aspose::Pdf 네임스페이스에 대한 참조 추가
- Document 클래스 인스턴스로 입력 PUB 파일 로드
- ConvertToPdf 메서드를 사용하여 중간 PDF 파일 만들기
- 각 페이지를 반복하고 출력 PNG 이미지 생성
몇 줄의 코드로 간단한 API 호출로 C++*를 사용하여 *PUB를 PNG로 효율적으로 내보낼 수 있습니다. 파일 경로를 지정하고 메서드를 호출하기만 하면 API가 변환 프로세스의 모든 사소한 세부 사항을 자체적으로 고려합니다. 처리 후 출력 PNG 이미지는 요구 사항에 따라 저장됩니다.
C++를 사용하여 PUB를 PNG로 변환하는 코드
#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(); | |
} | |
} | |
}; |
이전 주제에서 C++에서 PDF 파일을 읽는 방법에 대해 배웠습니다. 반면 이 기사에서는 C++를 사용하여 PUB 파일에서 PNG를 만드는 방법에 대해 논의했습니다.