I´m trying to replace Images in existing Pdf-documents.
I´ve tried using the PDFium Component Suite for FireMonkey.
With the following code i´ve managed to insert an image via the AddPicture
-function. Unfortunately i can´t find a way to get the position of the existing image. Unfortunately the record TPdfImage
only has the properties Width
, Height
and Data
. Does someone have experience with those Components or done anything like this already?
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
lImage: TPdfImage;
begin
FPdf1.FileName := 'C:\Path_To_Pdf.pdf';
FPdf1.PageNumber := 1;
FPdf1.Active := True;
for I := 0 to FPdf1.ImageCount - 1 do
begin
lImage := FPdf1.Image[I];
end;
FPdf1.AddPicture(ImageControl1.Bitmap, 20, 20, 100, 100);
FPdf1.SaveAs('C:\Path_To_Pdf_Images.pdf');
end;
I´ve found a solution for the problem. With PDFium you can iterate over the Objects in the Pdf and just check if the ObjectType
is otImage
.
With this sample i´m overriding all images in the pdf with the Bitmap
in ImageControl1
. I hope this might help someone else in the future.
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
lImage: TPdfImage;
lPdfRectangle: TPdfRectangle;
lBitmap: TBitmap;
begin
FPdf1.FileName := '..\..\Test.pdf';
FPdf1.PageNumber := 1;
FPdf1.Active := True;
for I := 0 to FPdf1.ImageCount - 1 do
begin
lImage := FPdf1.Image[I];
end;
for I := 0 to FPdf1.ObjectCount - 1 do
begin
if FPdf1.ObjectType[I] = TObjectType.otImage then
begin
lPdfRectangle := FPdf1.ObjectBounds[I];
lBitmap := FPdf1.ObjectBitmap[I];
FPdf1.AddPicture(ImageControl1.Bitmap, lPdfRectangle.Left, lPdfRectangle.Bottom,
lPdfRectangle.Right - lPdfRectangle.Left, lPdfRectangle.Top - lPdfRectangle.Bottom);
end;
end;
FPdf1.SaveAs('..\..\Test_replaced.pdf');
end;