Unicode C Requires Chilkat v11.7.0+
Unicode C
Add an Image (Logo) Watermark to a PDF
See more PDF Watermark Examples
Demonstrates an image watermark placed in a corner of every page. The image is a PNG or JPEG given either by path ("image.path") or, as here, as base64 in the JSON itself ("image.base64"), which suits a server that keeps its logo in a database rather than on disk. PNG transparency is kept. Giving only a width keeps the aspect ratio, and the units member says those numbers are inches rather than points.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 is available at four_pages.pdf
pdf = CkPdfW_Create();
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/four_pages.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"type",L"image");
// A small PNG logo, base64 encoded. Use "image.path" instead to name a PNG or JPEG file:
// ignore = json.UpdateString("image.path","logo.png");
CkJsonObjectW_UpdateString(json,L"image.base64",L"iVBORw0KGgoAAAANSUhEUgAAAGAAAAAgCAYAAADtwH1UAAAApElEQVR42u3a7QmAIBgAYTdpo/ZphhZti6IBylLrlXoO/G3cIeFHSgAAFDKM08rCy8Jzg6Ug8UJ0JF+EDuSXRGgxX8+jufxlTk0nFuCCiF360aidXICMiDP5uQgCXAxQI782ggAHEu7IP4sgQOcBIMA35ItgBQhQEqBmtbX6xl//hAUI3gcIELwTFiD4LEiA4NNQAYLvA+BGzJ0wvIrwLggA8DgbwPbrr8c7+lkAAAAASUVORK5CYII=");
// The size to draw it at. Give only the width and the height follows the aspect ratio.
CkJsonObjectW_UpdateString(json,L"units",L"in");
CkJsonObjectW_UpdateNumber(json,L"image.width",L"1.5");
// Bottom-right, half an inch in from the page edges.
CkJsonObjectW_UpdateString(json,L"position.align",L"bottomRight");
CkJsonObjectW_UpdateNumber(json,L"position.margin",L"0.5");
CkJsonObjectW_UpdateNumber(json,L"opacity",L"0.85");
CkJsonObjectW_UpdateString(json,L"id",L"logo");
jsonOut = CkJsonObjectW_Create();
success = CkPdfW_Watermark(pdf,L"add",json,jsonOut);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
wprintf(L"id: %s pages: %s\n",CkJsonObjectW_stringOf(jsonOut,L"id"),CkJsonObjectW_stringOf(jsonOut,L"pages"));
success = CkPdfW_WriteFile(pdf,L"qa_output/four_pages_logo.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
return;
}
// Sample output:
// id: logo pages: 1-4
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkJsonObjectW_Dispose(jsonOut);
}