I'm using iText 2.1.7,i'm adding header and footer at absolute position using writer.getDirectcontent() method.All is working fine except the fonts in first page gets darker than second pages.
Note the Header and Footer in First and second pages
anyhelp?
Here is what i did
Reader stringReader = new StringReader(svgXmlDoc);
Document document = new Document(PageSize.A4.rotate());
ByteArrayOutputStream content = new ByteArrayOutputStream();
try {
PdfWriter writer = PdfWriter.getInstance(document, content);
document.open();
PdfService event = new PdfService();
event.setHeader(date.toString());
event.onOpenDocument(writer, document);
event.onEndPage(writer, document);
event.onCloseDocument(writer, document);
writer.setPageEvent(event);
Image image1 = Image.getInstance("Infinity-Loop.png");
image1.scalePercent(35);
PdfPTable signature = new PdfPTable(numberOfSignature);
signature.setTotalWidth(document.getPageSize().getWidth()
- document.leftMargin() - document.rightMargin());
signature.setLockedWidth(true);
signature.setWidthPercentage(100);
signature.getDefaultCell().setFixedHeight(20);
signature.getDefaultCell().setHorizontalAlignment(
Element.ALIGN_RIGHT);
for (int i = 0; i < numberOfSignature; i++) {
PdfPCell cell = new PdfPCell(new Paragraph(
String.valueOf("____________")));
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
signature.addCell(cell);
}
PdfPTable header = new PdfPTable(3);
float[] columnWidths = {1f,2f,1f};
header.setWidths(columnWidths);
header.setTotalWidth(document.getPageSize().getWidth()
- document.leftMargin() - document.rightMargin());
header.setLockedWidth(true);
header.setWidthPercentage(100);
header.getDefaultCell().setPadding(15);
header.getDefaultCell().setBorder(Rectangle.NO_BORDER);
PdfPCell logocell = new PdfPCell(image1, false);
logocell.setRowspan(3);
logocell.setFixedHeight(72);
logocell.setBorder(Rectangle.NO_BORDER);
header.addCell(logocell);
PdfPCell Title = new PdfPCell(new Phrase(title,
FontFactory.getFont(FontFactory.TIMES_ROMAN, 18,
Font.PLAIN, new Color(0, 0, 0))));
Title.setBorder(Rectangle.NO_BORDER);
Title.setHorizontalAlignment(Element.ALIGN_CENTER);
header.addCell(Title);
header.addCell("");
header.completeRow();
PdfPCell SubTitle = new PdfPCell(new Phrase(subTitle));
SubTitle.setBorder(Rectangle.NO_BORDER);
SubTitle.setHorizontalAlignment(Element.ALIGN_CENTER);
header.addCell(SubTitle);
header.addCell("");
header.completeRow();
PdfPCell dataRange = new PdfPCell(new Phrase(
dateRange));
dataRange.setBorder(Rectangle.NO_BORDER);
dataRange.setHorizontalAlignment(Element.ALIGN_CENTER);
header.addCell(dataRange);
header.addCell("");
header.completeRow();
ArrayList tmp = header.getRows(0, header.getRows().size());
header.getRows().clear();
header.getRows().addAll(tmp);
int width = (int) ((int) document.getPageSize().getWidth()
- document.leftMargin() - document.rightMargin());
int height = 350;
PdfContentByte cb = writer.getDirectContent();
PdfTemplate template = cb.createTemplate(width, height);
Graphics2D g2 = template.createGraphics(width, height);
PrintTranscoder prm = new PrintTranscoder();
TranscoderInput ti = new TranscoderInput(stringReader);
prm.transcode(ti, null);
PageFormat pg = new PageFormat();
Paper pp = new Paper();
pp.setSize(width, height);
pp.setImageableArea(0, 0, width, height);
pg.setPaper(pp);
prm.print(g2, pg, 0);
g2.dispose();
ImgTemplate img = new ImgTemplate(template);
img.setAbsolutePosition(document.left(), document.topMargin() + 100);
document.add(img);
signature.writeSelectedRows(0, -1, document.left(),
document.bottom() + document.bottomMargin() * 2,
writer.getDirectContent());
header.writeSelectedRows(0, -1, document.left(), document.top(),
writer.getDirectContent());
document.newPage();
PdfPTable statisticsTitle=new PdfPTable(1);
statisticsTitle.setTotalWidth(document.right() - document.left());
statisticsTitle.setWidthPercentage(100);
PdfPCell titleCell= new PdfPCell(new Paragraph("Statistics Report"));
titleCell.setBorder(Rectangle.NO_BORDER);
titleCell.setHorizontalAlignment(Element.ALIGN_CENTER);
statisticsTitle.addCell(titleCell);
statisticsTitle.writeSelectedRows(0, -1, document.left(), document.top()
- document.topMargin() * 2 - 10, writer.getDirectContent());
PdfPTable table;
if(measurementType.equals("Temp")){
table = new PdfPTable(6);
}
else{
table = new PdfPTable(11);
}
table.setTotalWidth(document.right() - document.left());
table.setWidthPercentage(100);
table.setHeaderRows(1);
PdfPCell h1 = new PdfPCell(new Paragraph("Description"));
h1.setGrayFill(0.7f);
h1.setRowspan(2);
h1.setVerticalAlignment(Element.ALIGN_MIDDLE);
h1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(h1);
PdfPCell h2 = new PdfPCell(new Paragraph("Temperature"));
h2.setGrayFill(0.7f);
h2.setColspan(5);
h2.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(h2);
if(measurementType.equals("TempHumi")){
PdfPCell h3 = new PdfPCell(new Paragraph("Humidity"));
h3.setGrayFill(0.7f);
h3.setColspan(5);
h3.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(h3);
}
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell tc1 = new PdfPCell(new Paragraph("Min"));
tc1.setGrayFill(0.7f);
tc1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(tc1);
PdfPCell tc2 = new PdfPCell(new Paragraph("Max"));
tc2.setGrayFill(0.7f);
tc2.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(tc2);
PdfPCell tc3 = new PdfPCell(new Paragraph("Avg"));
tc3.setGrayFill(0.7f);
tc3.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(tc3);
PdfPCell tc4 = new PdfPCell(new Paragraph("Std"));
tc4.setGrayFill(0.7f);
tc4.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(tc4);
PdfPCell tc5 = new PdfPCell(new Paragraph("Mkt"));
tc5.setGrayFill(0.7f);
tc5.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(tc5);
if(measurementType.equals("TempHumi")){
PdfPCell hc1 = new PdfPCell(new Paragraph("Min"));
hc1.setGrayFill(0.7f);
hc1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(hc1);
PdfPCell hc2 = new PdfPCell(new Paragraph("Max"));
hc2.setGrayFill(0.7f);
hc2.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(hc2);
PdfPCell hc3 = new PdfPCell(new Paragraph("Avg"));
hc3.setGrayFill(0.7f);
hc3.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(hc3);
PdfPCell hc4 = new PdfPCell(new Paragraph("Std"));
hc4.setGrayFill(0.7f);
hc4.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(hc4);
PdfPCell hc5 = new PdfPCell(new Paragraph("Mkt"));
hc5.setGrayFill(0.7f);
hc5.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(hc5);
}
ArrayList tmpTable = table.getRows(0, table.getRows().size());
table.getRows().clear();
table.getRows().addAll(tmpTable);
JsonObject listloggerdatastatistics = ReportService
.getStatisticsDetails(serialnumberlist);
System.out.println("Json "+listloggerdatastatistics.get("Sensormodellist"));
for (int row = 0; row < listloggerdatastatistics.get("Sensormodellist").getAsJsonArray().size(); row++) {
JsonObject jsonobj=listloggerdatastatistics.get("Sensormodellist").getAsJsonArray().get(row).getAsJsonObject();
if (row > 20 && row % 21 == 0) {
table.completeRow();
table.writeSelectedRows(0, 22, document.left(),
document.top() - document.topMargin() * 2 - 10,
writer.getDirectContent());
table.deleteBodyRows();
table.setSkipFirstHeader(true);
signature.writeSelectedRows(0, -1, document.left(),
document.bottom() + document.bottomMargin() * 3,
writer.getDirectContent());
header.writeSelectedRows(0, -1, document.left(),
document.top(), writer.getDirectContent());
document.newPage();
}
table.addCell(jsonobj.get("Modeltype").getAsString());
table.addCell(""+jsonobj.get("Mintemperature").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
table.addCell(""+jsonobj.get("Maxtemperature").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
table.addCell(""+jsonobj.get("Avgtemperature").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
table.addCell(""+jsonobj.get("Stdtemperature").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
table.addCell(""+jsonobj.get("Mkttemperature").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
if(measurementType.equals("TempHumi")){
table.addCell(""+jsonobj.get("Minhumidity").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
table.addCell(""+jsonobj.get("Maxhumidity").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
table.addCell(""+jsonobj.get("Avghumidity").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
table.addCell(""+jsonobj.get("Stdhumidity").getAsBigDecimal().setScale(2, RoundingMode.HALF_UP));
table.addCell("-");
}
}
table.completeRow();
table.writeSelectedRows(0, -1, document.left(), document.top()
- document.topMargin() * 3 - 10, writer.getDirectContent());
signature.writeSelectedRows(0, -1, document.left(),
document.bottom() + document.bottomMargin() * 2,
writer.getDirectContent());
header.writeSelectedRows(0, -1, document.left(), document.top(),
writer.getDirectContent());
} catch (DocumentException e) {
System.err.println(e);
}
document.close();
content.close();
I'm constructing graph image from SVG and table data are populated from database
Event handlers for adding footer with page X of Y
public class PdfService extends PdfPageEventHelper {
/** The header text. */
String header;
/** The template with the total number of pages. */
PdfTemplate total;
/**
* 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)
*/
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
total.reset();
ColumnText
.showTextAligned(total, Element.ALIGN_LEFT,
new Phrase(String.valueOf(writer.getPageNumber() - 1)),
2, 2, 0);
}
/**
* Adds a header to every page
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter,
* com.itextpdf.text.Document)
*/
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfPTable table = new PdfPTable(4);
try {
table.setWidths(new float[] { (float) (2.25), (float) (1.5), 11, 13 });
table.setTotalWidth(document.getPageSize().getWidth()
- document.leftMargin() - document.rightMargin());
table.setLockedWidth(true);
table.setWidthPercentage(100);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(String.format("Page %d of", writer.getPageNumber()));
Image img = Image.getInstance(total);
Chunk conte = new Chunk(img, 0, 0);
Phrase chunk = new Phrase(conte);
PdfPCell cell = new PdfPCell(chunk);
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_TOP);
table.addCell(cell);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell("SuperAdmin");
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(header);
table.writeSelectedRows(0, -1, document.leftMargin(),
document.bottom(), writer.getDirectContent());
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
/**
* 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)
*/
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(15, 14);
// total.setBoundingBox(new Rectangle(-1, 6, 20, 20));
}
/**
* Allows us to change the content of the header.
*
* @param header
* The new header String
*/
public void setHeader(String header) {
this.header = header;
}
}