C Requires Chilkat v11.7.0+
C
Add a CONFIDENTIAL Watermark to Every Page of a PDF
See more PDF Watermark Examples
Demonstrates the simplest watermark: a line of text across every page. "rotation" set to diagonal runs the text from the bottom-left corner to the top-right, computed for each page size, and "scale" with fit page sizes it to fill 80 percent of the page. The watermark is stored as a marked artifact by default, which means viewers show it, text extraction leaves it out, and it can be found and removed again later. Nothing is written until WriteFile is called.Chilkat C Downloads
#include <C_CkPdf.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdf pdf;
HCkJsonObject json;
HCkJsonObject 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 is available at long_report.pdf
pdf = CkPdf_Create();
success = CkPdf_LoadFile(pdf,"qa_data/pdf/long_report.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
return;
}
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"type","text");
CkJsonObject_UpdateString(json,"text","CONFIDENTIAL");
// An id of your own choosing, so that the watermark can be found and removed later. One is
// generated when you do not give one.
CkJsonObject_UpdateString(json,"id","confidential");
CkJsonObject_UpdateString(json,"color","#C00000");
CkJsonObject_UpdateNumber(json,"opacity","0.25");
CkJsonObject_UpdateString(json,"rotation","diagonal");
CkJsonObject_UpdateString(json,"scale.fit","page");
CkJsonObject_UpdateNumber(json,"scale.fitPercent","80");
jsonOut = CkJsonObject_Create();
success = CkPdf_Watermark(pdf,"add",json,jsonOut);
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
// The id, and the pages it was stamped on. "pages" defaults to all.
printf("id: %s\n",CkJsonObject_stringOf(jsonOut,"id"));
printf("pages: %s\n",CkJsonObject_stringOf(jsonOut,"pages"));
success = CkPdf_WriteFile(pdf,"qa_output/report_confidential.pdf");
if (success == FALSE) {
printf("%s\n",CkPdf_lastErrorText(pdf));
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
return;
}
// Sample output:
// id: confidential
// pages: 1-3
CkPdf_Dispose(pdf);
CkJsonObject_Dispose(json);
CkJsonObject_Dispose(jsonOut);
}