在iText我只需要一个数字:文件中所有页面的计数。
我有这个类,我从iText的某个地方得到的模式:
class PageCounter extends PdfPageEventHelper {
PdfTemplate t;
Image total;
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
t = writer.getDirectContent().createTemplate(30, 16);
try {
total = Image.getInstance(t);
total.setRole(PdfName.ARTIFACT);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table = new PdfPTable(3);
try {
table.setWidths(new int[] { 24, 24, 2 });
table.setTotalWidth(tableWidth);
Font font = returnCustomFont(fontTypeRegular, fontSizeRegular,
BaseColor.BLACK);
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy / HH:mm");
PdfPCell cell1 = new PdfPCell(
new Phrase(dateFormat.format(new Date()), font));
cell1.setVerticalAlignment(Element.ALIGN_TOP);
cell1.setBorder(Rectangle.NO_BORDER);
cell1.setFixedHeight(30);
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase(
String.format("Page %d of", writer.getPageNumber()), font));
cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell2.setBorder(Rectangle.NO_BORDER);
cell2.setFixedHeight(30);
cell2.setVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(total);
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setBorder(Rectangle.NO_BORDER);
cell3.setVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell3);
PdfContentByte canvas = writer.getDirectContent();
canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
table.writeSelectedRows(0, -1, 30, 40, canvas);
canvas.endMarkedContentSequence();
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(t, Element.ALIGN_LEFT,
new Phrase(String.valueOf(writer.getPageNumber())), 2, 2, 0);
}
}
麻烦的是,它只将所有页面的数量作为图像格式导出,我需要它作为文本格式(Integer,String等)。
我发现的唯一一件事是stackoverflow中的这个问题: How to add total page number on every page with iText?
第一个答案很有意思,但不幸的是,没有例子,只有这个描述(在行间):
1)首先使用伪页面数将PdfWriter的输出处理为字节流。
2)从该字节流创建一个PdfReader,调用PdfReader.getNumberOfPages来获取实际的页数。
3)重新创建PDF输出,知道页数是多少,相应地更改页脚。
如果没有人有更好的计划如何实现该页数,我会感兴趣,具体如何处理POINT NUMBER 1。 你有什么想法吗?
注意:当我开始输入当前页面的编号时,我已经需要知道所有页面的数量并在那里输入。
我的整个代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfName;
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 Pdf1formiText {
public static final String DEST = "c:/radek-folder/pdf1iText.pdf";
protected int horizontalAlignmentLeft = Element.ALIGN_LEFT;
protected int horizontalAlignmentCenter = Element.ALIGN_CENTER;
protected int verticalAlignmentMiddle = Element.ALIGN_MIDDLE;
protected int verticalAlignmentTop = Element.ALIGN_TOP;
protected String fontTypeRegular = "c:/radek-folder/font_sitebook.ttf";
protected String fontTypeBold = "c:/radek-folder/font_sitebook_bold.ttf";
protected float fontSizeRegular = 10f;
protected float fontSizeLarger = 14f;
protected float fontSizeLarge = 16f;
float tableWidth;
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new Pdf1formiText().createPdf(DEST);
System.out.println("done");
}
public void createPdf(String dest) throws IOException, DocumentException {
float[] columns = { 100, 50, 100, 50, 50, 50, 50, 50, 75, 50, 50, 50 };
Document document = new Document(PageSize.A4.rotate(), 36, 36, 36, 36);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
writer.setPageEvent(new PageCounter());
document.open();
PdfPCell cell;
// Header part
PdfPTable mainTable = new PdfPTable(1);
mainTable.getDefaultCell().setPadding(0);
mainTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
mainTable.setTotalWidth(columns);
mainTable.setLockedWidth(true);
PdfPTable subTable1 = new PdfPTable(columns.length);
subTable1.setTotalWidth(columns);
subTable1.setLockedWidth(true);
cell = createAndReturnTextCell(horizontalAlignmentCenter, verticalAlignmentMiddle,
"Přenosové trasy GTS", columns.length, 1, fontTypeBold, fontSizeLarge,
BaseColor.BLACK);
cell.setPaddingTop(10f);
cell.setPaddingBottom(15f);
subTable1.addCell(cell);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Trasa [-]", 1, 1, fontTypeBold, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Pásmo [GHz]", 1, 1, fontTypeBold, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Kap a konf [Mbit/s]", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Typ zařízení Index [-]", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Zeměpis.délka WGS84[°-'-\"]", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Zeměpis.šířka WGS84[°-'-\"]", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Nadm.výška [m]", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Výška ant.[m]", 1, 1, fontTypeBold, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Průměr ant./zisk [m]/[dB]", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Útlum vlnovod [dB]", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Výkon vysílač dBm", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable1, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Délka trasy [m]", 1, 1, fontTypeBold, fontSizeRegular,
PdfPCell.NO_BORDER);
mainTable.addCell(subTable1);
PdfPTable subTable2 = new PdfPTable(columns.length);
subTable2.setTotalWidth(columns);
subTable2.setLockedWidth(true);
addTableCellToTable(subTable2, innerTableInCell1(), 10, 1);
addTextCellToTable(subTable2, horizontalAlignmentCenter, verticalAlignmentMiddle,
"2959.32", 1, 2, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(subTable2, horizontalAlignmentCenter, verticalAlignmentMiddle,
"2009-03-30", 1, 2, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
mainTable.addCell(subTable2);
PdfPTable subTable3 = new PdfPTable(columns.length);
subTable3.setTotalWidth(columns);
subTable3.setLockedWidth(true);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"CZ5142", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"38", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"100M", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"QAM_32", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"28", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"FibeAir 1500p 38GHz", 1, 1, fontTypeRegular, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"N/A", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Praha 4 Milevská 7", 1, 1, fontTypeRegular, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"14°26'18''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"14°26'18''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"265", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable3, horizontalAlignmentCenter, verticalAlignmentMiddle,
"70", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
mainTable.addCell(subTable3);
for (int i = 0; i < 200; i++) {
PdfPTable subTable4 = new PdfPTable(columns.length);
subTable4.setTotalWidth(columns);
subTable4.setLockedWidth(true);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"CZ5142", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"38", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"100M", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"QAM_32", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"28", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"FibeAir 1500p 38GHz", 1, 1, fontTypeRegular, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"N/A", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Praha 4 Milevská 7", 1, 1, fontTypeRegular, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"14°26'18''", 1, 1, fontTypeRegular, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"14°26'18''", 1, 1, fontTypeRegular, fontSizeRegular,
PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"265", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(subTable4, horizontalAlignmentCenter, verticalAlignmentMiddle,
"70", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.NO_BORDER);
mainTable.addCell(subTable4);
}
document.add(mainTable);
document.close();
}
private PdfPTable innerTableInCell1() throws DocumentException {
int[] columnWidths = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
PdfPTable innertable = new PdfPTable(columnWidths.length);
innertable.setWidths(columnWidths);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"Brno Špitálka 8 Brno Hájecká 1068/14 CZ5159", 1, 2, fontTypeRegular,
fontSizeRegular, PdfPCell.NO_BORDER);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"38510.5", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"37250.5", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"MINI-LINK/E 38GHz", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"V / 4x2M / 1+0", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"16?37'21.4''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"16?38'16.94''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"49?11'47.3''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"49?10'18.75''", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"217", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"201", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"92", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"10", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"0.30/40.4", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"0.60/44.3", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"UKY21008/SC11", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"UKY21009/SC11", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"0", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"0", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"15", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
addTextCellToTable(innertable, horizontalAlignmentCenter, verticalAlignmentMiddle,
"15", 1, 1, fontTypeRegular, fontSizeRegular, PdfPCell.BOX);
return innertable;
}
private static void addTableCellToTable(PdfPTable mainTable, PdfPTable insertedTable, int colspan,
int rowspan) {
PdfPCell cell = new PdfPCell(insertedTable);
cell.setColspan(colspan);
cell.setRowspan(rowspan);
mainTable.addCell(cell);
}
private static void addTextCellToTable(PdfPTable table, int horizontalAlignment,
int verticalAlignment, String value, int colspan, int rowspan,
String fontType, float fontSize, int borderStyle) {
BaseFont base = null;
try {
base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(base, fontSize);
PdfPCell cell = new PdfPCell(new Phrase(value, font));
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setVerticalAlignment(verticalAlignment);
cell.setBorder(borderStyle);
table.addCell(cell);
}
private static PdfPCell createAndReturnTextCell(int horizontalAlignment,
int verticalAlignment, String value, int colspan, int rowspan,
String fontType, float fontSize, BaseColor color) {
BaseFont base = null;
try {
base = BaseFont.createFont(fontType, BaseFont.CP1250, BaseFont.EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(base, fontSize);
font.setColor(color);
PdfPCell cell = new PdfPCell(new Phrase(value, font));
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setVerticalAlignment(verticalAlignment);
cell.setBorder(PdfPCell.NO_BORDER);
return cell;
}
private static void addImageCellToTable(PdfPTable table, int horizontalAlignment,
int verticalAlignment, Image value, int colspan, int rowspan, String fontType,
float fontSize) {
PdfPCell cell = new PdfPCell(value);
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setHorizontalAlignment(horizontalAlignment);
cell.setVerticalAlignment(verticalAlignment);
cell.setBorder(PdfPCell.NO_BORDER);
table.addCell(cell);
}
class PageCounter extends PdfPageEventHelper {
PdfTemplate t;
Image total;
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
t = writer.getDirectContent().createTemplate(30, 16);
t.setColorFill(BaseColor.LIGHT_GRAY);
try {
total = Image.getInstance(t);
total.setRole(PdfName.ARTIFACT);
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table = new PdfPTable(2);
try {
table.setWidths(new int[] { 48, 2 });
table.setTotalWidth(tableWidth);
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPCell cell = createAndReturnTextCell(Element.ALIGN_RIGHT,
verticalAlignmentTop,
String.format("%d /", writer.getPageNumber()), 1, 1,
fontTypeRegular, 13f, BaseColor.LIGHT_GRAY);
cell.setFixedHeight(30);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
addImageCellToTable(table, Element.ALIGN_RIGHT, verticalAlignmentTop,
total, 1, 1, fontTypeRegular, fontSizeRegular);
PdfContentByte canvas = writer.getDirectContent();
canvas.beginMarkedContentSequence(PdfName.ARTIFACT);
table.writeSelectedRows(0, -1, 40, 40, canvas);
canvas.endMarkedContentSequence();
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
ColumnText.showTextAligned(t, Element.ALIGN_LEFT,
new Phrase(String.valueOf(writer.getPageNumber())), 1, 1, 0);
}
}
}