|
-
Nov 2nd, 2005, 03:21 PM
#1
printing text center aligned
I have not used the printing functions of .NET because I have not had a need to. But now I do so I am wondering what is the best way to print text center aligned on a page.
I am using printdialog and printdocument components on the form, and I have most of the details worked out, but much of the text to be printed needs center alignment
when I use the graphics objects DrawString method, I am using a RectangleF for the location, and basically what I am doing is making the rectangle the width of the page, and passing a StringFormat object with its Alignment property set to center
This is similar to making a label on a form the full width of the form, and making the text center aligned.
Is this the best way to do this? or is there an easier way?
for some basic testing, it appears I also need to compensate for the margins.
-
Nov 3rd, 2005, 01:49 AM
#2
Re: printing text center aligned
i personally use the e.PageBounds.Width / 2 method to get the center of the page:
VB Code:
'PrintDocument:
Static fntHeadingFont As New Font("Arial", 16, FontStyle.Bold)
Dim sngCenterPage As Single
sngCenterPage = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString("Your String Here", fntHeadingFont).Width / 2)
e.Graphics.DrawString("Your String Here", fntHeadingFont, Brushes.Black, sngCenterPage, 2)
but either method achieves the same goal
-
Nov 3rd, 2005, 02:06 AM
#3
Re: printing text center aligned
Thats pretty much how I have seen it done... check out this link for a printing class you can reference to see how they deal with margins, etc...
http://www.developer.com/net/asp/article.php/3102381
-
Nov 3rd, 2005, 04:22 AM
#4
Re: printing text center aligned
If you use the System.Drawing.StringFormat you can set the alignment to use in the Graphics.DrawString method.
i.e.:
VB Code:
Public Enum CellTextHorizontalAlignment
LeftAlign = 1
CentreAlign = 2
RightAlign = 3
End Enum
Public Enum CellTextVerticalAlignment
TopAlign = 1
MiddleAlign = 2
BottomAlign = 3
End Enum
Public Function DrawCellString(ByVal s As String, _
ByVal HorizontalAlignment As CellTextHorizontalAlignment, _
ByVal VerticalAlignment As CellTextVerticalAlignment, _
ByVal BoundingRect As Rectangle, _
ByVal DrawRectangle As Boolean, _
ByVal Target As Graphics, _
ByVal PrintFont As Font, _
ByVal FillColour As Brush)
Dim x As Single, y As Single
Dim _Textlayout As New System.Drawing.StringFormat
If DrawRectangle Then
Target.FillRectangle(FillColour, BoundingRect)
Target.DrawRectangle(_GridPen, BoundingRect)
End If
'\\ Set the text alignment
If HorizontalAlignment = CellTextHorizontalAlignment.LeftAlign Then
_Textlayout.Alignment = StringAlignment.Near
ElseIf HorizontalAlignment = CellTextHorizontalAlignment.RightAlign Then
_Textlayout.Alignment = StringAlignment.Far
Else
_Textlayout.Alignment = StringAlignment.Center
End If
Dim BoundingRectF As New RectangleF(BoundingRect.X , BoundingRect.Y , BoundingRect.Width BoundingRect.Height )
Target.DrawString(s, PrintFont, System.Drawing.Brushes.Black, BoundingRectF, _Textlayout)
End Function
You can also set fancy effects like string trimming, etc.
-
Nov 3rd, 2005, 04:24 AM
#5
Re: printing text center aligned
(Code is from this codebank item in case I missed some declarations or something...)
-
Nov 3rd, 2005, 09:17 AM
#6
Re: printing text center aligned
yeah I am using the StringFormat object to center align the text... I just wanted to make sure there was not some really easy method that already existed for printing to the center of the page. What I have works, and its not really that hard to code, I just wanted to check since I have not done printing previously
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|