2011-10-20 22 views
2

我需要能夠確定用戶輸入的文本是否符合打印頁面部分的界限,並且具有預定義的報告佈局,例如。 A部分:爲2x3英寸長方形留出空間以包含文本,但可以包含來自戰爭與和平的所有文本。如果文本不符合給定的矩形,我需要打印另一頁並繼續打印溢出之前打印的頁面部分的任何文本。.NET打印 - 如何確定文本是否適合指定的矩形

該應用程序是用VB 2010 Express編寫的,但歡迎使用C#中的示例。

TIA

+0

這是GDI +(PrintDocument的)印刷爲主? –

回答

2

試着看一下對Graphics類的MeasureString和MeasureCharacterRanges功能。

+0

沒錯。 MeasureString可以用來實現這一點。這裏有一個例子: –

+0

不,聽到一個起點。如果智能感知不能使這種方法的使用完全顯而易見,那麼谷歌搜索的時間就是半秒。做你自己的工作。 –

0

這是我希望其他人會發現可用於確定文本 的,將適合在一個給定區域量的方法:

Public Function GetTextThatFitsLength(_ 
     ByVal e As PrintPageEventArgs, _ 
     Text As String, _ 
     ByVal Width As Integer, _ 
     ByVal Height As Integer, _ 
     ByVal Font As Font, _ 
     Optional ByRef LinesFilled As Integer = 0) As Integer 
     ' returns the number of charaters that fit into the specified area 
     ' optionally returns the number of lines that fit into the area 
     Dim LayoutArea As New SizeF 
     With LayoutArea 
      .Width = Width 
      .Height = Height 
     End With 
     Dim StringFormat As New StringFormat(StringFormat.GenericDefault) 
     With StringFormat 
      .Alignment = StringAlignment.Near 
      .Trimming = StringTrimming.Word 
     End With 
     Dim CharactersFitted As Integer 
     e.Graphics.MeasureString(Text, Font, LayoutArea, StringFormat, CharactersFitted, LinesFilled) 
     Return CharactersFitted 
    End Function 
相關問題