Developer Tools
|
Office Productivity Applications
|
Enterprise Solutions
|
|||||||||||||||||||||||||||
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;
| Privacy | Legal | Feedback | Newsletter | © 2002-2010 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 users are advised to use Microsoft ClearType Tuning for optimal experience. Linux and other users can enable font smoothing, as supported by their OS. Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.