private void emit(String fontName, String encoding) throws DocumentException, IOException { String text = "" + "(defn year-end-evaluation\n" + " []\n" + " (if (> (rand) 0.5)\n" + " \"You get a raise!\"\n" + " \"Better luck next year!\"))"; BaseFont bf = BaseFont.createFont(fontName, encoding, BaseFont.EMBEDDED); document.add(new Paragraph(String.format("Font file: %s with encoding %s", fontName, encoding))); document.add(new Paragraph(String.format("iText class: %s", bf.getClass().getName()))); Font font = new Font(bf, 12); document.add(new Paragraph(text, font)); document.add(new LineSeparator(0.5f, 100, null, 0, -5)); }
@Override public void buildComponent(SerialisationContext pSerialisationContext, PDFSerialiser pSerialiser, EvaluatedHtmlPresentationNode pEvalNode) { Paragraph lParagraph = pSerialiser.getElementFactory().getParagraph(); LineSeparator lLineSeparator = pSerialiser.getElementFactory().getLineSeparator(); lParagraph.add(new Chunk(lLineSeparator)); pSerialiser.add(lParagraph); }
private void emit(String fontName, String encoding) throws DocumentException, IOException { BaseFont bf = BaseFont.createFont(fontName, encoding, BaseFont.EMBEDDED); document.add(new Paragraph(String.format("Font file: %s with encoding %s", fontName, encoding))); document.add(new Paragraph(String.format("iText class: %s", bf.getClass().getName()))); Font font = new Font(bf, 12); document.add(new Paragraph(TEXT, font)); document.add(new LineSeparator(0.5f, 100, null, 0, -5)); }
private ColumnText generateTableOfContent() { ColumnText ct = new ColumnText(null); Chunk CONNECT = new Chunk(new LineSeparator(0.5f, 95, configuration.defaultColor(), Element.ALIGN_CENTER, -.5f)); Paragraph paragraph = new Paragraph(); paragraph.setSpacingBefore(20.0f); // first paragraph only ct.addElement(new Paragraph("Table of content", configuration.chapterTitleFont())); ct.addElement(new Paragraph("")); for (TableOfContents.Entry entry : tableOfContents.getEntries()) { if (entry.isExtra()) continue; Chunk chunk = new Chunk(entry.getText(), configuration.tocEntryFont()); paragraph.add(chunk); paragraph.add(CONNECT); paragraph.add(new Chunk("" + entry.getFormattedPageNumber(), configuration.tocEntryFont())); float indent = 10.0f * entry.getLevel(); paragraph.setIndentationLeft(indent); ct.addElement(paragraph); paragraph = new Paragraph(); } return ct; }
/** * 直接把网页内容转为PDF文件 * * @param fileName * @throws Exception */ public static void parseURL2PDFFile(String pdfFile, String blogURL) throws Exception { BaseFont bfCN = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); // 中文字体定义 Font chFont = new Font(bfCN, 14, Font.NORMAL, BaseColor.BLUE); Font secFont = new Font(bfCN, 12, Font.NORMAL, new BaseColor(0, 204, 255)); Font textFont = new Font(bfCN, 12, Font.NORMAL, BaseColor.BLACK); Document document = new Document(); PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(pdfFile)); pdfwriter.setViewerPreferences(PdfWriter.HideToolbar); document.open(); String[] blogInfo = extractBlogInfo(blogURL); int chNum = 1; Chapter chapter = new Chapter(new Paragraph("URL转PDF测试", chFont), chNum++); Section section = chapter .addSection(new Paragraph(blogInfo[0], secFont)); section.setIndentation(10); section.setIndentationLeft(10); section.setBookmarkOpen(false); section.setNumberStyle(Section.NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT); section.add(new Chunk("分类:" + blogInfo[1] + " 日期:" + blogInfo[2], textFont)); LineSeparator line = new LineSeparator(1, 100, new BaseColor(204, 204, 204), Element.ALIGN_CENTER, -2); Paragraph p_line = new Paragraph(" "); p_line.add(line); section.add(p_line); section.add(Chunk.NEWLINE); document.add(chapter); // html文件 XMLWorkerHelper.getInstance().parseXHtml(pdfwriter, document, parse2Stream(blogInfo[3])); document.close(); }
/** * Create a line separator item with the factory element attributes applied * @return A line separator item with the element attributes applied */ public LineSeparator getLineSeparator() { LineSeparator lLineSeparator = new LineSeparator(); return lLineSeparator; }
private Chunk connectChunk(Styles styles) { return new Chunk(new LineSeparator(0.5f, 95, styles.defaultColor(), Element.ALIGN_CENTER, -.5f)); }
@Override public void process(int level, Node node, InvocationContext context) { List<Element> subs = context.collectChildren(level, node); SimpleNode tNode = (SimpleNode) node; SimpleNode.Type type = tNode.getType(); switch (type) { case Apostrophe: context.append(new Chunk("'")); return; case Ellipsis: context.append(new Chunk("\u2026")); return; case Emdash: context.append(new Chunk("\u2014")); return; case Endash: context.append(new Chunk("\u2013")); return; case HRule: LineSeparator lineSeparator = new LineSeparator(); lineSeparator.setLineColor(lineColor); lineSeparator.setLineWidth(lineWidth); Paragraph p = new Paragraph(); p.add(lineSeparator); p.add(Chunk.NEWLINE); context.append(p); return; case Linebreak: int len = tNode.getEndIndex() - tNode.getStartIndex(); StringBuilder b = new StringBuilder(); for(int i=0;i<(len - 1);i++) { b.append(' '); } b.append('\n'); context.append(new Chunk(b.toString())); return; case Nbsp: context.append(new Chunk("\u00a0")); return; default: log.warn("Unsupported type '{}'", type); context.appendAll(subs); } }