Unicode C Requires Chilkat v11.7.0+
Unicode C
Extract Text from a PDF that Forbids Copying
See more PDF Text Extraction Examples
Demonstrates a PDF that opens without a password but whose permissions do not allow copying text. The "text", "markdown", "search" and "tables" operations fail with that reason; "assess" always works. Loading the PDF with the OwnerPassword property set grants owner rights, and extraction works.Chilkat Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
HCkJsonObjectW jsonOut;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The PDF used in this example (owner password: owner-secret; copying not permitted) is available at no_copy_permission.pdf
pdf = CkPdfW_Create();
json = CkJsonObjectW_Create();
jsonOut = CkJsonObjectW_Create();
// The PDF opens without a password.
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/no_copy_permission.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// Extracting text is refused, because the PDF's permissions do not allow copying.
success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
if (success == FALSE) {
wprintf(L"Extraction refused: %s\n",CkPdfW_lastErrorText(pdf));
}
// "assess" reports only counts and ratings, never text, so it is always allowed.
success = CkPdfW_ExtractText(pdf,L"assess",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"rating: %s\n",CkJsonObjectW_stringOf(jsonOut,L"quality.rating"));
// Load with the owner password to get owner rights, which include copying.
CkPdfW_putOwnerPassword(pdf,L"owner-secret");
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/no_copy_permission.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
success = CkPdfW_ExtractText(pdf,L"text",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"%s\n",CkJsonObjectW_stringOf(jsonOut,L"text"));
// Sample output:
// Extraction refused: ChilkatLog:
// ExtractText:
// ...
// The PDF's permissions do not allow copying or extracting text (load it with the owner password to extract).
// ...
// rating: good
// Visitor Leaflet
//
// Welcome to the harbor lighthouse. The tower is open on weekends from July to September.
//
// Please keep to the marked paths and do not climb on the breakwater.
//
// The lantern room is closed in high winds.
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}