I wanted to generate the pdf with page numbers and also having table of content which has page numbers in it. I am up to generating PDF but cannot add page numbers to it.
ol.anchorLink a::after {
content: leader('.') target-counter(attr(href), page);
}
@page {
@bottom-right {
padding-right:20px;
content: "Page " counter(page);
}
}
this piece of cs is not woking in the dependency i am using which is itextpdf. so I have made changes in the java file
@Override
public DataHandler getPDFVersion(StringBuffer htmlSrc, String documentTitle) throws Exception {
DataHandler dataHandler = null;
String outputFile = "test.pdf";
try (InputStream inputStream = new ByteArrayInputStream(htmlSrc.toString().getBytes());
OutputStream os = new FileOutputStream(new File(outputFile));) {
ConverterProperties converterProperties = new ConverterProperties();
PdfWriter writer = new PdfWriter(os, new WriterProperties().setFullCompressionMode(true));
PdfDocument pdfDoc = new PdfDocument(writer);
List<IElement> elements = HtmlConverter.convertToElements(inputStream, converterProperties);
Document document = new Document(pdfDoc);
document.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters() {
@Override
public boolean isSplitCharacter(GlyphLine text, int glyphPos) {
return true;
}
});
for (IElement element : elements) {
if (element instanceof AreaBreak) {
document.add((AreaBreak) element);
} else {
document.add((IBlockElement) element);
}
}
int n = pdfDoc.getNumberOfPages();
for(int i=1; i<=n; i++)
{
document.showTextAligned(new Paragraph(String.format("page %s of %s", i, n)),
559, 806, i, TextAlignment.RIGHT, VerticalAlignment.TOP, 0);
}
int g= pdfDoc.getNumberOfPages();
document.close();
dataHandler = new DataHandler(new FileDataSource(outputFile));
} catch (Exception e) {
//do not throw error
log.error("Error @ getPDFVersion() :" + e);
}
return dataHandler;
}
and When the run the PDF I am getting exception saying com.itextpdf.kernel.PdfException: Cannot draw elements on already flushed pages. so I have moved code before IELEMENT there I am able to print the page numbers of pdf but If I hard code the value. page number is 0 I think may be PDF is not generated by then how to solve this issue?