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 .NET Developer Tools
XtremeDocumentStudio .NET

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

PDFOne .NET

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

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our Java developer tools
XtremeDocumentStudio (for Java)

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

PDFOne (for Java)

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

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

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