TadvPDFLib.Graphics.DrawImageFromFile only draws partial image

I have this code (admittedly the scanning is involved also )

procedure TpsScanner.ConvertToPDF;
var
p: TadvPDFLib;
begin
p := TadvPDFLib.Create;
try
p.BeginDocument(TPath.GetTempPath + 'psScanner.pdf');
p.NewPage;
p.Graphics.DrawImageFromFile(TPath.GetTempPath + 'psScanner.jpg',
PointF(0, 0));
p.EndDocument(False);
finally
p.Free;
end;
end;

The produced pdf only shows the top left quarter of the image. The scanned image looks great. Am I missing some sizing? The scanned image uses letter size.

Thank you
Gary

FYI I am trying to simply create a PDF file from a scanned image., that's it. No additional info.

We have tested this here and couldn't see an issue drawing images. Please provide the image and/or a sample that demonstrates the issue.

Peter,

Here is the Image and the pdf produced.

Gary
psScanner.pdf (2.5 MB)

Must be a scaling issue. Got it working using SynPdf with this code:

procedure TpsCustomScanner.ConvertToSynPDF;
begin
if Assigned(FSynPDFDocument) then
FreeAndNil(FSynPDFDocument);
FSynPDFDocument := TPdfDocumentGDI.Create;
try
var
lPage := FSynPDFDocument.AddPage;

// Same result as TMS
// FSynPDFDocument.VCLCanvas.Draw(0,0, FResultPicture.Graphic);

var lRect := TRect.Create(0, 0, Round(lPage.PageWidth * 1.67),
  Round(lPage.PageHeight * 1.67));

//Log
CodeSite.Send(Format('PageWidth; %d PageHeight: %d', [lPage.PageWidth,
  lPage.PageHeight]));
CodeSite.Send(Format('Graphic Width: %d, Height: %d',
  [FResultPicture.Graphic.Width, FResultPicture.Graphic.Height]));
CodeSite.Send(Format('lRect Width: %d lRect Height: %d',
  [lRect.Width, lRect.Height]));

FSynPDFDocument.VCLCanvas.StretchDraw(lRect, FResultPicture.Graphic);
FSynPDFDocument.SaveToFile(FFileName + '.pdf');

finally
FreeAndNil(FSynPDFDocument);
end;
end;

Here is the CodeSite log:
Graphic Width: 1265, Height: 1636
lRect Width: 994 lRect Height: 1406

I included results (Black n white this time)
P22C0001_0001.pdf (1.4 MB)

Missed page width

PageWidth; 595 PageHeight: 842

Hi,

I overlooked your code initially, but it seems that you are drawing the image as-is from the top-left of the PDF. The image is bigger than the initial PDF pixel size, so you need to make sure that you are drawing the image with aspect ratio in a smaller rectangle. You can do this via

p.Graphics.DrawImageFromFile(TPath.GetTempPath + 'psScanner.jpg', p.MediaBox);      

Thanks Peter that worked beautifully, and without the scaling I had to do with SynPdf!

However, I cannot get rid of the header and footer.
I tried:

FPDFDocument.NewPage;
FPDFDocument.HeaderSize := 0;
FPDFDocument.FooterSize := 0;
FPDFDocument.Header := '';
FPDFDocument.Footer := '';

Setting size or content doesn't have any effect.

If I scan a full size image it does cover the Header and Footer, but an image that is not full page always shows the default 'Header' and 'Footer' text.
P22C0001_0001.pdf (1.5 MB)

Gary

Hi,

NewPage; is actually drawing the Header and Footer, so the settings should be placed before NewPage:

FPDFDocument.HeaderSize := 0;
FPDFDocument.FooterSize := 0;
FPDFDocument.Header := '';
FPDFDocument.Footer := '';
FPDFDocument.NewPage;

Awesome!
Thanks again

1 Like

Peter,

Still need to account for non standard sizes, and in my case the resolution of the scanned image.
This works, although I have to prompt for the resolution as DelphiTwain does not return the scanner resolution.

procedure TpsCustomScanner.ConvertToPDF;
begin
if Assigned(FPDFDocument) then
FreeAndNil(FPDFDocument);
FPDFDocument := TadvPDFLib.Create;
try
FPDFDocument.BeginDocument(FFileName + '.pdf');
FPDFDocument.Header := '';
FPDFDocument.Footer := '';
FPDFDocument.HeaderSize := 0;
FPDFDocument.FooterSize := 0;
FPDFDocument.PageSize := psCustom;
FPDFDocument.PageWidth := Round(FResultPicture.Width * (72 / FScanResolution) );
FPDFDocument.PageHeight := Round(FResultPicture.Height * (72 / FScanResolution) );
FPDFDocument.NewPage;
FPDFDocument.Graphics.DrawImage(FResultPicture, FPDFDocument.MediaBox);
FPDFDocument.EndDocument(False);
finally
FreeAndNil(FPDFDocument);
end;
end;

Gary

Hi,

For images, this is true, because the DPI used is 72, but the printer DPI is typically higher (300-600). This means that the PDF will indeed be stretched out when printing losing resolution. For all the other primitives this makes no difference as they are vector based.