Recent

Author Topic: Bug in procedure Image of the Class TJPFpdf  (Read 2235 times)

menegaz

  • New member
  • *
  • Posts: 7
Bug in procedure Image of the Class TJPFpdf
« on: April 25, 2018, 05:16:08 pm »
I think I found a bug in the Image of class TJPFpdf. When creating a PDF file, which had images both header and body, when inserting the image in the body, it was also placed in the header. I made some changes to the procedure and the error disappeared.

Original procedure:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TJPFpdf.Image(vFile: string; vX: double; vY: double;
  3.   vWidth: double; vHeight: double);
  4. var
  5.   i: integer;
  6.   img: TJPImageInfo;
  7.   flag: boolean;
  8. begin
  9.   //Put an image on the page
  10.   flag := False;
  11.   if (Length(Self.pImages) > 0) then
  12.     for i := 0 to Length(Self.pImages) - 1 do
  13.     begin
  14.       if (Self.pImages[i].filePath = vFile) then
  15.       begin
  16.         flag := True;
  17.         img := Self.pImages[i];
  18.         break;
  19.       end;
  20.     end;
  21.   if not (flag) then
  22.   begin
  23.     //First use of image, get info
  24.     SetLength(Self.pImages, Length(Self.pImages) + 1);
  25.     Self.pImages[Length(Self.pImages) - 1].imgSource := TMemoryStream.Create;
  26.     Self.pImages[Length(Self.pImages) - 1] := GetInfoImage(vFile);
  27.     Self.pImages[Length(Self.pImages) - 1].n := Length(Self.pImages);
  28.     Self.pImages[Length(Self.pImages) - 1].filePath := vFile;
  29.     img := Self.pImages[Length(Self.pImages) - 1];
  30.   end
  31.   else
  32.   //Automatic width or height calculus
  33.   if (vWidth = 0) then
  34.     vWidth := StrToFloat(FloatToStrF((vHeight * img.w / img.h), ffNumber,
  35.       14, 2, TPDFFormatSetings), TPDFFormatSetings);
  36.   if (vHeight = 0) then
  37.     vHeight := StrToFloat(FloatToStrF((vWidth * img.h / img.w), ffNumber,
  38.       14, 2, TPDFFormatSetings), TPDFFormatSetings);
  39.   _out('q ' + FloatToStr(vWidth) + ' 0 0 ' + FloatToStr(vHeight) +
  40.     ' ' + FloatToStr(vX) + ' -' + FloatToStr(vY + vHeight) + ' cm /I' +
  41.     IntToStr(Length(Self.pImages)) + ' Do Q');
  42. end;  
  43.  
  44. //modified procedure:
  45.  
  46. procedure TJPFpdf.Image(vFile: string; vX: double; vY: double;
  47.   vWidth: double; vHeight: double);
  48. var
  49.   j,i: integer;     //modified
  50.   img: TJPImageInfo;
  51.   flag: boolean;
  52. begin
  53.   //Put an image on the page
  54.   flag := False;
  55.   if (Length(Self.pImages) > 0) then
  56.   begin
  57.     for i := 0 to Length(Self.pImages) - 1 do
  58.     begin
  59.       if (Self.pImages[i].filePath = vFile) then
  60.       begin
  61.         flag := True;
  62.         img := Self.pImages[i];
  63.         j:=i+1;                       // insert
  64.         break;
  65.       end;
  66.     end;
  67.   end;
  68.   if not (flag) then
  69.   begin
  70.     //First use of image, get info
  71.     SetLength(Self.pImages, Length(Self.pImages) + 1);
  72.     Self.pImages[Length(Self.pImages) - 1].imgSource := TMemoryStream.Create;
  73.     Self.pImages[Length(Self.pImages) - 1] := GetInfoImage(vFile);
  74.     Self.pImages[Length(Self.pImages) - 1].n := Length(Self.pImages);
  75.     Self.pImages[Length(Self.pImages) - 1].filePath := vFile;
  76.     img := Self.pImages[Length(Self.pImages) - 1];
  77.     j:=Length(Self.pImages);    //insert
  78.   end;
  79.   //else
  80.   //Automatic width or height calculus
  81.   if (vWidth = 0) then
  82.     vWidth := StrToFloat(FloatToStrF((vHeight * img.w / img.h), ffNumber,
  83.       14, 2, TPDFFormatSetings), TPDFFormatSetings);
  84.   if (vHeight = 0) then
  85.     vHeight := StrToFloat(FloatToStrF((vWidth * img.h / img.w), ffNumber,
  86.       14, 2, TPDFFormatSetings), TPDFFormatSetings);
  87.   _out('q ' + FloatToStr(vWidth) + ' 0 0 ' + FloatToStr(vHeight) +
  88.     ' ' + FloatToStr(vX) + ' -' + FloatToStr(vY + vHeight) + ' cm /I' +
  89.     IntToStr(j) + ' Do Q');  //modified
  90. end;
  91.  
  92.  

Is my change correct?
« Last Edit: April 25, 2018, 08:04:08 pm by menegaz »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Bug in procedure Image of the Class TJPFpdf
« Reply #1 on: April 25, 2018, 07:10:39 pm »
It would be very pleasant, if you could edit your post and put your source code in "[ code=pascal] xxx [ /code]" without double quotation marks and spaces.

menegaz

  • New member
  • *
  • Posts: 7
Re: Bug in procedure Image of the Class TJPFpdf
« Reply #2 on: April 25, 2018, 08:06:04 pm »
Okay, I've already modified the code.

Thank you

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Bug in procedure Image of the Class TJPFpdf
« Reply #3 on: April 25, 2018, 08:51:12 pm »
Almost.  :)

Use two "[ code=pascal] xxx [ /code]" for "original procedure" and for "modified procedure".

menegaz

  • New member
  • *
  • Posts: 7
Re: Bug in procedure Image of the Class TJPFpdf
« Reply #4 on: April 25, 2018, 11:26:14 pm »
Ok. Na próxima vou fazer correto.

abs.

 

TinyPortal © 2005-2018