2017-09-04 271 views
1

如何將文本放置在PDF上的特定位置?我做了一些搜索,但沒有找到太好的東西。我有document.Add(new Paragraph("Date:" + DateTime.Now));,我想把它放在pdf文件的特定區域。如何使用iTextSharp在特定位置放置段落

我的代碼:

private void savePDF_Click(object sender, EventArgs e) 
    { 
     FileStream fileStream = new FileStream(nameTxtB.Text + "Repair.pdf", FileMode.Create, FileAccess.Write, FileShare.None); 
     Document document = new Document(); 
     document.Open(); 
     iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(PageSize.LETTER); 
     PdfWriter pdfWriter = PdfWriter.GetInstance(document, fileStream); 

     iTextSharp.text.Image r3tsLogo = iTextSharp.text.Image.GetInstance("rt3slogo.PNG"); //creates r3ts logo 
     iTextSharp.text.Image r3Info = iTextSharp.text.Image.GetInstance("R3 Information.PNG"); //creates r3 information text below r3ts logo 

     r3tsLogo.SetAbsolutePosition(document.PageSize.Width - 375 - 0f, document.PageSize.Height - 130 - 0f); 
     r3Info.SetAbsolutePosition(document.PageSize.Width - 365 - 0f, document.PageSize.Height - 170 - 0f); //higher the number in height the lower the place of text on paper 
            //less number will result in text more to right in width 

     //increase size of picture 
     r3tsLogo.ScalePercent(120); 
     r3Info.ScalePercent(65); 

//---------------adds all images to pdf file --------------------------------- 
     document.Add(r3tsLogo); 
     document.Add(r3Info); 
     document.Add(new Paragraph("Date:" + DateTime.Now)); 




     document.Close(); 
    } 

回答

2

假設你知道如何在絕對位置添加圖像(見里斯的答案),但看着如何添加文字,那麼你的問題的答案是:使用ColumnText

如果你只需要添加一個並不需要被包裝單行線,您可以使用ShowTextAligned()方法:

ColumnText.showTextAligned(writer.DirectContent, 
    Element.ALIGN_CENTER, new Phrase("single line"), x, y, rotation); 

在這行代碼,xy都爲座標中間的文本(其他可能的對齊值爲ALIGN_LEFTALIGN_RIGHT)。 rotation參數定義了以度爲單位的旋轉。請注意文字"single line"不會被包裝。如果您添加的文本太長,您可以以這種方式添加「脫離頁面」的文本。

如果你想添加一個特定的矩形內的文字,那麼你需要使用一個Rectangle對象來定義列:

ColumnText ct = new ColumnText(writer.DirectContent); 
ct.setSimpleColumn(new Rectangle(0, 0, 523, 50)); 
ct.addElement(new Paragraph("This could be a very long sentence that needs to be wrapped")); 
ct.go(); 

如果你提供比嵌入的矩形更多的文本,該文本將不會被渲染。但是,它仍然可在ct對象中使用,以便您可以將剩餘文本添加到其他位置。

所有這一切都已經被問和回答過:

一行:

多行:

難道我早已把尋找這些例子嗎?不,我在Absolute Positioning of text的官方網站上找到它們。

智慧是有那些誰搜索...

1

這個概念在書中 '的iText在行動' 徹底解釋。哪些可以在網站上找到。

http://developers.itextpdf.com/examples/itext-action-second-edition/chapter-3

短代碼示例(檢查是否有其他的例子網站):

// step 1 
Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30); 

// step 2 
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); 

// step 3 
document.open(); 

// step 4 
// Create and add a Paragraph 
Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22)); 
p.setAlignment(Element.ALIGN_CENTER); 
document.add(p); 

// Create and add an Image 
Image img = Image.getInstance(RESOURCE); 
img.setAbsolutePosition(
     (PageSize.POSTCARD.getWidth() - img.getScaledWidth())/2, 
     (PageSize.POSTCARD.getHeight() - img.getScaledHeight())/2); 
document.add(img); 
+0

我認爲這個問題是不是定位圖像,但定位'「日期:」 + DateTime.Now)'的絕對位置。當然,這也是一個以前已經回答過很多次的問題。 –

相關問題