WPCubed Product Support

Drawing Measurements / PageUnits

support

Hi,

I suggest to work with integer coordinates since this is what the PDF engine internally also does:

procedure TWinForm.PDFControl1_BeforeEndPage(Sender: System.Object);
var x, y : Integer;
begin
   x := Round( 8.5 * pdfControl1.Canvas.DpiX );
   y := Round( 11 * pdfControl1.Canvas.DpiX );
   pdfControl1.Canvas.DrawString('Hello World',
   System.Drawing.Font.Create('Comic Sans MS', 24),
   System.Drawing.SolidBrush.Create(Color.Black),40, 50);

   pdfControl1.Canvas.DrawLine(
      System.Drawing.SystemPens.WindowText,
      0,0,
      x, y
      );
end;

This code uses the BeforeEndPage event of wPDFControl under .NET. (Language Delphi .NET)

#2 ♥ 0
7k Views 2 users read 2 min

I'm having some difficulty getting the drawing coordinates measured correctly when attempting to draw to the PDFControl.Canvas. The PageUnits show they are set to GraphicsUnits.Display which is documented as 1/75th of an inch. I've attempted to adjust my coordinates based on this without success. I've also changes the PageUnits to GraphicsUnits.Inch and had similar results - everything is off by about 20%.

Is there some additional piece of information I need to know to get this correct? Here is a sample of code where I'm simply drawing an X from corner to corner of an 8.5x11 page (note the printer page bounds are always delivered in 1/100 of an inch:

private void OnPrintPDFPage(object sender, PrintPageEventArgs e)
{
pdfControl1.DrawMetafile(tern1.TerPageMetafile(m_iPrint_Page),300,300); // TerPageMetafile creates pages in 1/300 inches resolution

// Draw a big X on the page
Graphics gr = pdfControl1.Canvas;

float left, right, top, bottom;

left = ((float)e.PageBounds.Left) * (float)0.75;
right = ((float)e.PageBounds.Right) * (float)0.75;
top = ((float) e.PageBounds.Top) * (float)0.75;
bottom = ((float) e.PageBounds.Bottom) * (float)0.75;

gr.DrawLine(new Pen(new SolidBrush(Color.Black), (float)0), new PointF(left, top), new PointF(right, bottom));
gr.DrawLine(new Pen(new SolidBrush(Color.Black), (float)0), new PointF(right, top), new PointF(left, bottom));

The right and bottom always come out beyond the 8.5x11 edges. Any help is appreciated while I still have some hair left.

Thanks.

#1 ♥ 0