使用 C++ 将 PUB 转换为图像

Microsoft Publisher (PUB) 文件用于打印或发布信息。在某些情况下,您需要共享这些文件,而接收方无权访问 Microsoft Publisher。在这种情况下,在共享之前将这些文件转换为图像可能会很有帮助。为此,本文将教您如何使用 C++ 将 PUB 文件转换为各种图像格式。

用于将 PUB 文件转换为图像格式的 C++ API

我们将使用 Aspose.PUB for C++Aspose.PDF for C++ API 来执行此转换。前者是用于处理 Microsoft Publisher (PUB) 文件的库,而后者是用于创建、阅读和修改 PDF 文件的库。我们将使用 Aspose.PUB for C++ API 将 PUB 文件转换为 PDF 格式,使用 Aspose.PDF for C++ API 将生成的 PDF 文件转换为图像格式。您可以通过 NuGet 安装 API,也可以直接从 下载 部分下载它们。

PM> Install-Package Aspose.PUB.Cpp
PM> Install-Package Aspose.PDF.Cpp

使用 C++ 将 PUB 文件转换为 JPG 图像

以下是将 PUB 文件转换为 JPG 图像的步骤。

以下示例代码展示了如何使用 C++ 将 PUB 文件转换为 JPG 图像。

// 源 PUB 和输出 PDF 文件路径
System::String filePub = u"SourceDirectory\\1.pub";
System::String filePdf = u"OutputDirectory\\1.pdf";

// 加载 PUB 文件
System::SharedPtr<IPubParser> parser = PubFactory::CreateParser(filePub);
System::SharedPtr<Aspose::Pub::Document> document = parser->Parse();

// 将 PUB 文件转换为 PDF
PubFactory::CreatePdfConverter()->ConvertToPdf(document, filePdf);

// 加载生成的 PDF 文件
auto pdfDocument = MakeObject<Aspose::Pdf::Document>(filePdf);
auto info = MakeObject<Facades::PdfFileInfo>(pdfDocument);

// 遍历 PDF 页面
for (auto page : pdfDocument->get_Pages())
{
	// 获取 PDF 页面的尺寸
	int width = info->GetPageWidth(page->get_Number());
	int height = info->GetPageHeight(page->get_Number());

	// 创建 Resolution 类的实例
	auto resolution = MakeObject<Devices::Resolution>(300);

	// 创建具有指定宽度、高度和分辨率的 JPEG 设备
	auto device = MakeObject<Devices::JpegDevice>(width, height, resolution);

	// 为输出图像创建文件流
	System::SharedPtr<System::IO::FileStream> imageStream = System::IO::File::Create(String::Format(u"OutputDirectory\\page_{0}.jpg", page->get_Number()));

	// 将 PDF 页面转换为 JPG 图像
	device->Process(page, imageStream);

	// 关闭流
	imageStream->Close();
}

使用 C++ 将 PUB 文件转换为 PNG 图像

要将 PUB 文件转换为 PNG 图像,请按照以下步骤操作。

以下示例代码展示了如何使用 C++ 将 PUB 文件转换为 PNG 图像。

// 源 PUB 和输出 PDF 文件路径
System::String filePub = u"SourceDirectory\\1.pub";
System::String filePdf = u"OutputDirectory\\1.pdf";

// 加载 PUB 文件
System::SharedPtr<IPubParser> parser = PubFactory::CreateParser(filePub);
System::SharedPtr<Aspose::Pub::Document> document = parser->Parse();

// 将 PUB 文件转换为 PDF
PubFactory::CreatePdfConverter()->ConvertToPdf(document, filePdf);

// 加载生成的 PDF 文件
auto pdfDocument = MakeObject<Aspose::Pdf::Document>(filePdf);
auto info = MakeObject<Facades::PdfFileInfo>(pdfDocument);

// 遍历 PDF 页面
for (auto page : pdfDocument->get_Pages())
{
	// 获取 PDF 页面的尺寸
	int width = info->GetPageWidth(page->get_Number());
	int height = info->GetPageHeight(page->get_Number());

	// 创建 Resolution 类的实例
	auto resolution = MakeObject<Devices::Resolution>(300);

	// 创建具有指定宽度、高度和分辨率的 PNG 设备
	auto device = MakeObject<Devices::PngDevice>(width, height, resolution);

	// 为输出图像创建文件流
	System::SharedPtr<System::IO::FileStream> imageStream = System::IO::File::Create(String::Format(u"OutputDirectory\\page_{0}.png", page->get_Number()));

	// 将 PDF 页面转换为 PNG 图像
	device->Process(page, imageStream);

	// 关闭流
	imageStream->Close();
}

使用 C++ 将 PUB 文件转换为 TIFF 图像

以下是将 PUB 文件转换为 TIFF 图像的步骤。

以下示例代码演示了如何使用 C++ 将 PUB 文件转换为 TIFF 图像。

// 源 PUB 和输出 PDF 文件路径
System::String filePub = u"SourceDirectory\\1.pub";
System::String filePdf = u"OutputDirectory\\1.pdf";

// 加载 PUB 文件
System::SharedPtr<IPubParser> parser = PubFactory::CreateParser(filePub);
System::SharedPtr<Aspose::Pub::Document> document = parser->Parse();

// 将 PUB 文件转换为 PDF
PubFactory::CreatePdfConverter()->ConvertToPdf(document, filePdf);

// 加载生成的 PDF 文件
auto pdfDocument = MakeObject<Aspose::Pdf::Document>(filePdf);
auto info = MakeObject<Facades::PdfFileInfo>(pdfDocument);

// 获取第一个 PDF 页面的尺寸
int width = info->GetPageWidth(1);
int height = info->GetPageHeight(1);

// 创建 Resolution 类的实例
auto resolution = MakeObject<Devices::Resolution>(300);

// 创建 TiffSettings 类的实例并设置所需的设置
auto settings = MakeObject<Devices::TiffSettings>();
settings->set_Compression(Devices::CompressionType::None);
settings->set_Depth(Devices::ColorDepth::Default);

// 创建具有指定宽度、高度、分辨率和 TiffSettings 的 TIFF 设备
auto device = MakeObject<Devices::TiffDevice>(width, height, resolution, settings);

// 为输出图像创建文件流
System::SharedPtr<System::IO::FileStream> imageStream = System::IO::File::Create(u"OutputDirectory\\pdf.tiff");

// 将 PDF 文件转换为 TIFF 图像
device->Process(pdfDocument, 1, 1, imageStream);

// 关闭流
imageStream->Close();

获得免费许可证

您可以通过申请 免费的临时许可证 来试用该 API,而不受评估限制。

结论

在本文中,您学习了如何使用 C++ 将 Microsoft Publisher (PUB) 文件转换为 JPG、PNG 和 TIFF 图像。我们使用 C++ 的 Aspose.PUB 和 C++ API 的 Aspose.PDF 来实现这一点。您可以通过访问它们的官方文档来详细探索这些 API。如有任何问题,请随时通过我们的 免费支持论坛 与我们联系。

也可以看看