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?