<< Click to Display Table of Contents >> Navigation: Tasks > Convert PDF into watermark > COMPDF_MakeWatermarkFromPage |
COMPDF_MakeWatermarkFromPage : A page with page number param will be converted to an image object.
The ID is returned and can be used with COMPDF_ImagePrint and also with COMPDF_ApplyWatermark
A customer of WPViewPDF developed this code to load a PDF from a stream and use it as watermark:
(We say many thanks!)
procedure TestApplyWatermarksFromStream(aViewPDF: TWPViewPDF; aStream: TStream);
var xSlWatermarkIDs: TStringlist;
xCountPagesDoc, xCountPagesWatermark, iIndex: Integer;
xCommandResult, xWaterMarkID: Integer;
begin
xSlWatermarkIDs := TStringlist.Create;
Try
aViewPDF.Command(COMPDF_BEGINUPDATE);
xCountPagesDoc := aViewPDF.PageCount;
if (xCountPagesDoc > 0) then
begin
aStream.Position := 0;
aViewPDF.LoadFromStream(aStream, False); // Append stream to aViewPDF
xCountPagesWatermark := aViewPDF.PageCount - xCountPagesDoc;
if (xCountPagesWatermark > 0) then
begin
// Make watermarks
for iIndex := 1 to xCountPagesWatermark do
begin
xWaterMarkID := aViewPDF.Command( COMPDF_MakeWatermarkFromPage,
xCountPagesDoc + iIndex - 1);
xSlWatermarkIDs.AddPair(IntToStr(iIndex), IntToStr(xWaterMarkID));
if (xWaterMarkID <= 0) then
MessageDlg('Watermark page: ' + IntToStr(iIndex)
+ ' - Make watermark failed (' + IntToStr(xWaterMarkID) + ')',
mtError, [mbOK], 0);
end;
// Apply watermarks
for iIndex := 1 to xCountPagesDoc do
begin
xWaterMarkID := StrToIntDef(xSlWatermarkIDs.Values[IntToStr(iIndex)], - 1);
if (xWaterMarkID > 0) then
begin
xCommandResult := aViewPDF.CommandStrEx(
COMPDF_ApplyWatermark, IntToStr(iIndex), xWaterMarkID);
if (xCommandResult <= 0) then
MessageDlg('Page: ' + IntToStr(iIndex) +
' - Apply watermark failed ('
+ IntToStr(xCommandResult) + ')', mtError, [mbOK], 0);
end;
end;
// Delete temporarily added pages (from LoadFromStream)
xCommandResult := aViewPDF.Command( COMPDF_DeletePage, IntToStr(xCountPagesDoc + 1)
+ '-' + IntToStr(xCountPagesDoc + xCountPagesWatermark));
if (xCommandResult <= 0) then
MessageDlg('Delete temporary watermark pages failed ('
+ IntToStr(xCommandResult) + ')', mtError, [mbOK], 0);
end;
end;
Finally
xSlWatermarkIDs.Free;
aViewPDF.Command(COMPDF_ENDUPDATE);
end;
end;