2012-07-30 72 views
1

在这个例子中:在iText 2.1.7中获取Chunk的绝对边界框?

Document document=new Document(); 
PdfWriter.getInstance(document,new FileOutputStream("hello.pdf")); 
document.open(); 
Paragraph paragraph = new Paragraph(); 
paragraph.add(new Chunk("Hello "); 
paragraph.add(new Chunk("world"); 
paragraph.add(new Chunk(" in"); 
paragraph.add(new Chunk(" iText 2.7.1"); 
document.add(paragraph); 
document.close(); 

我怎样才能获得绝对位置,宽度和高度的第二块“世界”的网页上?换句话说,用户空间中的边界框。

+0

有[never](http://itextpdf.com/history/)是iText的2.7.1版本。你的意思是2.1.7? – 2012-07-30 09:20:21

+0

现在在文本中更正为2.1.7。谢谢亚历克西斯。 – sina72 2012-07-30 11:19:33

回答

2

您可以使用块上提供的通用标记功能。

public class PEvents extends PdfPageEventHelper { 
     @Override 
     public void onGenericTag(PdfWriter w, Document doc, Rectangle r, String tag) { 
     if (tag.equals("mytag")) {    
      //here r is the rectangle of "world" chunk 
     } 
     } 
    } 
    writer.setPageEvent(new PEvents()); 
    Chunk world = new Chunk("world"); 
    world.setGenericTag("mytag"); 
    paragraph.add(world); 
    . . .