List of usage examples for com.itextpdf.text.pdf PdfPTable getAbsoluteWidths
public float[] getAbsoluteWidths()
From source file:com.vectorprint.report.itext.TocOutputStream.java
License:Open Source License
private void printToc(Document d, PdfWriter w, VectorPrintDocument vpd) throws VectorPrintException, InstantiationException, IllegalAccessException, DocumentException { DocumentStyler ds = outer.getStylerFactory().getDocumentStyler(); if (ds.getValue(DocumentSettings.TOCAPPEND, Boolean.class)) { d.add(Chunk.NEXTPAGE);/*from w w w . ja va 2 s . com*/ } if (outer.isWasDebug()) { outer.getSettings().put(ReportConstants.DEBUG, Boolean.TRUE.toString()); PdfContentByte canvas = w.getDirectContent(); outer.startLayerInGroup(ReportConstants.DEBUG, canvas); BaseFont bf = DebugHelper.debugFont(canvas, outer.getSettings()); canvas.showTextAligned(Element.ALIGN_RIGHT, "FOR DEBUG INFO IN THE DOCUMENT TURN OFF TOC (-DocumentSettings.toc=false)", d.right(), d.getPageSize().getHeight() - ItextHelper.getTextHeight("F", bf, 8), 0); canvas.endLayer(); } ElementProducer ep = outer.getElementProducer(); StylerFactory sf = outer.getStylerFactory(); PdfPTable tocTable = ep.createElement(null, PdfPTable.class, sf.getStylers(DocumentSettings.TOCTABLEKEY)); for (Map.Entry<Integer, List<Section>> e : vpd.getToc().entrySet()) { String link = null; for (Section s : e.getValue()) { if (ds.isParameterSet(DocumentSettings.TOCMAXDEPTH) && ds.getValue(DocumentSettings.TOCMAXDEPTH, Integer.class) < s.getDepth()) { continue; } if (link == null) { link = s.getTitle().getContent(); } Chunk c = ep.createElement(s.getTitle().getContent(), Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)); if (ds.getValue(DocumentSettings.TOCDOTS, Boolean.class)) { float tw = ItextHelper.getTextWidth(c); float cw = tocTable.getAbsoluteWidths()[0]; float dw = ItextHelper.getTextWidth( ep.createElement(".", Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY))) * 1.5f; int numDots = (int) ((cw > tw) ? (cw - tw) / dw : 0); char[] dots = new char[numDots]; Arrays.fill(dots, '.'); c = ep.createElement(s.getTitle().getContent() + " " + String.valueOf(dots), Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)); } c.setLocalGoto(link); tocTable.addCell( ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY))); c = ep.createElement(e.getKey(), Chunk.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY)); c.setLocalGoto(link); tocTable.addCell( ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY))); } } d.add(tocTable); }