Sorry for replying so late, I was away for a couple of days.
About the error I get when using your MetafileOverlay demo, I will email that to you now.
In the meantime I tried to push on with my own prototype and I have a couple of more questions.
I am using Delphi 5 and the following logic to loop through all pages in my PDF to apply a watermark to specified pages:
type
TWatermarkPage = (wpAll, wpFirst, wpLast, wpEven, wpOdd);
// ------------------------------------------------------------
// PageNeedsWatermark determines by page number if a given page is
// supposed to have a watermark applied.
// Returns True if watermark is needed, False otherwise
// ------------------------------------------------------------
function TForm3.PageNeedsWatermark(aSpec: TWatermarkPage; const PageCount, CurrentPage: Integer): Boolean;
var
IsEvenPage: Boolean;
begin
IsEvenPage := CurrentPage mod 2 = 0;
Result := (aSpec = wpAll) or
((aSpec = wpFirst) and (CurrentPage = 1)) or
((aSpec = wpLast) and (CurrentPage = PageCount)) or
((aSpec = wpEven) and IsEvenPage) or
((aSpec = wpOdd) and not IsEvenPage);
end;
// ------------------------------------------------------------
// WatermarkPDFUsingWPViewPDF loops through all of the pages of a PDF
// file and applies a text watermark as needed.
// ------------------------------------------------------------
procedure TForm3.WatermarkPDFUsingWPViewPDF;
var
WPPDF: TWPViewPDF;
T: TPDFDrawObjectRec;
cnt: Integer;
begin
// Get the Watermark text from edit control on screen...
FWatermarkText := ReadWatermarkText;
// Get the transparency from edit control on screen...
FTransparencyPercent := ReadTransparencyPercent;
// Get the angle of rotation from edit control on screen...
FRotationAngle := ReadRotationAngleDetails;
// Get the enumeration for pages to apply watermark to from edit control on screen...
FTargetPages := ReadTargetPagesForWatermark;
// create WPViewPDF instance
WPPDF := TWPViewPDF.Create(Self);
WPPDF.Parent := Self;
// load a PDF file
WPPDF.LoadFromFile(frmTesterMain.InputFile);
// loop through all pages of the PDF
for cnt := 0 to WPPDF.PageCount-1 do
begin
// FTargetPages holds the pages I want to watermark.
// PageNeedsWatermark compares the cur
if PageNeedsWatermark(FTargetPages, WPPDF.PageCount, cnt+1) then
begin
FillChar(T, SizeOf(T), 0);
T.PageNo := cnt;
T.grtyp := 100;
T.ColorText := clRed;
T.Alpha := FTransparencyPercent;
T.Angle := FRotationAngle;
T.ObjectOptions := 64;
T.units_xywh := 10; // 720 dpi
T.x := Round( 2/2.54 * 720); // 2 cm
T.y := Round( 3/2.54 * 720); // 3 cm
T.w := Round( 5/2.54 * 720);
T.h := Round( 1/2.54 * 720);
WPPDF.AddDrawObject(wpAddNow, WideString('TEXTOBJECT'), T, nil, WideString(FWatermarkText));
end;
end;
// red the output file name from edit control on screen...
FOutputFileName := ReadOutputFileName;
// save the watermarked PDF...
WPPDF.Plus.SaveToFile(FOutputFileName);
// Free the WPViewPDF instance...
WPPDF.Free;
end;
Alles anzeigen
My questions:
1. in the above code I am trying to insert a text watermark but I continually get a PDF with a large 3D rectangular image drawn instead.
2. The loop in the code above correctly prevents processing of certain pages of the PDF, which prevents the AddDrawObject method from being called. Despite this, every page of my PDF always gets a watermark. So obviously I am doing something very wrong. I just don't see what.
What do I need to change to the code above to make it print a text watermark and to have it not put the watermark on every page?