List of usage examples for com.lowagie.text.pdf PdfPCell getHorizontalAlignment
public int getHorizontalAlignment()
From source file:com.concursive.connect.web.modules.wiki.utils.WikiPDFUtils.java
License:Open Source License
private static boolean parseContent(WikiPDFContext context, Wiki wiki, String content, Document document, PdfPCell cell, Connection db, ArrayList<Integer> wikiListTodo, ArrayList<Integer> wikiListDone, float cellWidth) throws Exception { LOG.debug("PARSING CONTENT: " + content); // Parse the wiki page int lastIndent = 0; boolean preTag = false; boolean pre = false; boolean code = false; boolean header = true; try {//from w w w. j a v a2 s .c o m BufferedReader in = new BufferedReader(new StringReader(content)); String line = null; ArrayList unorderedParents = null; List thisList = null; Paragraph codeParagraph = null; while ((line = in.readLine()) != null) { // Tables if (line.startsWith("|")) { // @todo Close all ordered lists, unordered lists, and paragraphs // Parse the table line = parseTable(context, wiki, line, document, db, wikiListTodo, wikiListDone, in); if (line == null) { continue; } } // Forms if (line.startsWith("[{form")) { // @todo close any lists or paragraphs // parseForm operates over all the lines that make up the form, // it will have to look forward so it returns an unparsed line parseForm(context, db, in, line, document, cell); continue; } // Handle code blocks // @todo chunk the content similar to WikiToHTMLUtils otherwise inaccurate if (line.startsWith("<pre>") || line.startsWith("<code>")) { if (!code && !pre) { if (line.startsWith("<pre>")) { preTag = true; pre = true; } else if (line.startsWith("<code>")) { code = true; } codeParagraph = new Paragraph("", codeFont); codeParagraph.setSpacingBefore(10); if (pre && line.length() > ("<pre>").length()) { int endOfLine = line.length(); if (line.endsWith("</pre>")) { endOfLine = line.indexOf("</pre>"); } // This line has some extra content that needs to be added codeParagraph.add(new Chunk( line.substring(line.indexOf("<pre>") + 5, endOfLine) + Chunk.NEWLINE)); } if (code && line.length() > ("<code>").length()) { int endOfLine = line.length(); if (line.endsWith("</code>")) { endOfLine = line.indexOf("</code>"); } // This line has some extra content that needs to be added codeParagraph.add(new Chunk( line.substring(line.indexOf("<code>") + 6, endOfLine) + Chunk.NEWLINE)); } // See if this is a single line block if (preTag && line.endsWith("</pre>")) { // This is a single line block, so finish processing it } else if (code && line.endsWith("</code>")) { // This is a single line block, so finish processing it } else { // There are more lines to process, so do that continue; } } } if (line.startsWith("</code>") || line.endsWith("</code>")) { if (code) { code = false; if (line.indexOf("</code>") > 0 && !line.startsWith("<code>")) { // This line has some extra content that needs to be added codeParagraph .add(new Chunk(line.substring(0, line.indexOf("</code>")) + Chunk.NEWLINE)); } // Draw the final content PdfPTable codeTable = new PdfPTable(1); codeTable.setWidthPercentage(100); codeTable.setSpacingBefore(10); PdfPCell codeCell = new PdfPCell(codeParagraph); codeCell.setPadding(20); codeCell.setBorderColor(new Color(100, 100, 100)); codeCell.setBackgroundColor(new Color(200, 200, 200)); // codeCell.setNoWrap(true); codeTable.addCell(codeCell); LOG.debug("document.add(codeTable)"); document.add(codeTable); continue; } } if (line.startsWith("</pre>") || line.endsWith("</pre>")) { if (pre) { preTag = false; pre = false; if (line.indexOf("</pre>") > 0 && !line.startsWith("<pre>")) { // This line has some extra content that needs to be added codeParagraph.add(new Chunk(line.substring(0, line.indexOf("</pre>")) + Chunk.NEWLINE)); } // Draw the final content PdfPTable codeTable = new PdfPTable(1); codeTable.setWidthPercentage(100); codeTable.setSpacingBefore(10); PdfPCell codeCell = new PdfPCell(codeParagraph); codeCell.setPadding(20); codeCell.setBorderColor(new Color(100, 100, 100)); codeCell.setBackgroundColor(new Color(200, 200, 200)); // codeCell.setNoWrap(true); codeTable.addCell(codeCell); LOG.debug("document.add(codeTable)"); document.add(codeTable); continue; } } if (code || preTag) { // Append the chunk codeParagraph.add(new Chunk(line + Chunk.NEWLINE)); continue; } // Section if (line.startsWith("=") && line.endsWith("=")) { // @todo close any open lists or paragraphs int hCount = parseHCount(line, "="); if (hCount > 6) { hCount = 6; } String section = line.substring(line.indexOf("=") + hCount, line.lastIndexOf("=") - hCount + 1); header = true; context.foundHeader(hCount); String headerAnchor = null; // Store the h2's with anchors for table of contents or index if (hCount == 2) { headerAnchor = StringUtils.toHtmlValue(section).replace(" ", "_"); context.getHeaderAnchors().put(headerAnchor, section); } if (hCount == 3) { Paragraph title = new Paragraph(section.trim(), section2Font); title.setSpacingBefore(10); if (cell != null) { LOG.debug("phrase.add(title)"); cell.addElement(title); } else { LOG.debug("document.add(title)"); document.add(title); } } else if (hCount > 3) { Paragraph title = new Paragraph(section.trim(), section3Font); title.setSpacingBefore(10); if (cell != null) { LOG.debug("phrase.add(title)"); cell.addElement(title); } else { LOG.debug("document.add(title)"); document.add(title); } } else { Paragraph title = new Paragraph(section.trim(), sectionFont); title.setSpacingBefore(10); if (cell != null) { LOG.debug("phrase.add(title)"); cell.addElement(title); } else { LOG.debug("document.add(title)"); document.add(title); } } continue; } if (header) { header = false; if (line.trim().equals("")) { // remove the extra space a user may leave after a header continue; } } // Determine if this is a bulleted list if (line.startsWith("*") || line.startsWith("#")) { // Initialize the list array if (unorderedParents == null) { unorderedParents = new ArrayList(); // if (phrase != null) { // LOG.debug("phrase.add(new Paragraph(Chunk.NEWLINE))"); // phrase.add(new Paragraph(Chunk.NEWLINE)); // } else { // LOG.debug("document.add(new Paragraph(Chunk.NEWLINE))"); // document.add(new Paragraph(Chunk.NEWLINE)); // } } // Get the indent level boolean ol = line.startsWith("#"); int hCount = WikiPDFUtils.parseHCount(line, ol ? "#" : "*"); // Determine a shift in the tree if (lastIndent == 0) { if (ol) { thisList = new List(ol, 20); } else { thisList = new List(ol, 10); thisList.setListSymbol( new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 12))); } thisList.setIndentationLeft(36); thisList.setIndentationRight(36); unorderedParents.add(thisList); } else { if (hCount > lastIndent) { if (ol) { thisList = new List(ol, 20); } else { thisList = new List(ol, 10); thisList.setListSymbol( new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 12))); } thisList.setIndentationLeft(36); thisList.setIndentationRight(36); ((List) unorderedParents.get(unorderedParents.size() - 1)).add(thisList); unorderedParents.add(thisList); } else if (hCount < lastIndent) { unorderedParents.remove(unorderedParents.size() - 1); thisList = (List) unorderedParents.get(unorderedParents.size() - 1); } } lastIndent = hCount; // Append the item... Paragraph thisItem = new Paragraph(); parseLine(context, line.substring(hCount).trim(), thisItem, db, wikiListTodo, cellWidth, cell); thisList.add(new ListItem(thisItem)); continue; } // List is finished, so append it to the document before working on // other paragraphs if (unorderedParents != null) { if (cell != null) { LOG.debug("phrase.add((List) unorderedParents.get(0))"); cell.addElement((List) unorderedParents.get(0)); } else { LOG.debug("document.add((List) unorderedParents.get(0))"); document.add((List) unorderedParents.get(0)); } unorderedParents = null; thisList = null; lastIndent = 0; } // Otherwise a simple paragraph Paragraph paragraph = new Paragraph(); parseLine(context, line, paragraph, db, wikiListTodo, cellWidth, cell); if (cell != null) { LOG.debug("phrase.add(paragraph)"); if (cell.getHorizontalAlignment() == Element.ALIGN_CENTER) { paragraph.setAlignment(Element.ALIGN_CENTER); } paragraph.setSpacingBefore(5); cell.addElement(paragraph); } else { LOG.debug("document.add(paragraph)"); paragraph.setSpacingBefore(5); document.add(paragraph); } } // Cleanup now that the lines are finished if (pre || code) { PdfPTable codeTable = new PdfPTable(1); codeTable.setWidthPercentage(100); codeTable.setSpacingBefore(10); PdfPCell codeCell = new PdfPCell(codeParagraph); codeCell.setPadding(20); codeCell.setBorderColor(new Color(100, 100, 100)); codeCell.setBackgroundColor(new Color(200, 200, 200)); // codeCell.setNoWrap(true); codeTable.addCell(codeCell); LOG.debug("document.add(codeTable)"); document.add(codeTable); } if (unorderedParents != null) { if (cell != null) { LOG.debug("phrase.add((List) unorderedParents.get(0))"); cell.addElement((List) unorderedParents.get(0)); } else { LOG.debug("document.add((List) unorderedParents.get(0))"); document.add((List) unorderedParents.get(0)); } } in.close(); } catch (Exception e) { LOG.error("parseContent", e); } return true; }
From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.rtf.itext.PatchRtfCell.java
License:Open Source License
/** * Imports the Cell properties into the PatchRtfCell * * @param cell// w w w . j a v a2 s . c o m * The PdfPCell to import * @since 2.1.3 */ private void importCell(PdfPCell cell) { this.content = new ArrayList<RtfBasicElement>(); if (cell == null) { this.borders = new PatchRtfBorderGroup(this.document, PatchRtfBorder.CELL_BORDER, this.parentRow.getParentTable().getBorders()); return; } // padding this.cellPadding = (int) this.parentRow.getParentTable().getCellPadding(); this.cellPaddingBottom = cell.getPaddingBottom(); this.cellPaddingTop = cell.getPaddingTop(); this.cellPaddingRight = cell.getPaddingRight(); this.cellPaddingLeft = cell.getPaddingLeft(); // BORDERS this.borders = new PatchRtfBorderGroup(this.document, PatchRtfBorder.CELL_BORDER, cell.getBorder(), cell.getBorderWidth(), cell.getBorderColor()); // border colors this.border = cell.getBorder(); this.borderColor = cell.getBorderColor(); this.borderColorBottom = cell.getBorderColorBottom(); this.borderColorTop = cell.getBorderColorTop(); this.borderColorLeft = cell.getBorderColorLeft(); this.borderColorRight = cell.getBorderColorRight(); // border widths this.borderWidth = cell.getBorderWidth(); this.borderWidthBottom = cell.getBorderWidthBottom(); this.borderWidthTop = cell.getBorderWidthTop(); this.borderWidthLeft = cell.getBorderWidthLeft(); this.borderWidthRight = cell.getBorderWidthRight(); this.colspan = cell.getColspan(); this.rowspan = 1; // cell.getRowspan(); // if(cell.getRowspan() > 1) { // this.mergeType = MERGE_VERT_PARENT; // } this.verticalAlignment = cell.getVerticalAlignment(); if (cell.getBackgroundColor() == null) { this.backgroundColor = new RtfColor(this.document, 255, 255, 255); } else { this.backgroundColor = new RtfColor(this.document, cell.getBackgroundColor()); } // does it have column composite info? java.util.List compositeElements = cell.getCompositeElements(); if (compositeElements != null) { Iterator cellIterator = compositeElements.iterator(); // does it have column info? Paragraph container = null; while (cellIterator.hasNext()) { try { Element element = (Element) cellIterator.next(); // should we wrap it in a paragraph if (!(element instanceof Paragraph) && !(element instanceof List)) { if (container != null) { container.add(element); } else { container = new Paragraph(); container.setAlignment(cell.getHorizontalAlignment()); container.add(element); } } else { if (container != null) { RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(container); for (int i = 0; i < rtfElements.length; i++) { rtfElements[i].setInTable(true); this.content.add(rtfElements[i]); } container = null; } // if horizontal alignment is undefined overwrite // with that of enclosing cell if (element instanceof Paragraph && ((Paragraph) element).getAlignment() == Element.ALIGN_UNDEFINED) { ((Paragraph) element).setAlignment(cell.getHorizontalAlignment()); } RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(element); for (int i = 0; i < rtfElements.length; i++) { rtfElements[i].setInTable(true); this.content.add(rtfElements[i]); } } } catch (DocumentException de) { de.printStackTrace(); } } if (container != null) { try { RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(container); for (int i = 0; i < rtfElements.length; i++) { rtfElements[i].setInTable(true); this.content.add(rtfElements[i]); } } catch (DocumentException de) { de.printStackTrace(); } } } // does it have image info? Image img = cell.getImage(); if (img != null) { try { RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(img); for (int i = 0; i < rtfElements.length; i++) { rtfElements[i].setInTable(true); this.content.add(rtfElements[i]); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // does it have phrase info? Phrase phrase = cell.getPhrase(); if (phrase != null) { try { RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(phrase); for (int i = 0; i < rtfElements.length; i++) { rtfElements[i].setInTable(true); this.content.add(rtfElements[i]); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // does it have table info? PdfPTable table = cell.getTable(); if (table != null) { this.add(table); // try { // RtfBasicElement[] rtfElements = this.document.getMapper().mapElement(table); // for (int i = 0; i < rtfElements.length; i++) { // rtfElements[i].setInTable(true); // this.content.add(rtfElements[i]); // } // } catch (DocumentException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } } }