Frequently Asked Question:
Add an image watermark to a PDF
Question
How do I apply an image watermark to a PDF using Quick PDF Library?
Answer
Yes, Quick PDF Library can add an image watermark to a PDF programmatically. I've included some Delphi sample code below that demonstrates how to do this.
This sample code will apply a watermark to every page in the selected PDF. The location of the watermark can easily be changed by updating the co-ordinates for the DrawImage function.
procedure TForm2.btnImageWatermarkClick(Sender: TObject);
var
PDFOriginal: Integer;
ImagePath: AnsiString;
WatermarkID: Integer;
PageHeight: Double;
PageWidth: Double;
OutputPath: AnsiString;
I: Integer;
begin
QP := TQuickPDF0718.Create;
try
// Insert trial or paid license key here
UnlockResult := QP.UnlockKey('..Insert License Key Here...');
if UnlockResult = 1 then
begin
// Load the PDF that you want to watermark
if dlgOpen.Execute then
begin
QP.LoadFromFile(dlgOpen.FileName);
PDFOriginal := QP.SelectedDocument();
end;
// Load the image that you want to use as the watermark
if dlgOpen.Execute then
begin
ImagePath := dlgOpen.FileName;
end;
// This is setup for use with PNG images.
// Check out AddImageFromFile function in the
// reference guide for info on other image types.
WatermarkID := QP.AddImageFromFile(ImagePath, 0);
// Loop through each page in the document
// and apply the watermark.
for I := 1 to QP.PageCount() - 1 do
begin
QP.SelectPage(I);
PageHeight := QP.PageHeight();
PageWidth := QP.PageWidth();
QP.SelectImage(WatermarkID);
QP.SetOrigin(1);
QP.DrawImage(100, 100, QP.ImageWidth(), QP.ImageHeight());
end;
// Save the watermarked PDF to disk
if dlgSave.Execute then
begin
OutputPath := dlgSave.FileName;
end;
QP.SaveToFile(OutputPath);
end;
finally
QP.Free;
end;
end;