I did check by saving the PDF and it all does seem fine (after implementing your suggestions for a custom size and tweaking/elimination of the margins). I used the values of 80mm and 50mm and when I check the saved PDF I do get the correct values in inches. (However, during the running of the actual application I cannot save the PDF to disk.) When I print it, I don't know how long it is, because I stop the printer long before it finishes (once I see that it is longer than I intend it to be). However, when I tried it just now it was much longer than 50mm before I stopped it, and "Hello World" hadn't even been printed yet. Here is the new code I changed (same CreateDocument() function otherwise):
Code:
private Document CreateDocument()
{
Document document = new Document();
MigraDoc.DocumentObjectModel.Unit width, height;
width = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(80);
height = MigraDoc.DocumentObjectModel.Unit.FromMillimeter(50);
Section section = document.AddSection();
section.PageSetup.PageHeight = height;
section.PageSetup.PageWidth = width;
section.PageSetup.PageHeight = height;
section.PageSetup.PageWidth = width;
section.PageSetup.LeftMargin = 0;
section.PageSetup.RightMargin = 0;
section.PageSetup.TopMargin = 0;
Paragraph paragraph = section.AddParagraph();
paragraph.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Black; //Same as System.Drawing.Color
paragraph.AddFormattedText("Hello, World!", TextFormat.Bold);
return document;
}
I print the PDF file from this code. This causes it to be displayed as a preview, then the user needs to click the print icon, then they need to accept the proper printer. I must admit I find how this works to be annoying (considering that the user needs to lick a print button first, this means they need to click 3 times just to print anything), but at this point I am just trying to get anything printed.
Code:
MemoryStream stream = new MemoryStream();
pdfRenderer.PdfDocument.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();