2017-09-04 273 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)'的绝对位置。当然,这也是一个以前已经回答过很多次的问题。 –

相关问题