通過C++從PDF文檔中提取附件

如何使用 C++以程式設計方式從 PDF 中提取附件。

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

為了提取PDF檔中的附件,我們將使用[Aspose.PDF for C++](https://products.aspose.com/pdf/cpp)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 cpp 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

從 PDF C++中提取附件


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

  1. 抓取嵌入檔案集合。
  2. 抓取嵌入檔案的計數。 迴圈訪問集合以獲取所有附件。
  3. 檢查參數物件是否包含參數。
  4. 取得附件並寫入檔或流。

將附件提取到 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;
	}