PDFtoolkit VCL
Edit, enhance, secure, merge, split, view, print PDF and AcroForms documents
Compatibility
Delphi C++Builder

Extract PDF Text and Images, and Add Them As PDF Watermarks

“I would like to extract text and images from a page in a PDF file and add it to another PDF file as watermarks.”
By Shekhar Kumar Keshri & Shine Babu

Extracting text and images from one PDF file and inserting them as watermark in another PDF file is not a difficult task but certainly a novel one. Here is how we attempted to solve our user's query.

The GetPageElements method is used to extract page elements such as text, images, path objects (lines, rectangles, curves, etc.), and form field objects from a specified page in a PDF file. Using this, you can choose to extract text and image elements.

First, drag and drop two TgtPDFDocument components on your form.

object PDFDoc1: TgtPDFDocument
object PDFDoc2: TgtPDFDocument

Next, load the document containing the images and text that needs to be extracted in the the first TgtPDFDocument object. Load the document to which the text and images are to be added as watermarks in the second TgtPDFDocument object.

PDFDoc1.LoadFromFile('Input.pdf');
PDFDoc2.LoadFromFile('Output.pdf');

Now, you can start extracting the text and image elements using the GetPageElements method.

begin
  try
    PDFDoc1.MeasurementUnit := muPixels;
    PDFDoc2.MeasurementUnit := muPixels;
    LPageList :=
         PDFDoc1.GetPageElements(
                           LPageNo,[etText, etImage],muPixels);
    for LI := 0 to LPageList.Count-1 do
    begin
      if LPageList.Items[LI] is TgtPDFTextElement then
      begin
        AddTextWatermark(
                 TgtPDFTextElement(LPageList.Items[LI]));
      end
      else if LPageList.Items[LI] is TgtPDFImageElement then
      begin
        AddImageWatermark(
                TgtPDFImageElement(LPageList.Items[LI]));
      end;
    end;
    PDFDoc2.SaveToFile('Output.pdf');
  finally
    if LPageList <> nil then
      LPageList.free;
  end;
end;

The AddTextWatermark and AddImageWatermark routines are listed below. Basically, their job is to take the supplied text or image element and add it to the second TgtPDFDocument object using the InsertWatermark method.

procedure TTfrmWatermark.AddTextWatermark(
                           ATextElement: TgtPDFTextElement);
var
  Watermark: TgtTextWatermarkTemplate;
begin
  Watermark := TgtTextWatermarkTemplate.Create;
  with Watermark do
	begin
    Text := ATextElement.Text;
    Font.Name := ATextElement.Font.Name;
    Font.Color :=  ATextElement.TextColor;
    Font.Size := ATextElement.Font.Size;
    Font.Style := ATextElement.Font.Style;
    StrokeColor := ATextElement.Font.Color;
    Overlay := True;
    HorizPos := hpCustom;
    VertPos := vpCustom;
    X:=ATextElement.XCordOrigin;
    Y:=ATextElement.YCordOrigin;
  end;
  PDFDoc2.InsertWatermark(Watermark);
  Watermark.Free;
end;



procedure TTfrmWatermark.AddImageWatermark(
                            AImageElement: TgtPDFImageElement);
var
  Watermark: TgtImageWatermarkTemplate;
  LBitmap: TBitmap;
begin
  LBitmap := TBitmap.Create;
  LBitmap.Assign(
        AImageElement.ImageItemList.Items[
                     AImageElement.ImageIndex].Image);
  Watermark := TgtImageWatermarkTemplate.Create;
  With Watermark do
  begin
    Image := LBitmap;
    Overlay := True;
    HorizPos :=hpCustom;
    VertPos := vpCustom;
    X := Round(AImageElement.XCordOrigin);
    Y := Round(AImageElement.YCordOrigin);
  end;
  PDFDoc2.InsertWatermark(Watermark);
  FreeAndNil(LBitmap);
  FreeAndNil(Watermark);
end;

---o0O0o---

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).