回答
忘掉一秒鐘的servlet。確保你可以讓頁眉/頁腳在本地工作。
編輯:我是一個不好的SO用戶發佈過時的HeaderFooter的東西。似乎很多人都這樣做,使得找到這個問題的簡潔例子變得非常困難。 add header to pdf using iText回答了這個問題。
這裏似乎是正確的方法來做到這一點:(來自http://itextpdf.com/examples/iia.php?id=104修改):的[添加報頭以利用iText PDF] https://gist.github.com/977502
/*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*
* Modified by Nick Dunn (http://developmentality.wordpress.com) to
* focus on just the PDF header aspect.
*/
package part1.chapter05;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import java.util.TreeSet;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
public class MovieCountries1 {
/** The resulting PDF file. */
public static final String RESULT
= "example_result.pdf";
/**
* Inner class to add a table as header.
*/
class TableHeader extends PdfPageEventHelper {
/** The header text. */
String header;
/** The template with the total number of pages. */
PdfTemplate total;
/**
* Allows us to change the content of the header.
* @param header The new header String
*/
public void setHeader(String header) {
this.header = header;
}
/**
* Creates the PdfTemplate that will hold the total number of pages.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(30, 16);
}
/**
* Adds a header to every page
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table = new PdfPTable(3);
try {
table.setWidths(new int[]{24, 24, 2});
table.setTotalWidth(527);
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.BOTTOM);
table.addCell(header);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(String.format("Page %d of", writer.getPageNumber()));
PdfPCell cell = new PdfPCell(Image.getInstance(total));
cell.setBorder(Rectangle.BOTTOM);
table.addCell(cell);
table.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());
}
catch(DocumentException de) {
throw new ExceptionConverter(de);
}
}
/**
* Fills out the total number of pages before the document is closed.
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(total, Element.ALIGN_LEFT,
new Phrase(String.valueOf(writer.getPageNumber() - 1)),
2, 2, 0);
}
}
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
* @throws SQLException
*/
public void createPdf(String filename)
throws IOException, DocumentException, SQLException {
// step 1
Document document = new Document(PageSize.A4, 36, 36, 54, 36);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
TableHeader event = new TableHeader();
writer.setPageEvent(event);
// step 3 - fill in the document
document.open();
event.setHeader("Hello!");
document.add(new Paragraph("Testing."));
document.newPage();
event.setHeader("There!");
document.add(new Paragraph("Testing."));
document.close();
}
/**
* Main method.
*
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
* @throws SQLException
*/
public static void main(String[] args)
throws IOException, DocumentException, SQLException {
new MovieCountries1().createPdf(RESULT);
}
}
答:不錯的建議:「忘記第二個servlet」。在將它們添加到組合中之前,始終讓您的應用程序在servlet之外工作。 – 2011-05-17 20:54:12
B:HeaderFooter在相當長的一段時間內並未成爲iText的一部分。當然不是5.1的一部分。 – 2011-05-17 20:55:07
@Mark Storer感謝您的信息,我相應地更新了我的答案。 – I82Much 2011-05-17 22:02:06
- 1. 在VerticalFieldManager中設置頁眉和頁腳
- 2. 如何在詩詞(XWPF)中設置頁眉和頁腳位置?
- 3. 設置頁眉和頁腳在每一頁上重複
- 4. 在Vsmoraes PDF laravel5中設置頁眉和頁腳?
- 5. 在jquery mobile中設置頁眉和頁腳後的問題android
- 6. 在jquery mobile中設置頁眉和頁腳的有效方法
- 7. 如何在android中設置頁眉和頁腳?
- 8. 在BlackBerry OS 4.5中設置頁眉和頁腳
- 9. 在黑莓的彈出框中設置頁眉和頁腳
- 10. 如何在css中設置頁眉和頁腳?
- 11. 頁眉和頁腳在
- 12. 設置頁眉和頁腳reveal.js演示文稿
- 13. 設置頁眉
- 14. HTML文檔編輯器中的頁眉/頁腳/分頁
- 15. 如何在使用angularjs時設置頁眉和頁腳?
- 16. 如何在新的docx文檔中創建頁眉/頁腳?
- 17. 在graphviz中添加頁眉和頁腳
- 18. 在laravel中擴展頁眉和頁腳
- 19. html提取頁眉和頁腳來分隔文檔
- 20. 將頁眉和頁腳複製到所有其他文檔OpenXML
- 21. 爲許多單詞文檔添加頁眉和頁腳?
- 22. 使用Power Shell向Word文檔添加頁眉和頁腳
- 23. 頁眉和頁腳徽標
- 24. 粘性頁眉和頁腳
- 25. pdfkit頁眉和頁腳
- 26. 頁眉和頁腳衝突
- 27. 通用頁眉和頁腳
- 28. PHP頁眉和頁腳
- 29. 頁眉和頁腳XSL FO
- 30. NSCollectionView與頁眉和頁腳
可能重複(http://stackoverflow.com/questions/5062898/add-header-to-pdf-using-itext) – I82Much 2011-05-17 22:02:24