如何使用 C++ 将 PUB 转换为 PNG

在本教程中,我们将了解如何使用 C++** 将 PUB) 转换为 PNG。您会注意到转换分两个步骤进行。首先,将 PUB 文件转换为 PDF 文件,然后使用 C++ 通过简单的 API 调用将中间 PDF 文件渲染为 PNG 格式。

使用 C++ 将 PUB 转换为 PNG 的步骤

  1. 从 NuGet 包管理器工具安装 Aspose.PUB for C++Aspose.PDF for C++
  2. 添加对 Aspose::PubAspose::Pdf 命名空间的引用
  3. 使用 Document 类实例加载输入 PUB 文件
  4. 使用 ConvertToPdf 方法创建中间 PDF 文件
  5. 迭代每个页面并创建输出 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。

 简体中文