Resolved PrintPreview/Printed page descrepencies?

soundbomber

Member
Joined
Nov 29, 2010
Messages
6
Programming Experience
5-10
Hello,
I have a document printed to a PrintPreview and then to a printer. I am using graphics.measurestring to position some custom subscript text. My problem is that upon examination of the printed output, the position of my subscript text is incorrect, but it is correct on the screen printpreview. I am using identical code for both the print preview and the printed output. The values returned from the measurestring are also identical yet there is this discrepency!
It's hard to tell but perhaps this only becomes apparent when measuring long strings?
Attached is a graphic showing the problem, the red rectangles indicate the width of the measured string when drawn to the graphics surface.
Any help would be appreciated.
 

Attachments

  • GDI.jpg
    GDI.jpg
    60.7 KB · Views: 40
Last edited:
You're using e.Graphics ?
 
Also wondering if you're using a StringFormat, and if so what settings are applied.
 
Hmm, according to documentation that should have same effect as you already had, for example from Graphics.MeasureString Method (String, Font, Int32, StringFormat) (System.Drawing)
The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.
This could also be an option TextRenderer.MeasureText Method (IDeviceContext, String, Font, Size, TextFormatFlags) (System.Windows.Forms)
It has the same padding behaviour that also can be configured:
For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. If you need to draw a line of text without these extra spaces, use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter, as shown in the example.
 
There appears to be no reason that I can see why MeasureCharacterRanges should work where MeasureString doesn't, but I am just glad that it does. What I cant figure is why MeasureString works on the Preview but not on the printed output - same code used exactly! Anyway I am happy with the end result and that is what counts.
Thank you for your input JohnH, much appreciated.
 
Back
Top