Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
This is a simple C++ code sample to extract text from PDF files using COM Interop using early binding. Before running the sample pay attention that
// Cross-referenced type libraries:
and has one or more #import’s. Just copy them into your code before importing the main type library and do it in the same order. Thus you’ll bite the first type of problem. The next type of problem comes from the fact C++ environment has a big number of macros, predefined functions, etc., which can conflict with type library methods. For example, GetType has been already widely used in C++ but also Aspose.PDF has it. I found rename and auto_rename attributes of #import directive are very convenient to get rid of possible warnings and errors.
For details please look at this post.
C++ example
#include "stdafx.h"
#import "C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.tlb"
#import "C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb" auto_rename
#import "C:\Temp\Aspose.PDF.tlb" rename("GetType", "GetType_") auto_rename
using namespace System;
String ^earlyBinding(String ^file)
{
String ^text;
// Create ComHelper
Aspose_Pdf::_ComHelperPtr comHelperPtr;
HRESULT hr = comHelperPtr.CreateInstance(__uuidof(Aspose_Pdf::ComHelper));
if (FAILED(hr))
{
Console::WriteLine(L"Error occured");
}
else
{
// Set license
Aspose_Pdf::_LicensePtr licPtr;
licPtr.CreateInstance(__uuidof(Aspose_Pdf::License));
licPtr->SetLicense("C:\\Temp\\Aspose.PDF.lic");
licPtr.Release();
// Get Document
Aspose_Pdf::_DocumentPtr docPtr;
docPtr = comHelperPtr->OpenFile((BSTR)System::Runtime::InteropServices::Marshal::StringToBSTR(file).ToPointer());
comHelperPtr.Release();
// Create Absorber
Aspose_Pdf::_TextAbsorberPtr absorberPtr;
HRESULT hRes = absorberPtr.CreateInstance(__uuidof(Aspose_Pdf::TextAbsorber));
// Browse text
docPtr->GetPages()->Accept_4(absorberPtr);
// Retrieve text
BSTR extractedText = absorberPtr->GetText();
text = gcnew String(extractedText);
docPtr.Release();
absorberPtr.Release();
}
return text;
}
int main(array<System::String ^> ^args)
{
CoInitialize(NULL);
if (args->Length != 1)
{
Console::WriteLine("Missing parameters\nUsage:testCOM <pdf file>");
return 0;
}
String ^text = earlyBinding(args[0]);
CoUninitialize();
Console::WriteLine("Extracted text:");
Console::WriteLine("---\n{0}", text != nullptr ? text->Trim() : "<empty>");
Console::WriteLine("---");
return 0;
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.