PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Render to Graphics
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4399
Page 1 of 1

Author:  bw3674 [ Tue Oct 25, 2022 1:04 pm ]
Post subject:  Render to Graphics

First of all, thank you very much for PDFsharp and MigraDoc!

I am trying to use MigraDoc to generate report pages. From another application's printing function I am receiving an hdc to a GDI+ object (metafile) as a pointer to generate additional report pages from C#.

I am using GetDeviceCaps() from gdi32.dll to get the page dimensions (210 x 297 mm, DIN A4 page) and create a Graphics object. Then I am creating the MigraDoc document and try to render it to the Graphics obejct with an XGraphics object. Here is the simplified code:

Code:
public void print(IntPtr hdc)
{
  int horzSize = GetDeviceCaps(hdc, (int)DeviceCap.Horzsize); // 210
  int vertSize = GetDeviceCaps(hdc, (int)DeviceCap.Vertsize); // 297
 
  Graphics gfx = Graphics.FromHdc(hdc);
  gfx.PageUnit = GraphicsUnit.Millimeter;
  gfx.Clear(Color.White);
 
  Document document = new Document();

  Style style = document.Styles["Normal"];
  style.Font.Name = "Verdana";
  style.Font.Size = 8;

  document.DefaultPageSetup.LeftMargin = 0;
  document.DefaultPageSetup.TopMargin = 0;
  document.DefaultPageSetup.PageWidth = new Unit(rectangle.Width, UnitType.Millimeter);
  document.DefaultPageSetup.PageHeight = new Unit(rectangle.Height, UnitType.Millimeter);
 
  Section section = document.AddSection();
 
  Table table = section.AddTable();
  table.AddColumn(new Unit(30, UnitType.Millimeter));
  table.AddColumn(new Unit(30, UnitType.Millimeter));

  Row row;
  row = table.AddRow();
  row.HeadingFormat = true;
  row.Cells[0].AddParagraph("Column 1");
  row.Cells[1].AddParagraph("Column 2");

  for (int i = 1; i < 33; i++)
  {
    row = table.AddRow();
    row.Cells[0].AddParagraph(i.ToString());
    row.Cells[1].AddParagraph(i.ToString());
  }
 
  section.AddParagraph("Hello world!");
 
  DocumentRenderer renderer = new DocumentRenderer(document);
  renderer.PrepareDocument();
 
  XGraphics xgfx = XGraphics.FromGraphics(gfx, new XSize(rectangle.Width, rectangle.Height), XGraphicsUnit.Millimeter);
  renderer.RenderPage(xgfx, 1);
  xgfx.Dispose();
 
  gfx.Dispose();
}


The result is attached. The font is way too big. I played around with different units but I can't get it to scale correctly.

Am I doing the rendering correctly? Maybe it isn't even intended to use MigraDoc like this?

Attachments:
page.png
page.png [ 12.82 KiB | Viewed 1918 times ]

Author:  bw3674 [ Thu Oct 27, 2022 3:17 pm ]
Post subject:  Re: Render to Graphics

I found the solution: I have to set points as the unit for the Graphics object and calculate the page size accordingly:

Code:
float scaleFactor = 72.0f / 25.4f;
int horzSize = (int)(GetDeviceCaps(hdc, (int)DeviceCap.Horzsize) * scaleFactor);
int vertSize = (int)(GetDeviceCaps(hdc, (int)DeviceCap.Vertsize) * scaleFactor);

Graphics gfx = Graphics.FromHdc(hdc);
gfx.PageUnit = GraphicsUnit.Point;

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/