2016-09-30 29 views
0

次のコードを書いてPDFでテキストを書きましたが、テキストの後に改行したいと思います。iTextSharpで改行文字を導入するには

Dim document As Document 

    document = New Document(PageSize.A4, 5.0F, 20.0F, 20.0F, 20.0F) 

    Try 

     Dim writer As PdfWriter 
     writer = PdfWriter.GetInstance(document, New FileStream(filename, FileMode.Create)) 

     document.Open() 

     Dim spacing As Integer 

     spacing = 0 

     Dim curY, lineHeight As Double 

     curY = document.Top 
     lineHeight = 0 

     Const maxPerLine As Integer = 3 


     For i As Integer = 0 To 5 

      Dim table As PdfPTable 
      table = New PdfPTable(4) 

      table.DefaultCell.Border = Rectangle.NO_BORDER 
      table.TotalWidth = 200.0F 
      table.LockedWidth = True 

      Dim cell As PdfPCell 
      cell = New PdfPCell(New Phrase("hello \n" + i + "\n" + "wass up ?")) 
      cell.Colspan = 4 
      cell.HorizontalAlignment = 0 
      cell.Border = Rectangle.NO_BORDER 
      cell.Padding = 30.0F 
      table.AddCell(cell) 

      table.WriteSelectedRows(0, -1, document.Left + spacing, curY, writer.DirectContent) 

      spacing = spacing + 200 

      lineHeight = Math.Max(lineHeight, table.TotalHeight) 

      If 0 = (i + 1) Mod maxPerLine Then 

       curY = curY - lineHeight 
       spacing = 0 
       lineHeight = 0 

      End If 

     Next 

    Catch ex As Exception 

    Finally 

     document.Close() 


    End Try 

私はParagraphで試しましたが、まだ新しい行にテキストを入力できません。

私はiTextSharpの文書を読んでいますが、行を分割して "\ n"を使いたいが、それは機能していません。

テキストの後に改行するにはどうすればよいですか?

答えて

2

最も簡単な方法は、(あなたがテキストモードを使用している)複合モードPdfPCellを使用することです。あなたは、この場合のPdfPCellのレベルでの配置を設定することはできません

Dim cell As PdfPCell 
cell = New PdfPCell() 
cell.AddElement(New Paragraph("line 1")) 
cell.AddElement(New Paragraph("line 2")) 

注:AddElementを使用する場合、複合モードがプレイに入りました。 複合モードを使用する場合は、要素のレベル(この場合はParagraphのレベル)で配置を設定する必要があります。

+0

ありがとうございます。 – deepak

+0

Environment.NewLineを使用しています。私は別々の段落を追加する必要はありません –

関連する問題