通过 C++ 从 PDF 文档中提取附件

如何使用 C++ 以编程方式从 PDF 中提取附件。

如何使用 C++ 库提取附件

为了提取 PDF 文件中的附件,我们将使用 Aspose.PDF for C++ API,这是一款功能丰富、功能强大且易于使用的适用于 cpp 平台的文档处理 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

从 PDF 中提取附件 C++


你需要 Aspose.PDF for C++ 才能在你的环境中试用代码。

1.获取嵌入式文件集合。 1.获取嵌入文件的计数。 1.循环浏览集合以获取所有附件。 1.检查参数对象是否包含参数。 1.获取附件并写入文件或流。

将附件提取到 PDF 文档。


	// Open document
	auto pdfDocument = new Document(_dataDir + u"GetAlltheAttachments.pdf");

	// Get embedded files collection
	auto embeddedFiles = pdfDocument->get_EmbeddedFiles();

	// Get count of the embedded files
	Console::WriteLine(u"Total files : {0}", embeddedFiles->get_Count());

	int count = 1;

	// Loop through the collection to get all the attachments
	for (auto fileSpecification : embeddedFiles)
	{
	Console::WriteLine(u"Name: {0}", fileSpecification->get_Name());
	Console::WriteLine(u"Description: {0}", fileSpecification->get_Description());
	Console::WriteLine(u"Mime Type: {0}", fileSpecification->get_MIMEType());

	// Check if parameter object contains the parameters
	if (fileSpecification->get_Params() != nullptr)
	{
	Console::WriteLine(u"CheckSum: {0}",
		fileSpecification->get_Params()->get_CheckSum());
	Console::WriteLine(u"Creation Date: {0}",
		fileSpecification->get_Params()->get_CreationDate());
	Console::WriteLine(u"Modification Date: {0}",
		fileSpecification->get_Params()->get_ModDate());
	Console::WriteLine(u"Size: {0}", fileSpecification->get_Params()->get_Size());
	}

	// Get the attachment and write to file or stream
	auto fileContent = MakeArray<uint8_t>(fileSpecification->get_Contents()->get_Length());
	fileSpecification->get_Contents()->Read(fileContent, 0, fileContent->get_Length());
	auto fileStream = System::IO::File::OpenWrite(_dataDir + u"test" + count + u"_out.txt");
	fileStream->Write(fileContent, 0, fileContent->get_Length());
	fileStream->Close();
	count += 1;
	}