2016-09-30 503 views
0

我有以下代碼以打印一個文本到使用iTextSharp的PDF文檔:ColumnText.ShowTextAligned()切斷後的文本斷行

canvas = stamper.GetOverContent(i) 
watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED) 
watermarkFontColor = iTextSharp.text.BaseColor.RED 
canvas.SetFontAndSize(watermarkFont, 11) 
canvas.SetColorFill(watermarkFontColor) 

Dim sText As String = "Line1" & vbCrLf & "Line2" 
Dim nPhrase As New Phrase(sText) 

ColumnText.ShowTextAligned(canvas, Element.ALIGN_TOP, nPhrase, 0, 50, 0) 

然而,僅在第一行(「線路1」)被印刷,第二行(「Line2」)不是。

我是否必須通過任何標誌才能使其工作?

回答

0

ColumnText.ShowTextAligned已經實現爲用於在給定對齊的給定位置添加單行的用例的捷徑。賦予源代碼文檔:

/** Shows a line of text. Only the first line is written. 
* @param canvas where the text is to be written to 
* @param alignment the alignment 
* @param phrase the <CODE>Phrase</CODE> with the text 
* @param x the x reference position 
* @param y the y reference position 
* @param rotation the rotation to be applied in degrees counterclockwise 
*/  
public static void ShowTextAligned(PdfContentByte canvas, int alignment, Phrase phrase, float x, float y, float rotation) 

更多通用用例,請實例化一個ColumnText,設定畫的內容和輪廓繪製的,並呼籲Go()

1

如文檔所述,ShowTextAligned()方法只能用於繪製一條線。如果你想畫兩條線,你有兩個選擇:

選項1:使用的方法兩次:

ColumnText.ShowTextAligned(canvas, Element.ALIGN_TOP, new Phrase("line 1"), 0, 50, 0) 
ColumnText.ShowTextAligned(canvas, Element.ALIGN_TOP, new Phrase("line 2"), 0, 25, 0) 

選項2:使用ColumnText以不同的方式:

ColumnText ct = new ColumnText(canvas); 
ct.SetSimpleColumn(rect); 
ct.AddElement(new Paragraph("line 1")); 
ct.AddElement(new Paragraph("line 2")); 
ct.Go(); 

在這段代碼中,rect的類型是Rectangle。它定義了你想添加文本的區域。見How to add text in PdfContentByte rectangle using itextsharp?