use of com.lowagie.text.BadElementException in project mdw-designer by CenturyLinkCloud.
the class ExportHelper method generateElementHtml.
private Object generateElementHtml(Element element, int depth, Font font) {
String tag = element.getName();
Object myself;
Object av;
Font vFont = font;
if (element instanceof HTMLDocument.RunElement) {
HTMLDocument.RunElement re = (HTMLDocument.RunElement) element;
int start = re.getStartOffset();
int end = re.getEndOffset();
try {
String content = re.getDocument().getText(start, end - start);
HtmlAttr htmlattr = printAttributesHtml(re);
av = re.getAttribute(CSS.Attribute.FONT_SIZE);
String fontsize = av == null ? null : av.toString();
av = re.getAttribute(CSS.Attribute.FONT_FAMILY);
String fontfamily = av == null ? null : av.toString();
av = re.getAttribute(CSS.Attribute.COLOR);
String fontcolor = av == null ? null : av.toString();
if (fontcolor != null || fontsize != null || fontfamily != null) {
if (fontfamily == null)
fontfamily = font.getFamilyname();
if (fontsize != null && fontsize.endsWith("pt"))
fontsize = fontsize.substring(0, fontsize.indexOf("pt"));
float size = fontsize == null ? font.getSize() : (Float.parseFloat(fontsize) + 8);
int style = font.getStyle();
Color color;
if (fontcolor != null) {
color = Color.decode(fontcolor);
} else
color = font.getColor();
vFont = FontFactory.getFont(fontfamily, size, style, color);
} else if (htmlattr.bold || htmlattr.italic) {
String family = font.getFamilyname();
float size = font.getSize();
Color color = font.getColor();
if (htmlattr.bold && htmlattr.italic)
vFont = FontFactory.getFont(family, size, Font.BOLDITALIC, color);
else if (htmlattr.italic)
vFont = FontFactory.getFont(family, size, Font.ITALIC, color);
else if (htmlattr.bold)
vFont = FontFactory.getFont(family, size, Font.BOLD);
}
myself = new Chunk(content, vFont);
} catch (BadLocationException e) {
e.printStackTrace();
myself = null;
}
} else if (element instanceof HTMLDocument.BlockElement) {
HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element;
HtmlAttr htmlattr = printAttributesHtml(be);
av = be.getAttribute(javax.swing.text.html.HTML.Attribute.ALIGN);
String align = av == null ? null : av.toString();
if (htmlattr.bold || htmlattr.italic) {
String family = font.getFamilyname();
float size = font.getSize();
Color color = font.getColor();
if (htmlattr.bold && htmlattr.italic)
vFont = FontFactory.getFont(family, size, Font.BOLDITALIC, color);
else if (htmlattr.italic)
vFont = FontFactory.getFont(family, size, Font.ITALIC, color);
else if (htmlattr.bold)
vFont = FontFactory.getFont(family, size, Font.BOLD, Color.blue);
}
if ("html".equalsIgnoreCase(tag)) {
myself = generateElementChildrenHtml(element, depth + 1, vFont);
} else if ("head".equalsIgnoreCase(tag)) {
myself = null;
} else if ("body".equalsIgnoreCase(tag)) {
myself = generateElementChildrenHtml(element, depth + 1, vFont);
} else if ("p".equalsIgnoreCase(tag) || "p-implied".equalsIgnoreCase(tag)) {
List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
Paragraph paragraph = new Paragraph();
paragraph.setFirstLineIndent(0F);
for (Object child : children) {
if (!(child instanceof Chunk))
paragraph.add(child);
}
if (align != null)
paragraph.setAlignment(align);
myself = paragraph;
} else if ("h1".equalsIgnoreCase(tag) || "h2".equalsIgnoreCase(tag) || "h3".equalsIgnoreCase(tag)) {
List<Object> children = generateElementChildrenHtml(element, depth + 1, subSectionFont);
Paragraph title = new Paragraph();
for (Object child : children) {
title.add(child);
}
myself = new TempSectionPdf(title);
} else if ("ul".equalsIgnoreCase(tag)) {
com.lowagie.text.List list = new com.lowagie.text.List(false, false, 20.0f);
list.setIndentationLeft(25.0f);
List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
for (Object child : children) {
list.add(child);
}
myself = list;
} else if ("ol".equalsIgnoreCase(tag)) {
com.lowagie.text.List list = new com.lowagie.text.List(true, false, 20.0f);
list.setIndentationLeft(25.0f);
List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
for (Object child : children) {
list.add(child);
}
myself = list;
} else if ("li".equalsIgnoreCase(tag)) {
ListItem li = new ListItem();
li.setSpacingAfter(0.0f);
List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
for (Object child : children) {
li.add(child);
}
myself = li;
} else if ("table".equalsIgnoreCase(tag)) {
List<Object> rows = generateElementChildrenHtml(element, depth + 1, normalFont);
try {
int ncols = 0;
for (Object row : rows) {
if (row instanceof List<?>) {
int n = ((List<?>) row).size();
if (n > ncols)
ncols = n;
}
}
Table table = new Table(2);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 128, 128));
table.setPadding(1.0f);
table.setSpacing(0.5f);
Cell c = new Cell("header");
c.setHeader(true);
c.setColspan(ncols);
table.addCell(c);
table.endHeaders();
for (Object row : rows) {
if (row instanceof List<?>) {
for (Object cell : (List<?>) row) {
if (cell instanceof Cell)
table.addCell((Cell) cell);
}
}
}
myself = table;
} catch (BadElementException e) {
e.printStackTrace();
myself = null;
}
} else if ("tr".equalsIgnoreCase(tag)) {
List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
myself = children;
} else if ("td".equalsIgnoreCase(tag)) {
Cell cell = new Cell();
List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
for (Object child : children) {
cell.add(child);
}
myself = cell;
} else if ("div".equalsIgnoreCase(tag)) {
List<Object> children = generateElementChildrenHtml(element, depth + 1, normalFont);
Paragraph paragraph = new Paragraph();
paragraph.setFirstLineIndent(0F);
for (Object child : children) {
paragraph.add(child);
}
if (align != null)
paragraph.setAlignment(align);
myself = paragraph;
} else {
System.err.println("Unknown element " + element.getName());
myself = null;
}
} else {
// could be BidiElement - not sure what it is
myself = null;
}
return myself;
}
use of com.lowagie.text.BadElementException in project mdw-designer by CenturyLinkCloud.
the class ExportHelper method printElementHtml.
private void printElementHtml(Element element, Object parent, int depth, Font font, int parentLevel) {
String tag = element.getName();
Object av;
if (element instanceof HTMLDocument.RunElement) {
HTMLDocument.RunElement re = (HTMLDocument.RunElement) element;
int start = re.getStartOffset();
int end = re.getEndOffset();
try {
String content = re.getDocument().getText(start, end - start);
printAttributesHtml(re);
av = re.getAttribute(CSS.Attribute.FONT_SIZE);
String fontsize = av == null ? null : av.toString();
av = re.getAttribute(CSS.Attribute.FONT_FAMILY);
String fontfamily = av == null ? null : av.toString();
av = re.getAttribute(CSS.Attribute.COLOR);
String fontcolor = av == null ? null : av.toString();
if (fontcolor != null || fontsize != null || fontfamily != null) {
if (fontfamily == null)
fontfamily = font.getFamilyname();
float size = fontsize == null ? font.getSize() : (Float.parseFloat(fontsize) + 9);
int style = font.getStyle();
Color color;
if (fontcolor != null) {
color = Color.decode(fontcolor);
} else
color = font.getColor();
font = FontFactory.getFont(fontfamily, size, style, color);
}
if (parent instanceof Paragraph) {
((Paragraph) parent).add(new Chunk(content, font));
} else {
System.err.println("chunk with parent " + (parent == null ? "null" : parent.getClass().getName()) + ": " + content);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
} else if (element instanceof HTMLDocument.BlockElement) {
HTMLDocument.BlockElement be = (HTMLDocument.BlockElement) element;
printAttributesHtml(be);
av = be.getAttribute(javax.swing.text.html.HTML.Attribute.ALIGN);
String align = av == null ? null : av.toString();
if ("html".equalsIgnoreCase(tag)) {
printElementChildrenHtml(element, parent, depth + 1, font, parentLevel);
} else if ("head".equalsIgnoreCase(tag)) {
// do nothing
} else if ("body".equalsIgnoreCase(tag)) {
printElementChildrenHtml(element, parent, depth + 1, font, parentLevel);
} else if ("p".equalsIgnoreCase(tag)) {
if (parent instanceof Section) {
Paragraph paragraph = new Paragraph();
if (align != null) {
paragraph.setAlignment(align);
}
printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
((Section) parent).add(paragraph);
} else {
System.err.println("p with parent " + (parent == null ? "null" : parent.getClass().getName()));
}
} else if ("h1".equalsIgnoreCase(tag) || "h2".equalsIgnoreCase(tag) || "h3".equalsIgnoreCase(tag)) {
if (parent instanceof Section) {
Paragraph title = new Paragraph();
printElementChildrenHtml(element, title, depth + 1, subSectionFont, parentLevel);
((Section) parent).addSection(title, parentLevel == 0 ? 0 : (parentLevel + 1));
} else {
System.err.println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
}
} else if ("ul".equalsIgnoreCase(tag)) {
if (parent instanceof Section) {
com.lowagie.text.List list = new com.lowagie.text.List(false, false, 20.0f);
printElementChildrenHtml(element, list, depth + 1, normalFont, parentLevel);
((Section) parent).add(list);
} else {
System.err.println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
}
} else if ("ol".equalsIgnoreCase(tag)) {
if (parent instanceof Section) {
com.lowagie.text.List list = new com.lowagie.text.List(true, false, 20.0f);
printElementChildrenHtml(element, list, depth + 1, normalFont, parentLevel);
((Section) parent).add(list);
} else {
System.err.println("list with parent " + (parent == null ? "null" : parent.getClass().getName()));
}
} else if ("li".equalsIgnoreCase(tag)) {
ListItem li = new ListItem();
li.setSpacingAfter(0.0f);
printElementChildrenHtml(element, li, depth + 1, normalFont, parentLevel);
((com.lowagie.text.List) parent).add(li);
} else if ("p-implied".equalsIgnoreCase(tag)) {
if (parent instanceof ListItem) {
Paragraph paragraph = new Paragraph();
printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
((ListItem) parent).add(paragraph);
} else if (parent instanceof Cell) {
Paragraph paragraph = new Paragraph();
printElementChildrenHtml(element, paragraph, depth + 1, normalFont, parentLevel);
((Cell) parent).add(paragraph);
}
} else if ("table".equalsIgnoreCase(tag)) {
try {
Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 128, 128));
table.setPadding(1.0f);
table.setSpacing(0.5f);
Cell c = new Cell("header");
c.setHeader(true);
c.setColspan(3);
table.addCell(c);
table.endHeaders();
printElementChildrenHtml(element, table, depth + 1, normalFont, parentLevel);
((Section) parent).add(table);
} catch (BadElementException e) {
e.printStackTrace();
}
} else if ("tr".equalsIgnoreCase(tag)) {
printElementChildrenHtml(element, parent, depth + 1, normalFont, parentLevel);
} else if ("td".equalsIgnoreCase(tag)) {
Cell cell = new Cell();
printElementChildrenHtml(element, cell, depth + 1, normalFont, parentLevel);
((Table) parent).addCell(cell);
} else {
System.err.println("Unknown element " + element.getName());
printElementChildrenHtml(element, parent, depth + 1, normalFont, parentLevel);
}
} else {
// could be BidiElement - not sure what it is
return;
}
}
use of com.lowagie.text.BadElementException in project charts by vaadin.
the class PdfExportDemo method drawUnscaledSvg.
private Image drawUnscaledSvg(PdfContentByte contentByte) throws IOException {
// First, lets create a graphics node for the SVG image.
GraphicsNode imageGraphics = buildBatikGraphicsNode(svgStr);
// SVG's width and height
float width = (float) imageGraphics.getBounds().getWidth();
float height = (float) imageGraphics.getBounds().getHeight();
// Create a PDF template for the SVG image
PdfTemplate template = contentByte.createTemplate(width, height);
// Create Graphics2D rendered object from the template
Graphics2D graphics = template.createGraphics(width, height);
try {
// SVGs can have their corner at coordinates other than (0,0).
Rectangle2D bounds = imageGraphics.getBounds();
graphics.translate(-bounds.getX(), -bounds.getY());
// Paint SVG GraphicsNode with the 2d-renderer.
imageGraphics.paint(graphics);
// image.
return new ImgTemplate(template);
} catch (BadElementException e) {
throw new RuntimeException("Couldn't generate PDF from SVG", e);
} finally {
// Manual cleaning (optional)
graphics.dispose();
}
}
Aggregations