Recent

Author Topic: [SOLVED] add an image in an existing pdf  (Read 2457 times)

scons

  • Full Member
  • ***
  • Posts: 141
[SOLVED] add an image in an existing pdf
« on: July 16, 2018, 05:36:31 pm »
Hi all,

I want to add an image (bmp) into an existing pdf.

Simple approach, add the image to the pdf with x and y coördinates.

Some searching gave the impression that it can be done with imagemagick. But the imagemagick version in the fpc folder seems to be kinda outdated (?).
Is this still the way to go or are there other alternatives which are more up-to-date ?

Any thoughts on this ?

Thanks
« Last Edit: July 19, 2018, 01:16:44 pm by scons »
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

scons

  • Full Member
  • ***
  • Posts: 141
Re: [SOLVED] add an image in an existing pdf
« Reply #1 on: July 19, 2018, 01:26:44 pm »
okay, I found a way (quick and dirty !) for my W10 installation.

ImageMagick should be installed, and add BGRABitmapPack as a requirement.

Code: Pascal  [Select][+][-]
  1. program imagemagickaddimage;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses {$IFDEF UNIX} {$IFDEF UseCThreads}
  6.   cthreads, {$ENDIF} {$ENDIF}
  7.   Classes,
  8.   SysUtils,
  9.   Process,
  10.   BGRABitmap,
  11.   BGRABitmapTypes { you can add units after this };
  12.  
  13. var
  14.   AProcess: TProcess;
  15.   AStringList, Temp: TStrings;
  16.   x1, xval, yval: integer;
  17.   result1, result2: string;
  18.   bmp1, bmp2: TBGRABitmap;
  19.  
  20. begin
  21.   AProcess := TProcess.Create(nil);
  22.   try
  23.     AProcess.Executable := 'magick';
  24.     AProcess.Parameters.Add('convert');
  25.     AProcess.Parameters.Add('-density');
  26.     AProcess.Parameters.Add('300');
  27.     AProcess.Parameters.Add('D20.pdf');
  28.     AProcess.Parameters.Add('-page');
  29.     AProcess.Parameters.Add('A4');
  30.     AProcess.Parameters.Add('D20.png');
  31.     AProcess.Options := AProcess.Options + [poWaitOnExit];
  32.     AProcess.Execute;
  33.   finally
  34.     AProcess.Free;
  35.   end;
  36.  
  37.   AProcess := TProcess.Create(nil);
  38.   try
  39.     AProcess.Executable := 'magick';
  40.     AProcess.Parameters.Add('identify');
  41.     AProcess.Parameters.Add('D20.png');
  42.     AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  43.     AProcess.Execute;
  44.  
  45.     Temp := TStringList.Create;
  46.     AStringList := TStringList.Create;
  47.     AStringList.Delimiter := ' ';
  48.     AStringList.StrictDelimiter:= True;
  49.     AStringList.LoadFromStream(AProcess.Output);
  50.  
  51.     //AStringList.SaveToFile('output.txt');
  52.     Temp.CommaText:= AStringList[0];
  53.     x1      := Pos('x', Temp[2]);
  54.     result1 := Copy(Temp[2], 0, x1 - 1);
  55.     result2 := Copy(Temp[2], x1 + 1, length(AStringList[0]));
  56.  
  57.     //writeln(Temp[2]);
  58.     //writeln(result1);
  59.     //writeln(result2);
  60.   finally
  61.     Temp.Free;
  62.     AStringList.Free;
  63.     AProcess.Free;
  64.   end;
  65.  
  66.   xval := StrToInt(result1);
  67.   yval := StrToInt(result2);
  68.  
  69.   xval := trunc(xval - xval+(50/0.264583)); // needs optimisation for shift X/Y in mm + DPI
  70.   yval := trunc(yval -  250-(50/0.264583)); // needs optimisation for shift X/Y in mm + DPI
  71.  
  72.   bmp1 := TBGRABitmap.Create;
  73.   bmp2 := TBGRABitmap.Create;
  74.   try
  75.     bmp1.LoadFromFile('D:\imagemagick2\D20.png');
  76.     bmp2.LoadFromFile('D:\imagemagick2\1002.bmp');  // this image will be added
  77.     bmp1.PutImage(xval, yval, bmp2, dmSet);
  78.     bmp1.SaveToFile('test.png');
  79.   finally
  80.     bmp1.Free;
  81.     bmp2.Free;
  82.   end;
  83.  
  84.   AProcess := TProcess.Create(nil);
  85.   try
  86.     AProcess.Executable := 'magick';
  87.     AProcess.Parameters.Add('convert');
  88.     AProcess.Parameters.Add('-density');
  89.     AProcess.Parameters.Add('300');
  90.     AProcess.Parameters.Add('test.png');
  91.     AProcess.Parameters.Add('test.pdf');
  92.     AProcess.Options := AProcess.Options + [poWaitOnExit];
  93.     AProcess.Execute;
  94.   finally
  95.     AProcess.Free;
  96.   end;
  97. end.
  98.  

Of course this is not the best and most elegant way (and badly coded), but I needed to move forward.
If I find some time in the near future I will try to optimize this.

Cheers
Windows 10-64bit Lazarus 2.0.12 + FPC 3.2.0

 

TinyPortal © 2005-2018