Extraia anexos do documento PDF via C++

Como extrair anexos de um PDF programaticamente com C++

Como extrair anexos usando a biblioteca C++

Para extrair anexos em arquivo PDF, usaremos a API Aspose.PDF para C++, que é uma API de manipulação de documentos rica em recursos, poderosa e fácil de usar para a plataforma cpp. Abra o gerenciador de pacotes NuGet, procure por Aspose.pdf e instale. Você também pode usar o seguinte comando no Console do Gerenciador de Pacotes.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Extrair anexos do PDF C++


Você precisa do Aspose.PDF para C++ para testar o código em seu ambiente.

  1. Obtenha a coleção de arquivos incorporados.
  2. Obtenha a contagem dos arquivos incorporados.
  3. Percorra a coleção para obter todos os anexos.
  4. Verifique se o objeto de parâmetro contém os parâmetros.
  5. Obtenha o Anexo e grave em arquivo ou stream.

Extrair anexo de um documento 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;
	}