I want to add a header image and page numbers as footer to my PDF file. How can I add header and footer to my PDF file using Itext?
我想在我的PDF文件中添加页眉图像和页码作为页脚。如何使用Itext将页眉和页脚添加到我的PDF文件?
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Header;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class Demo {
/**
* @param args
*/
public static void main(String[] args) {
new Demo().createPDF();
}
public void createPDF(){
Document document = new Document (PageSize.A4);
try {
PdfWriter.getInstance(document, new FileOutputStream("C:/Documents and Settings/mnavya/Desktop/AddImage.pdf"));
document.open ();
document.addCreator("Binod by Demo.java");
document.addAuthor("Binod Suman");
document.addTitle("First PDF By Binod");
Image image = Image.getInstance("https://snaplogic-h.s3.amazonaws.com/uploads/partner/logo/24/stratappa.jpg?u=1382443264");
image.scaleAbsolute(50,50);
document.add(image);
Paragraph paragraph = new Paragraph(
" Factors",new Font(Font.FontFamily.HELVETICA, 25));
document.add(paragraph);
document.add(Chunk.SPACETABBING);
PdfPTable table = new PdfPTable(8);
table.setWidthPercentage(100);
float[] columnWidths = new float[] { 7, 20, 9, 9, 9, 9, 5, 3 };
table.setWidths(columnWidths);
PdfPCell cell = new PdfPCell();
cell.setColspan(8);
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
table.addCell(" ");
table.addCell(" ");
table.addCell("");
table.addCell("");
table.addCell("%");
table.addCell("");
table.addCell(" ");
table.addCell(" ");
document.add(table);
document.close ();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("******** PDF Created ***************");
}
}
4 个解决方案
#1
11
Footer Header utils:
页脚标题工具:
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
public class HeaderFooterPageEvent extends PdfPageEventHelper {
public void onStartPage(PdfWriter writer, Document document) {
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Top Left"), 30, 800, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Top Right"), 550, 800, 0);
}
public void onEndPage(PdfWriter writer, Document document) {
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("http://www.xxxx-your_example.com/"), 110, 30, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("page " + document.getPageNumber()), 550, 30, 0);
}
}
Use HeaderFooterPageEvent:
使用HeaderFooterPageEvent:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4, 20, 20, 50, 25);
PdfWriter writer = PdfWriter.getInstance(document, baos);
HeaderFooterPageEvent event = new HeaderFooterPageEvent();
writer.setPageEvent(event);
#2
4
HeaderFooter event = new HeaderFooter();
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
writer.setPageEvent(event);
记录itxt
ejemplo example of PDF
ejemplo PDF的例子
#3
3
Please take a look at the official iText documentation before posting a question on *. More specifically: check the examples for the keyword header / footer.
在*上发布问题之前,请先查看官方的iText文档。更具体地说:检查关键字页眉/页脚的示例。
You'll find the MovieCountries1 example that creates a PDF with a header that has page numbers Page 1 / 39, Page 2 / 39, etc.
您将找到MovieCountries1示例,该示例创建一个带有页码的PDF,页面编号为Page 1/39,Page 2/39等。
Use that example as inspiration. If you don't need a Page X of Y header (or footer). You can use a more simple example, such as the one in the answer to How to add Header and Footer in dynamic pdf using iTextLibrary? (which was also inspired by one of the examples of my book).
使用该示例作为灵感。如果您不需要Y页眉(或页脚)的页面X.您可以使用更简单的示例,例如如何使用iTextLibrary在动态pdf中添加页眉和页脚的答案中的示例? (这也是我书中的一个例子的启发)。
#4
-1
I am using iText2. Although PdfPageEventHelper can solve it, HeaderFooter seems more easier. Make sure add HeaderFooter before document.open(), otherwise first page will not display it.
我正在使用iText2。虽然PdfPageEventHelper可以解决它,但HeaderFooter似乎更容易。确保在document.open()之前添加HeaderFooter,否则第一页将不会显示它。
Document document = new Document(PageSize.A4, MARGIN, MARGIN, MARGIN, MARGIN);
HeaderFooter footer = new HeaderFooter(new Phrase("- "), new Phrase(" -"));
footer.setAlignment(Element.ALIGN_CENTER);
footer.setBorder(Rectangle.NO_BORDER);
document.setFooter(footer);
#1
11
Footer Header utils:
页脚标题工具:
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
public class HeaderFooterPageEvent extends PdfPageEventHelper {
public void onStartPage(PdfWriter writer, Document document) {
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Top Left"), 30, 800, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("Top Right"), 550, 800, 0);
}
public void onEndPage(PdfWriter writer, Document document) {
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("http://www.xxxx-your_example.com/"), 110, 30, 0);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("page " + document.getPageNumber()), 550, 30, 0);
}
}
Use HeaderFooterPageEvent:
使用HeaderFooterPageEvent:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4, 20, 20, 50, 25);
PdfWriter writer = PdfWriter.getInstance(document, baos);
HeaderFooterPageEvent event = new HeaderFooterPageEvent();
writer.setPageEvent(event);
#2
4
HeaderFooter event = new HeaderFooter();
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
writer.setPageEvent(event);
记录itxt
ejemplo example of PDF
ejemplo PDF的例子
#3
3
Please take a look at the official iText documentation before posting a question on *. More specifically: check the examples for the keyword header / footer.
在*上发布问题之前,请先查看官方的iText文档。更具体地说:检查关键字页眉/页脚的示例。
You'll find the MovieCountries1 example that creates a PDF with a header that has page numbers Page 1 / 39, Page 2 / 39, etc.
您将找到MovieCountries1示例,该示例创建一个带有页码的PDF,页面编号为Page 1/39,Page 2/39等。
Use that example as inspiration. If you don't need a Page X of Y header (or footer). You can use a more simple example, such as the one in the answer to How to add Header and Footer in dynamic pdf using iTextLibrary? (which was also inspired by one of the examples of my book).
使用该示例作为灵感。如果您不需要Y页眉(或页脚)的页面X.您可以使用更简单的示例,例如如何使用iTextLibrary在动态pdf中添加页眉和页脚的答案中的示例? (这也是我书中的一个例子的启发)。
#4
-1
I am using iText2. Although PdfPageEventHelper can solve it, HeaderFooter seems more easier. Make sure add HeaderFooter before document.open(), otherwise first page will not display it.
我正在使用iText2。虽然PdfPageEventHelper可以解决它,但HeaderFooter似乎更容易。确保在document.open()之前添加HeaderFooter,否则第一页将不会显示它。
Document document = new Document(PageSize.A4, MARGIN, MARGIN, MARGIN, MARGIN);
HeaderFooter footer = new HeaderFooter(new Phrase("- "), new Phrase(" -"));
footer.setAlignment(Element.ALIGN_CENTER);
footer.setBorder(Rectangle.NO_BORDER);
document.setFooter(footer);