List of usage examples for com.itextpdf.text.html.simpleparser StyleSheet StyleSheet
public StyleSheet()
From source file:com.framework.example.html2pdf.ConvertDemo.java
public static void htmlCodeComeFromFile(String filePath, String pdfPath) { Document document = new Document(); try {// w w w. j a v a2 s .co m StyleSheet st = new StyleSheet(); st.loadTagStyle("body", "leading", "16,0"); PdfWriter.getInstance(document, new FileOutputStream(pdfPath)); document.open(); ArrayList p = (ArrayList) HTMLWorker.parseToList(new FileReader(filePath), st); for (int k = 0; k < p.size(); ++k) { document.add((Element) p.get(k)); } document.close(); System.out.println("?"); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.masscustsoft.service.ToPdf.java
License:Open Source License
/** * Setter for the StyleSheet//from w w w . ja v a2s . c o m * @param style the StyleSheet */ public void setStyleSheet(StyleSheet style) { if (style == null) style = new StyleSheet(); this.style = style; }
From source file:de.unisiegen.informatik.bs.alvis.export.PdfExport.java
License:Open Source License
/** * adds organized, structured, highlighted source code to new paragraph and * returns it/*ww w . j a v a 2s .c o m*/ * * @author Sebastian Schmitz & Frank Weiler * @param sourceCode * the source code as string including html tags for highlighting * etc * @return a paragraph including the source code * @throws DocumentException * will be thrown when new paragraph could not have been added */ private Paragraph toParagraph(StyledText sourceCode) throws DocumentException { if (sourceCode == null) return null; String content = highlightStyleTextinHTML(sourceCode); // returnt den // nicht // eingerckten, // aber // gehighlighteten // Code Paragraph paragraph = new Paragraph(); if (content != null) { content = indentCode(content); // rckt den Code ein List<Element> bodyText; StyleSheet styles = new StyleSheet(); styles.loadTagStyle("ol", "leading", "16,0"); try { bodyText = HTMLWorker.parseToList(new StringReader(content), styles); paragraph.setFont(FontFactory.getFont("Courier", 10, Font.NORMAL)); for (Element elem : bodyText) { paragraph.add(elem); } } catch (IOException e) { paragraph.add(Messages.noSourceCodeAdded); } } return paragraph; }
From source file:de.unisiegen.informatik.bs.alvis.export.PdfExport.java
License:Open Source License
/** * adds one line of organized, structured, highlighted source code to new * paragraph and returns it//w ww . j ava2 s .c o m * * @author Sebastian Schmitz & Frank Weiler * @param sourceCode * the source code as string including html tags for highlighting * etc * @param wantedLineIndex * the wanted line number * @return a paragraph including the source code line * @throws DocumentException * will be thrown when new paragraph could not have been added */ private Paragraph toParagraph(StyledText sourceCode, int wantedLineIndex) throws DocumentException { if (sourceCode == null) return null; if (wantedLineIndex < 0 || wantedLineIndex >= sourceCode.getLineCount()) return null; String content = highlightStyleTextinHTML(sourceCode, wantedLineIndex); Paragraph paragraph = new Paragraph(); if (content != null) { List<Element> bodyText; StyleSheet styles = new StyleSheet(); styles.loadTagStyle("ol", "leading", "16,0"); try { bodyText = HTMLWorker.parseToList(new StringReader(content), styles); paragraph.setFont(FontFactory.getFont("Courier", 10, Font.NORMAL)); for (Element elem : bodyText) { paragraph.add(elem); } } catch (IOException e) { paragraph.add(Messages.noSourceCodeAdded); } } return paragraph; }
From source file:ilarkesto.integration.itext.Html.java
License:Open Source License
@Override public Element[] createITextElements(Document document) { String cssClassName = "itextpdfelement"; code = "<div class='" + cssClassName + "'>" + code + "</div>"; StyleSheet css = new StyleSheet(); css.loadStyle(cssClassName, "font-family", fontStyle.getFont()); css.loadStyle(cssClassName, "color", fontStyle.getColor().toString()); css.loadStyle(cssClassName, "font-size", APdfBuilder.mmToPoints(fontStyle.getSize()) + "pt"); css.loadStyle(cssClassName, "size", APdfBuilder.mmToPoints(fontStyle.getSize()) + "pt"); css.loadStyle(cssClassName, "line-height", "100em"); StringReader reader = new StringReader(code); List<Element> elements; HTMLWorker worker = new HTMLWorker(document); try {/* w ww .jav a 2s . c o m*/ elements = worker.parseToList(reader, css); } catch (IOException ex) { throw new RuntimeException(ex); } return Utl.toArray(elements, new Element[elements.size()]); }
From source file:net.apocalypselabs.symat.CodeExport.java
License:Open Source License
private void savePdfFile(String html, String path) { try {/*w ww . j a va2s .c o m*/ String k = html; try (OutputStream file = new FileOutputStream(new File(path))) { Document document = new Document(); PdfWriter.getInstance(document, file); document.open(); HTMLWorker htmlWorker = new HTMLWorker(document); StyleSheet styles = new StyleSheet(); styles.loadStyle("com", "color", "green"); styles.loadStyle("kwd", "color", "blue"); styles.loadStyle("pln", "color", "black"); styles.loadStyle("lit", "color", "#0099cc"); styles.loadStyle("pun", "color", "black"); styles.loadStyle("pun", "font-weight", "bold"); htmlWorker.setStyleSheet(styles); htmlWorker.parse(new StringReader(k)); document.close(); savedMsg(); } } catch (IOException | DocumentException e) { JOptionPane.showInternalMessageDialog(this, "Error saving: " + e.getMessage()); } }
From source file:net.sf.texprinter.generators.PDFGenerator.java
License:Open Source License
/** * Parses the HTML text to a list of elements. * /*from w w w . ja va 2 s. co m*/ * @param text The text. * @return A list of elements. * @throws IOException Throws an IOException if the StringReader couldn't * get the string provided. */ private static List<Element> getPostText(String text) throws IOException { // set the text to a snippet String snippet = text; // full code tag is not supported snippet = snippet.replaceAll("<pre><code>", "<pre>"); snippet = snippet.replaceAll("<pre class=.*\"><code>", "<pre>"); snippet = snippet.replaceAll("</code></pre>", "</pre>"); // code tag is not supported snippet = snippet.replaceAll("<code>", "<font face=\"Courier\">"); snippet = snippet.replaceAll("</code>", "</font>"); // add new lines snippet = snippet.replaceAll("\n", "<br/>"); // create a new stylesheet StyleSheet styles = new StyleSheet(); // configure lists styles.loadTagStyle("ul", "indent", "10"); styles.loadTagStyle("li", "leading", "14"); // configure hyperlinks styles.loadTagStyle("a", "color", "blue"); // create a map of providers HashMap providers = new HashMap(); // set the image provider providers.put("img_provider", new TeXImageFactory()); // parse the HTML to a list List<Element> objects = HTMLWorker.parseToList(new StringReader(snippet), styles, providers); // return the new list return objects; }
From source file:ro.nextreports.engine.exporter.PdfExporter.java
License:Apache License
private PdfPCell renderPdfCell(BandElement bandElement, Object value, int gridRow, int rowSpan, int colSpan, boolean image, int column) { Map<String, Object> style = buildCellStyleMap(bandElement, value, gridRow, column, colSpan); FontFactoryImp fact = new FontFactoryImp(); com.itextpdf.text.Font fnt;//from ww w. j a va2 s . c om if (bandElement != null) { fontName = (String) style.get(StyleFormatConstants.FONT_NAME_KEY); int size = ((Float) style.get(StyleFormatConstants.FONT_SIZE)).intValue(); fnt = getFont(size); } else { fnt = getFont(10); } PdfPCell cell; if (image) { if (value == null) { cell = new PdfPCell(new Phrase(IMAGE_NOT_FOUND)); } else { ImageBandElement ibe = (ImageBandElement) bandElement; try { byte[] imageBytes = getImage((String) value); cell = getImageCell(ibe, imageBytes, column, colSpan); } catch (Exception e) { cell = new PdfPCell(new Phrase(IMAGE_NOT_LOADED)); } } } else if (bandElement instanceof HyperlinkBandElement) { Hyperlink hyperlink = ((HyperlinkBandElement) bandElement).getHyperlink(); Anchor anchor = new Anchor(hyperlink.getText(), fnt); anchor.setReference(hyperlink.getUrl()); Phrase ph = new Phrase(); ph.add(anchor); cell = new PdfPCell(ph); } else if (bandElement instanceof ReportBandElement) { Report report = ((ReportBandElement) bandElement).getReport(); ExporterBean eb = null; try { eb = getSubreportExporterBean(report); PdfExporter subExporter = new PdfExporter(eb); subExporter.export(); PdfPTable innerTable = subExporter.getTable(); cell = new PdfPCell(innerTable); } catch (Exception e) { cell = new PdfPCell(); e.printStackTrace(); } finally { if ((eb != null) && (eb.getResult() != null)) { eb.getResult().close(); } } } else if ((bandElement instanceof VariableBandElement) && (VariableFactory .getVariable(((VariableBandElement) bandElement).getVariable()) instanceof TotalPageNoVariable)) { try { cell = new PdfPCell(Image.getInstance(total)); } catch (BadElementException e) { cell = new PdfPCell(new Phrase("NA")); } } else if (bandElement instanceof ImageColumnBandElement) { try { String v = StringUtil.getValueAsString(value, null); if (StringUtil.BLOB.equals(v)) { cell = new PdfPCell(new Phrase(StringUtil.BLOB)); } else { byte[] bytes = StringUtil.decodeImage(v); cell = getImageCell(bandElement, bytes, column, colSpan); } } catch (Exception e) { e.printStackTrace(); cell = new PdfPCell(new Phrase(IMAGE_NOT_LOADED)); } } else { String stringValue; if (style.containsKey(StyleFormatConstants.PATTERN)) { stringValue = StringUtil.getValueAsString(value, (String) style.get(StyleFormatConstants.PATTERN), getReportLanguage()); } else { stringValue = StringUtil.getValueAsString(value, null, getReportLanguage()); } if (stringValue == null) { stringValue = ""; } if (stringValue.startsWith("<html>")) { StringReader reader = new StringReader(stringValue); List<Element> elems = new ArrayList<Element>(); try { elems = HTMLWorker.parseToList(reader, new StyleSheet()); Phrase ph = new Phrase(); for (int i = 0; i < elems.size(); i++) { Element elem = (Element) elems.get(i); ph.add(elem); } cell = new PdfPCell(ph); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Phrase ph = new Phrase(stringValue, fnt); cell = new PdfPCell(ph); } } else { Phrase ph = new Phrase(stringValue, fnt); cell = new PdfPCell(ph); } } cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setUseDescender(true); // needed for a cell without padding cell.setMinimumHeight(MINIMUM_HEIGHT); // needed if there is a row in which all cells are empty if (bandElement != null) { cell.setRotation(bandElement.getTextRotation()); } if (colSpan > 1) { cell.setColspan(colSpan); } if (rowSpan > 1) { cell.setRowspan(rowSpan); } if (style != null) { updateFont(style, fnt); if (style.containsKey(StyleFormatConstants.BACKGROUND_COLOR)) { Color val = (Color) style.get(StyleFormatConstants.BACKGROUND_COLOR); cell.setBackgroundColor(new BaseColor(val)); } if (style.containsKey(StyleFormatConstants.HORIZONTAL_ALIGN_KEY)) { if (StyleFormatConstants.HORIZONTAL_ALIGN_LEFT .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); } if (StyleFormatConstants.HORIZONTAL_ALIGN_RIGHT .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); } if (StyleFormatConstants.HORIZONTAL_ALIGN_CENTER .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); } } if (style.containsKey(StyleFormatConstants.VERTICAL_ALIGN_KEY)) { if (StyleFormatConstants.VERTICAL_ALIGN_TOP .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_TOP); } if (StyleFormatConstants.VERTICAL_ALIGN_MIDDLE .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_MIDDLE); } if (StyleFormatConstants.VERTICAL_ALIGN_BOTTOM .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_BOTTOM); } } if (style.containsKey(StyleFormatConstants.PADDING_LEFT)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_LEFT); cell.setPaddingLeft(val); } if (style.containsKey(StyleFormatConstants.PADDING_RIGHT)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_RIGHT); cell.setPaddingRight(val); } if (style.containsKey(StyleFormatConstants.PADDING_TOP)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_TOP); cell.setPaddingTop(val); } if (style.containsKey(StyleFormatConstants.PADDING_BOTTOM)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_BOTTOM); cell.setPaddingBottom(val); } cell.setBorderWidth(0); if (style.containsKey(StyleFormatConstants.BORDER_LEFT)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_LEFT); cell.setBorderWidthLeft(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_LEFT_COLOR); cell.setBorderColorLeft(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_RIGHT)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_RIGHT); cell.setBorderWidthRight(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_RIGHT_COLOR); cell.setBorderColorRight(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_TOP)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_TOP); cell.setBorderWidthTop(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_TOP_COLOR); cell.setBorderColorTop(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_BOTTOM)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_BOTTOM); cell.setBorderWidthBottom(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_BOTTOM_COLOR); cell.setBorderColorBottom(new BaseColor(color)); } // for subreports we use default no wrap if (cell.getTable() == null) { cell.setNoWrap(true); if (bandElement != null) { if (bandElement.isWrapText()) { cell.setNoWrap(false); } } } // to see a background image all cells must not have any background! if (bean.getReportLayout().getBackgroundImage() != null) { cell.setBackgroundColor(null); } } return cell; }