0

I'd like to print text using a monospace font, 80 character wide, 66 characters tall. The goal is to mimic sending raw text to an impact printer.

Here's some code:

public void Print(string printerName, string text, string desc = "")
{
    PrintDialog dialog = new PrintDialog();
    PrintQueue queue = new LocalPrintServer().GetPrintQueue(printerName);
    dialog.PrintQueue = queue;

    PageImageableArea area = queue.GetPrintCapabilities(dialog.PrintTicket).PageImageableArea;

    TextBlock block = new TextBlock();
    block.FontFamily = new FontFamily("Courier New");
    block.Margin = new Thickness(area.OriginWidth, area.OriginHeight, area.OriginWidth, area.OriginHeight);
    block.FontSize = ???
    block.Text = text;
    block.Measure(new Size(area.ExtentWidth, area.ExtentHeight));
    block.Arrange(new Rect(new Point(0, 0), block.DesiredSize));

    dialog.PrintVisual(block, desc);
}

It's my understanding that PageImageableArea gives me area.ExtentWidth and area.ExtentHeight which are my actual printing dimensions (in pixels), and area.OriginWidth and area.OriginHeight which are the size of my margins (in pixels).

If I want to fit exactly 80 characters across the page, each character should be area.ExtentWidth / 80 wide, in pixels. The area.ExtentWidth of my printer is 768, so each character should be 9.6 pixels wide.

Additionally, if I want to fit exactly 66 lines on my page, each line should be area.ExtentHeight / 66 tall, in pixels. The area.ExtentHeight of my printer is 1038.66, so each line should be 15.7 pixels tall.

Using these inputs, how do I choose a FontSize where each character is X pixels wide, and, when wrapped, causes each line to be Y pixels tall?

epalm
  • 4,283
  • 4
  • 43
  • 65

1 Answers1

0

start by picking a font to print in that is fixed in size, e.g., Courier New. This will give you known character dimensions and hugely simplifies the calculations. When dealing with the printer, it is best to use Twips rather than "pixels" as the twip/pixel ratio varies a lot depending on your monitor/OS settings, etc. A good article/answer to the rendering mode can be found here; How do I convert Twips to Pixels in .NET?

The reason you use twips when dealing with printing is because it translates very closely with font point sizes. A font "point" is 1/72 of an inch. Likewise there are 1440 twips in an inch. Therefor there are 20 twips (1440 / 72) per font "point".

Once you have a fixed font size that you can control accurately, the calculations you've outlined will give you the font size. Eg, if you have a printing space of 648 twips available then divide by 72 and you get a font "point" of 9 to use in your application.

Community
  • 1
  • 1
DiskJunky
  • 4,750
  • 3
  • 37
  • 66