2011-10-20 18 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 
相关问题