Results 1 to 6 of 6

Thread: printing text center aligned

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: printing text center aligned

    i personally use the e.PageBounds.Width / 2 method to get the center of the page:

    VB Code:
    1. 'PrintDocument:
    2. Static fntHeadingFont As New Font("Arial", 16, FontStyle.Bold)
    3. Dim sngCenterPage As Single
    4. sngCenterPage = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString("Your String Here", fntHeadingFont).Width / 2)
    5. e.Graphics.DrawString("Your String Here", fntHeadingFont, Brushes.Black, sngCenterPage, 2)

    but either method achieves the same goal
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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:
    1. Public Enum CellTextHorizontalAlignment
    2.         LeftAlign = 1
    3.         CentreAlign = 2
    4.         RightAlign = 3
    5.     End Enum
    6.  
    7.     Public Enum CellTextVerticalAlignment
    8.         TopAlign = 1
    9.         MiddleAlign = 2
    10.         BottomAlign = 3
    11.     End Enum
    12.  
    13.     Public Function DrawCellString(ByVal s As String, _
    14.                                     ByVal HorizontalAlignment As CellTextHorizontalAlignment, _
    15.                                     ByVal VerticalAlignment As CellTextVerticalAlignment, _
    16.                                     ByVal BoundingRect As Rectangle, _
    17.                                     ByVal DrawRectangle As Boolean, _
    18.                                     ByVal Target As Graphics, _
    19.                                     ByVal PrintFont As Font, _
    20.                                     ByVal FillColour As Brush)
    21.  
    22.  
    23.         Dim x As Single, y As Single
    24.         Dim _Textlayout As New System.Drawing.StringFormat
    25.  
    26.         If DrawRectangle Then
    27.             Target.FillRectangle(FillColour, BoundingRect)
    28.             Target.DrawRectangle(_GridPen, BoundingRect)
    29.         End If
    30.  
    31.         '\\ Set the text alignment
    32.         If HorizontalAlignment = CellTextHorizontalAlignment.LeftAlign Then
    33.             _Textlayout.Alignment = StringAlignment.Near
    34.         ElseIf HorizontalAlignment = CellTextHorizontalAlignment.RightAlign Then
    35.             _Textlayout.Alignment = StringAlignment.Far
    36.         Else
    37.             _Textlayout.Alignment = StringAlignment.Center
    38.         End If
    39.  
    40.         Dim BoundingRectF As New RectangleF(BoundingRect.X , BoundingRect.Y , BoundingRect.Width BoundingRect.Height )
    41.  
    42.         Target.DrawString(s, PrintFont, System.Drawing.Brushes.Black, BoundingRectF, _Textlayout)
    43.  
    44.     End Function

    You can also set fancy effects like string trimming, etc.

  5. #5
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: printing text center aligned

    (Code is from this codebank item in case I missed some declarations or something...)

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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
  •  



Click Here to Expand Forum to Full Width