Hello,
I'd like to offer a table for all included file attachments. There was a hint
of Bruno Lowagie to use the cell event. Below you find an excert from
my test case.
Bye, Carsten
PS: How could one put this table onto an existing PDF using a PdfStamper ??
----------schnipp-------------
@Test public void test() throws Exception {
Document document = new Document();
final PdfWriter writer = PdfWriter.getInstance( document,
new FileOutputStream( "result.pdf" ) );
// step 3: we open the document
document.open();
// step 4: we add a table to the document
PdfPTable table = new PdfPTable( 3 );
PdfPCellEvent addAnnotation = new AnnotationCellEvent( writer );
PdfPCell cell = new PdfPCell( new Paragraph( "header with
colspan 3" ) );
cell.setColspan( 3 );
table.addCell( cell );
table.addCell( "1.1" );
table.addCell( "2.1" );
PdfPCell ca = new PdfPCell( new Paragraph( " file.txt" ) );
ca.setCellEvent( addAnnotation );
table.addCell( ca );
table.addCell( "1.2" );
table.addCell( "2.2" );
table.addCell( "3.2" );
document.add( table );
// step 5: we close the document
document.close();
}
private static final class AnnotationCellEvent implements PdfPCellEvent {
private PdfWriter writer;
public AnnotationCellEvent( PdfWriter writer ) {
this.writer = writer;
}
public void cellLayout( PdfPCell cell, Rectangle position,
PdfContentByte[] canvases ) {
log.info( "cellLayout, cell " + cell + ", pos " + position );
try {
PdfContentByte cb = canvases[ PdfPTable.TEXTCANVAS ];
Rectangle rect = new Rectangle( position.getLeft() +2
, position.getTop() -2 ,
position.getLeft() + 10, position.getTop() -10 );
PdfAnnotation annot1 =
PdfAnnotation.createFileAttachment( writer, rect,
"Double click to open file.txt",
"content".getBytes(), null, "file.txt" );
annot1.put( PdfName.NAME, new PdfString( "Paperclip" ) );
writer.addAnnotation( annot1 );
} catch ( Exception exp ) {
exp.printStackTrace();
}
}
}
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Do you like iText?
Buy the iText book: http://www.1t3xt.com/docs/book.php
Or leave a tip: https://tipit.to/itexttipjar