3

我有一个从集合中的项目创建列表的应用程序。然后对于每个项目,我会将它添加到一个空字符串中,然后在它的末尾添加一个换行符。所以理想情况下,我的字符串看起来像:

列表1\n列表2\n列表3\n

生成此字符串后,我将其发回以放置在 pdf 的占位符中。如果我在一个简单的控制台应用程序中尝试此代码,它会在换行符上打印所有内容。但在我的真实世界情况下,我必须将其打印为 pdf。这些项目只显示它们之间的空格而不是换行符。如何格式化我的字符串,以便 pdf 识别换行符而不是忽略它?

这是我用换行符生成字符串的代码。

    Private Function ConcatPlacardNumbers(ByVal BusinessPlacardCollection As BusinessPlacardCollection) As String
    Dim PlacardNumbersList As String = Nothing
    Dim numberofBusinessPlacards As Long = BusinessPlacardCollection.LongCount()
    For Each BusinessPlacard As BusinessPlacard In BusinessPlacardCollection
        numberofBusinessPlacards = numberofBusinessPlacards - 1
        PlacardNumbersList = String.Concat(PlacardNumbersList, BusinessPlacard.PlacardNumber)
        If numberofBusinessPlacards <> 0 Then
            PlacardNumbersList = String.Concat(PlacardNumbersList, Enviornment.newline)
        End If
    Next
    Return PlacardNumbersList
End Function
4

2 回答 2

1

答案将取决于用于生成 PDF 的工具。由于换行不起作用,我实际上会尝试\n. 另一种可能性是 PDF 生成代码并非设计为发出多行;您只能通过检查生成代码来确定这一点。

但是,您应该在代码中解决一个重要的性能问题:您将使用此代码生成大量字符串对象。您应该更改设计以使用System.Text.StringBuilder,这将大大提高性能:

Private Function ConcatPlacardNumbers(ByVal BusinessPlacardCollection As BusinessPlacardCollection) As String
    Dim PlacardNumbersList As New System.Text.StringBuilder(10000)
    For Each BusinessPlacard As BusinessPlacard In BusinessPlacardCollection
        If PlacardNumbersList.Length <> 0 Then
            ' This is equivalent to Environment.NewLine
            'PlacardNumbersList.AppendLine()
            ' The attempt to use \n
            PlacardNumbersList.Append("\n")
        End If
        PlacardNumbersList.Append(BusinessPlacard.PlacardNumber)
    Next
    Return PlacardNumbersList.ToString
End Function

请注意,您也不需要跟踪标语牌编号:您可以在第一个通道之后的每个通道的前一个条目的末尾添加一个换行符。

于 2013-01-07T02:24:08.080 回答
1

尝试添加\u2028

Private Function ConcatPlacardNumbers(ByVal BusinessPlacardCollection As _
                                      BusinessPlacardCollection) As String

    Dim PlacardNumbersList As New StringBuilder()

    For Each BusinessPlacard As BusinessPlacard In BusinessPlacardCollection

        PlacardNumbersList.Append(BusinessPlacard.PlacardNumber)
        'PlacardNumbersList.Append(ChrW(8232)) '\u2028 line in decimal form
        PlacardNumbersList.Append(ChrW(8233)) '\u2029 paragr. in decimal form

    Next

    Return PlacardNumbersList.ToString

End Function

对于段落,请\u2029改用。更多详情:http:
//blogs.adobe.com/formfeed/2009/01/paragraph_breaks_in_plain_text.html

于 2013-01-07T09:49:55.857 回答