PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 11:15 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Render to Graphics
PostPosted: Tue Oct 25, 2022 1:04 pm 
Offline

Joined: Tue Oct 25, 2022 9:13 am
Posts: 2
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 1348 times ]
Top
 Profile  
Reply with quote  
 Post subject: Re: Render to Graphics
PostPosted: Thu Oct 27, 2022 3:17 pm 
Offline

Joined: Tue Oct 25, 2022 9:13 am
Posts: 2
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;


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 55 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group