Код, который я написал до сих пор, ниже:
public class HeaderFooterEvent extends PdfPageEventHelper {
@Override
public void onStartPage(PdfWriter writer, Document document) {
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
addHeader(writer, document);
addHeader2(writer, document);
addFooter(writer, document);
}
void addHeader(PdfWriter writer, Document document){
try {
PdfContentByte cb = writer.getDirectContent();
// the initial rectangle defines the max size of the content
rect = new Rectangle(10, document.getPageSize().getBottom() + 50,
document.getPageSize().getWidth() - 2 * 10, document.getPageSize().getTop() - 50);
// flip the rectangle if top and bottom were switched
rect.normalize();
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(rect);
ct.addElement(createTable1(auditBundle, context));
// do a simulation run
int result = ct.go(true);
// assume the content fits in the initial rectangle
if (result == ColumnText.NO_MORE_TEXT) {
// the bottom of the simulated content
float verticalpos = ct.getYLine();
// redefine the rectangle based on the simulation
rect = new Rectangle(10, verticalpos, document.getPageSize().getWidth() - 2 * 10,
document.getPageSize().getTop() - 50);
ct.setSimpleColumn(rect);
// the original content was consumed in the simulation, so add it again
ct.addElement(createTable1(auditBundle, context));
// render again
ct.go(false);
// draw the rectangle
rect.setBorder(Rectangle.BOX);
rect.setBorderWidth(1);
rect.setBorderColor(BaseColor.BLACK);
cb.rectangle(rect);
}
} catch (DocumentException e){
e.printStackTrace();
}
}
void addHeader2(PdfWriter writer, Document document){
try {
float fntSize, lineSpacing;
fntSize = 20f;
lineSpacing = 15f;
String title = "", recordedBy = "", entity = "", orgUnit = "", actualStartDate = "",
actualStartTime = "";
Paragraph paragraph = new Paragraph();
paragraph.add(new Phrase(lineSpacing, title + " " + recordedBy + " " + entity + " " +
orgUnit + " " + actualStartDate + " " + actualStartTime,
FontFactory.getFont(FontFactory.HELVETICA, fntSize)));
paragraph.setAlignment(Element.ALIGN_CENTER);
paragraph.setPaddingTop(5);
PdfContentByte cb = writer.getDirectContent();
// the initial rectangle defines the max size of the content
Rectangle rect2 = new Rectangle(10, document.getPageSize().getBottom() + 50,
document.getPageSize().getWidth() - 2 * 10, document.getPageSize().getTop() -
250);
// flip the rectangle if top and bottom were switched
rect2.normalize();
ColumnText ct = new ColumnText(cb);
ct.setSimpleColumn(rect2);
ct.addElement(paragraph);
// do a simulation run
int result = ct.go(true);
// assume the content fits in the initial rectangle
if (result == ColumnText.NO_MORE_TEXT) {
// the bottom of the simulated content
float verticalpos = ct.getYLine()+rect.getHeight();
// redefine the rectangle based on the simulation
rect2 = new Rectangle(10, verticalpos, document.getPageSize().getWidth() - 2 * 10,
document.getPageSize().getTop() - 250);
ct.setSimpleColumn(rect2);
// the original content was consumed in the simulation, so add it again
ct.addElement(paragraph);
// render again
ct.go(false);
// draw the rectangle
rect2.setBorder(Rectangle.BOX);
rect2.setBorderWidth(1);
rect2.setBorderColor(BaseColor.BLACK);
cb.rectangle(rect2);
}
} catch (DocumentException e){
e.printStackTrace();
}
}
void addFooter(PdfWriter writer, Document document){
String text = "Report printed by %1$s at %2$s on %3$s CMO COMPLIANCE Management Software by www.cmo-compliance.com";
Date currentTime = Calendar.getInstance().getTime();
String currentDate = DateTimeFormatter.getUserTZDateFormat(
DateTimeFormatter.CUSTOM_DATE_TEMPLATE_DD_MMM_YYYY
).format(currentTime);
String currentTimes = DateTimeFormatter.getUserTZDateFormat(
DateTimeFormatter.CUSTOM_24H_TIME_TEMPLATE_H_MM
).format(currentTime);
PdfContentByte cb = writer.getDirectContent();
Rectangle pageSize = document.getPageSize();
float left = document.leftMargin();
float right = document.rightMargin();
float bottom = document.bottomMargin();
Rectangle rect = new Rectangle(
pageSize.getLeft() + left, pageSize.getBottom(),
pageSize.getRight() - right, pageSize.getBottom() + bottom);
cb.rectangle(rect);
ColumnText ct = new ColumnText(writer.getDirectContent());
ct.setSimpleColumn(rect);
ct.addElement(new Paragraph(String.format(text, CmoApplication.getInstance().getUser().loginName, currentDate, currentTimes)));
try {
ct.go();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
Затем для абзаца я добавил данные, как показано ниже: Документ документа = new Document ();
// Location to save
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// Open to write
document.open();
// Document Settings
document.setPageSize(PageSize.A4);
document.addCreationDate();
document.addAuthor(CmoApplication.getInstance().getCompany().companyName);
document.addCreator(CmoApplication.getInstance().getUser().loginName);
HeaderFooterEvent event = new HeaderFooterEvent();
writer.setPageEvent(event);
Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC);
Font paragraphFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
Chunk chunk = new Chunk("This is the title", chapterFont);
Chapter chapter = new Chapter(new Paragraph(chunk), 1);
chapter.setNumberDepth(0);
chapter.add(new Paragraph("This is the paragraph", paragraphFont));
document.add(chapter);
document.close();
Я не могу понять, как добавить разделы, как показано на изображении. Также я хочу, чтобы данные автоматически корректировались на нескольких страницах в соответствии с текстом. Буду признателен, если кто-нибудь сможет на это ответить. Спасибо.