#1 2021-01-01 13:40:07

liam1983
Member
Registered: 2021-01-01
Posts: 10

PDF as Background Image

Hi,

I found a way to make Pictures as a Background or Watermark Image, but how to do this with a PDF?

Offline

#2 2021-01-01 17:11:26

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,081
Website

Re: PDF as Background Image

Draw first the picture, then write the text and the main content over it.

Offline

#3 2021-01-05 10:17:29

liam1983
Member
Registered: 2021-01-01
Posts: 10

Re: PDF as Background Image

Sorry here is a better description of my problem.

I export a Quickreport file to PDF.
          Pdf := TPdfDocument.Create();
          try
            Pdf.DefaultPaperSize := psA4;
            Pdf.UseUniscribe := False;
            Pdf.EmbeddedTTF := true;
            Pdf.EmbeddedWholeTTF := False;
            Pdf.UseOptionalContent := True;
            Pdf.StandardFontsReplace := False;
            Pdf.CompressionMethod := cmFlateDecode;
//            pic := TPicture.Create;
//            pic.LoadFromFile('C:\tmp\img_snow.jpg');
//            background := TPdfImage.Create(PDF, pic.Graphic, true);
//            PDF.AddXObject('BackgroundImage',background);
            TDesignQuickReport(qrd.ParentControl).Prepare;
            for i := 1 to TDesignQuickReport(qrd.ParentControl).QRPrinter.PageCount do begin
              aMeta := TDesignQuickReport(qrd.ParentControl).QRPrinter.GetPage(i);
              try
                Pdf.DefaultPageWidth := MulDiv(aMeta.Width,72,Pdf.ScreenLogPixels);
                Pdf.DefaultPageHeight := MulDiv(aMeta.Height,72,Pdf.ScreenLogPixels);
                page := Pdf.AddPage;
//              PDF.Canvas.DrawXObject(0,0,page.PageWidth,page.PageHeight,'BackgroundImage');

                Pdf.Canvas.RenderMetaFile(aMeta, 1,0, 0,0, tpExactTextCharacterPositining, 0, 0, tcNeverClip);
              finally
                aMeta.Free;
              end;
            end;
            Pdf.SaveToFile(SaveFileName);

it works with images, but I need to use it with a pdf file

Offline

#4 2021-01-05 10:38:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,081
Website

Re: PDF as Background Image

Embedding PDF is not supported.

Try to convert the input into an EMF, then render it on the page...

Offline

#5 2021-01-05 11:04:39

liam1983
Member
Registered: 2021-01-01
Posts: 10

Re: PDF as Background Image

The same way like the jpg?

Offline

#6 2025-07-09 13:48:03

etwoss
Member
Registered: 2025-07-09
Posts: 2

Re: PDF as Background Image

Hi

Is there no complete example on how to add a watermark? I'm new to SyncPDF

Thanks,

Eric

Offline

#7 Yesterday 16:52:25

rvk
Member
Registered: 2022-04-14
Posts: 139

Re: PDF as Background Image

etwoss wrote:

Is there no complete example on how to add a watermark? I'm new to SyncPDF

(maybe this should have been a separate question...)

What do you mean by watermark? Just text is the easiest. With GDI you can just write the text at an angle with a grayscale.

Small example (I hope this is ok here instead of a external clip-site):
Make sure to call the watermark text first before writing anything else to the page so it appears in the background.

// call with WriteTextAngled(pdf, 100, 600, 'CONFIDENTIAL');

procedure WriteTextAngled(pdf: TPdfDocumentGDI; x, Y: Integer; Text: string);
var
  LogFont: TLogFont;
  Font: HFONT;
  OldFont: HFONT;
  OldBkMode: Integer;
  OldColor: COLORREF;
begin
  FillChar(LogFont, SizeOf(LogFont), 0);
  LogFont.lfFaceName := 'Arial';
  LogFont.lfHeight := -60; // Font size
  LogFont.lfEscapement := 60 { angle } * 10; // Rotation in tenths of a degree
  LogFont.lfOrientation := LogFont.lfEscapement;
  LogFont.lfQuality := ANTIALIASED_QUALITY;
  Font := CreateFontIndirect(LogFont);
  OldFont := SelectObject(pdf.VCLCanvas.Handle, Font);
  OldBkMode := SetBkMode(pdf.VCLCanvas.Handle, TRANSPARENT);
  OldColor := SetTextColor(pdf.VCLCanvas.Handle, RGB(180, 180, 180));
  pdf.VCLCanvas.TextOut(x, Y, Text);
  SelectObject(pdf.VCLCanvas.Handle, OldFont);
  SetBkMode(pdf.VCLCanvas.Handle, OldBkMode);
  SetTextColor(pdf.VCLCanvas.Handle, OldColor);
  DeleteObject(Font);
end;

2025-07-11-18-48-21.png

Offline

Board footer

Powered by FluxBB