use of com.lowagie.text.DocumentException in project vcell by virtualcell.
the class ITextWriter method writeBioModel.
public void writeBioModel(BioModel bioModel, FileOutputStream fos, PageFormat pageFormat, PublishPreferences preferences) throws Exception {
if (bioModel == null || fos == null || pageFormat == null || preferences == null) {
throw new IllegalArgumentException("One or more null params while publishing BioModel.");
}
try {
createDocument(pageFormat);
createDocWriter(fos);
// Add metadata before you open the document...
String name = bioModel.getName().trim();
String userName = "Unknown";
if (bioModel.getVersion() != null) {
userName = bioModel.getVersion().getOwner().getName();
}
document.addTitle(name + "[owned by " + userName + "]");
document.addCreator("Virtual Cell");
document.addCreationDate();
// writeWatermark(document, pageFormat);
writeHeaderFooter("BioModel: " + name);
document.open();
//
Section introSection = null;
int chapterNum = 1;
if (preferences.includePhysio()) {
Chapter physioChapter = new Chapter("Physiology For " + name, chapterNum++);
introSection = physioChapter.addSection("General Info", physioChapter.numberDepth() + 1);
String freeTextAnnotation = bioModel.getVCMetaData().getFreeTextAnnotation(bioModel);
writeMetadata(introSection, name, freeTextAnnotation, userName, "BioModel");
writeModel(physioChapter, bioModel.getModel());
document.add(physioChapter);
}
if (preferences.includeApp()) {
SimulationContext[] simContexts = bioModel.getSimulationContexts();
if (simContexts.length > 0) {
Chapter simContextsChapter = new Chapter("Applications For " + name, chapterNum++);
if (introSection == null) {
introSection = simContextsChapter.addSection("General Info", simContextsChapter.numberDepth() + 1);
String freeTextAnnotation = bioModel.getVCMetaData().getFreeTextAnnotation(bioModel);
writeMetadata(introSection, name, freeTextAnnotation, userName, "BioModel");
}
for (int i = 0; i < simContexts.length; i++) {
writeSimulationContext(simContextsChapter, simContexts[i], preferences);
}
document.add(simContextsChapter);
} else {
System.err.println("Bad Request: No applications to publish for Biomodel: " + bioModel.getName());
}
}
document.close();
} catch (DocumentException e) {
System.err.println("Unable to publish BioModel.");
e.printStackTrace();
throw e;
}
}
use of com.lowagie.text.DocumentException in project vcell by virtualcell.
the class ITextWriter method writeReactions.
// each reaction has its own table, ordered by the structures.
protected void writeReactions(Chapter physioChapter, Model model) throws DocumentException {
if (model == null) {
return;
}
Paragraph reactionParagraph = new Paragraph();
reactionParagraph.add(new Chunk("Structures and Reactions Diagram").setLocalDestination(model.getName()));
Section reactionDiagramSection = physioChapter.addSection(reactionParagraph, physioChapter.numberDepth() + 1);
try {
addImage(reactionDiagramSection, encodeJPEG(generateDocReactionsImage(model, null)));
} catch (Exception e) {
e.printStackTrace();
throw new DocumentException(e.getClass().getName() + ": " + e.getMessage());
}
for (int i = 0; i < model.getNumStructures(); i++) {
ReactionStep[] reactionSteps = model.getReactionSteps();
ReactionStep rs = null;
Table modifierTable = null;
Table reactionTable = null;
boolean firstTime = true;
Section reactStructSection = null;
for (int j = 0; j < reactionSteps.length; j++) {
if (reactionSteps[j].getStructure() == model.getStructure(i)) {
// can also use structureName1.equals(structureName2)
if (firstTime) {
Paragraph linkParagraph = new Paragraph();
linkParagraph.add(new Chunk("Reaction(s) in " + model.getStructure(i).getName()).setLocalDestination(model.getStructure(i).getName()));
reactStructSection = physioChapter.addSection(linkParagraph, physioChapter.numberDepth() + 1);
firstTime = false;
}
rs = reactionSteps[j];
String type;
if (rs instanceof SimpleReaction) {
type = "Reaction";
} else {
type = "Flux";
}
// write Reaction equation as a table
// Get the image arrow cell depending on type of reactionStep : MassAction => double arrow, otherwise, forward arrow
boolean bReversible = false;
if (rs.getKinetics() instanceof MassActionKinetics) {
bReversible = true;
}
Cell arrowImageCell = getReactionArrowImageCell(bReversible);
// Get reactants and products strings
ReactionCanvas rc = new ReactionCanvas();
rc.setReactionStep(rs);
ReactionCanvasDisplaySpec rcdSpec = rc.getReactionCanvasDisplaySpec();
String reactants = rcdSpec.getLeftText();
String products = rcdSpec.getRightText();
// Create table and add cells for reactants, arrow(s) images, products
int[] widths = { 8, 1, 8 };
reactionTable = getTable(3, 100, 0, 2, 2);
// Add reactants as cell
Cell tableCell = createCell(reactants, getBold());
tableCell.setHorizontalAlignment(Cell.ALIGN_RIGHT);
tableCell.setBorderColor(Color.white);
reactionTable.addCell(tableCell);
// add arrow(s) image as cell
if (arrowImageCell != null) {
arrowImageCell.setHorizontalAlignment(Cell.ALIGN_CENTER);
arrowImageCell.setBorderColor(Color.white);
reactionTable.addCell(arrowImageCell);
}
// add products as cell
tableCell = createCell(products, getBold());
tableCell.setBorderColor(Color.white);
reactionTable.addCell(tableCell);
// reactionTable.setBorderColor(Color.white);
reactionTable.setWidths(widths);
// Identify modifiers,
ReactionParticipant[] rpArr = rs.getReactionParticipants();
Vector<ReactionParticipant> modifiersVector = new Vector<ReactionParticipant>();
for (int k = 0; k < rpArr.length; k += 1) {
if (rpArr[k] instanceof Catalyst) {
modifiersVector.add(rpArr[k]);
}
}
// Write the modifiers in a separate table, if present
if (modifiersVector.size() > 0) {
modifierTable = getTable(1, 50, 0, 1, 1);
modifierTable.addCell(createCell("Modifiers List", getBold(DEF_HEADER_FONT_SIZE), 1, 1, Element.ALIGN_CENTER, true));
StringBuffer modifierNames = new StringBuffer();
for (int k = 0; k < modifiersVector.size(); k++) {
modifierNames.append(((Catalyst) modifiersVector.elementAt(k)).getName() + "\n");
}
modifierTable.addCell(createCell(modifierNames.toString().trim(), getFont()));
modifiersVector.removeAllElements();
}
Section reactionSection = reactStructSection.addSection(type + " " + rs.getName(), reactStructSection.numberDepth() + 1);
// Annotation
VCMetaData vcMetaData = rs.getModel().getVcMetaData();
if (vcMetaData.getFreeTextAnnotation(rs) != null) {
Table annotTable = getTable(1, 100, 1, 3, 3);
annotTable.addCell(createCell("Reaction Annotation", getBold(DEF_HEADER_FONT_SIZE), 1, 1, Element.ALIGN_CENTER, true));
annotTable.addCell(createCell(vcMetaData.getFreeTextAnnotation(rs), getFont()));
reactionSection.add(annotTable);
// reactionSection.add(new Paragraph("\""+rs.getAnnotation()+"\""));
}
// reaction table
if (reactionTable != null) {
reactionSection.add(reactionTable);
// re-set reactionTable
reactionTable = null;
}
if (modifierTable != null) {
reactionSection.add(modifierTable);
modifierTable = null;
}
// Write kinetics parameters, etc. in a table
writeKineticsParams(reactionSection, rs);
}
}
}
}
use of com.lowagie.text.DocumentException in project janrufmonitor by tbrandt77.
the class PDFCallerListFilter method doExport.
public boolean doExport() {
Document document = new Document(PageSize.A4.rotate());
document.addCreationDate();
document.addCreator("jAnrufmonitor");
try {
PdfWriter.getInstance(document, new FileOutputStream(this.m_filename));
document.open();
// get renderers
List renderer = new ArrayList();
String renderer_config = this.getRuntime().getConfigManagerFactory().getConfigManager().getProperty("ui.jface.application.editor.Editor", "renderer");
if (renderer_config != null && renderer_config.length() > 0) {
StringTokenizer s = new StringTokenizer(renderer_config, ",");
while (s.hasMoreTokens()) {
renderer.add(RendererRegistry.getInstance().getRenderer(s.nextToken()));
}
}
// get column width
float totalWidth = 0;
String[] cWidth = new String[renderer.size()];
for (int i = 0, j = renderer.size(); i < j; i++) {
cWidth[i] = getRuntime().getConfigManagerFactory().getConfigManager().getProperty("ui.jface.application.editor.Editor", "col_size_" + ((ITableCellRenderer) renderer.get(i)).getID());
if (cWidth[i] != null && cWidth[i].length() > 0) {
totalWidth += Float.parseFloat(cWidth[i]);
}
if (cWidth[i] != null && cWidth[i].length() == 0) {
cWidth[i] = "0";
}
}
float[] widths = new float[renderer.size()];
for (int i = 0, j = renderer.size(); i < j; i++) {
widths[i] = Float.parseFloat(cWidth[i]) / totalWidth;
}
PdfPTable table = new PdfPTable(widths);
table.setHeaderRows(1);
table.setWidthPercentage(100f);
ITableCellRenderer t = null;
PdfPCell cell = null;
for (int i = 0, j = renderer.size(); i < j; i++) {
t = (ITableCellRenderer) renderer.get(i);
if (t == null) {
this.m_logger.severe("No renderer found for ID: " + (String) renderer.get(i));
this.m_logger.severe("Export to PDF format canceled...");
return false;
}
cell = new PdfPCell(new Paragraph(t.getHeader()));
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
table.addCell(cell);
}
ICaller c = null;
String cellContent = null;
Color iterateColor1 = new Color(0xDD, 0xDD, 0xDD);
Color iterateColor2 = new Color(0xFF, 0xFF, 0xFF);
cell = null;
int type_col = -1, num_col = -1;
for (int i = 0, j = this.m_callerList.size(); i < j; i++) {
c = this.m_callerList.get(i);
for (int k = 0, m = renderer.size(); k < m; k++) {
t = (ITableCellRenderer) renderer.get(k);
t.updateData(c);
// find number and typ column for multiline callers
if (t.getID().equalsIgnoreCase("NumberType"))
type_col = k;
if (t.getID().equalsIgnoreCase("Number"))
num_col = k;
cellContent = t.renderAsText();
if (cellContent != null && cellContent.length() > 0) {
cell = new PdfPCell(new Phrase(cellContent));
cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
table.addCell(cell);
} else {
cellContent = t.renderAsImageID();
if (cellContent != null && cellContent.length() > 0) {
if (cellContent.startsWith("db://")) {
InputStream in = ImageHandler.getInstance().getImageStream(c);
if (in != null) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Stream.copy(in, out, true);
in.close();
out.close();
Image pdfImage = Image.getInstance(out.toByteArray());
// pdfImage.scaleAbsolute(90.0f, 45.0f);
table.addCell(pdfImage);
} else {
table.addCell(" ");
}
} else if (new File(cellContent).exists()) {
Image pdfImage = Image.getInstance(cellContent);
table.addCell(pdfImage);
} else {
Image pdfImage = Image.getInstance(SWTImageManager.getInstance(PIMRuntime.getInstance()).getImagePath(cellContent));
table.addCell(pdfImage);
}
// ((Image pdfImage = Image.getInstance(cellContent);
// cell = new PdfPCell(pdfImage);
// cell.setBackgroundColor((i%2==0 ? iterateColor1 : iterateColor2));
// table.addCell(pdfImage);
} else {
cell = new PdfPCell(new Phrase(" "));
cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
table.addCell(cell);
}
}
// last column reached check for multiline caller
if (k == m - 1 && (type_col > -1 || num_col > -1) && (c instanceof IMultiPhoneCaller) && (((IMultiPhoneCaller) c).getPhonenumbers().size() > 1)) {
List phones = ((IMultiPhoneCaller) c).getPhonenumbers();
IPhonenumber pn = null;
for (int y = 1, z = phones.size(); y < z; y++) {
pn = (IPhonenumber) phones.get(y);
for (int w = 0, x = renderer.size(); w < x; w++) {
t = (ITableCellRenderer) renderer.get(w);
if (t.getID().equalsIgnoreCase("NumberType") || t.getID().equalsIgnoreCase("Number")) {
if (t.getID().equalsIgnoreCase("NumberType")) {
final IPhonenumber lpn = pn;
final IAttributeMap lam = c.getAttributes();
t.updateData(new ITreeItemCallerData() {
public IAttributeMap getAttributes() {
return lam;
}
public IPhonenumber getPhone() {
return lpn;
}
});
} else
t.updateData(pn);
cellContent = t.renderAsText();
if (cellContent != null && cellContent.length() > 0) {
cell = new PdfPCell(new Phrase(cellContent));
cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
table.addCell(cell);
} else {
cell = new PdfPCell(new Phrase(" "));
cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
table.addCell(cell);
}
} else {
cell = new PdfPCell(new Phrase(" "));
cell.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
table.addCell(cell);
}
}
}
}
}
}
document.add(table);
} catch (DocumentException de) {
this.m_logger.severe(de.getMessage());
return false;
} catch (IOException ioe) {
this.m_logger.severe(ioe.getMessage());
return false;
} finally {
document.close();
}
return true;
}
use of com.lowagie.text.DocumentException in project janrufmonitor by tbrandt77.
the class PDFCreator method createPdf.
public void createPdf() {
Document document = new Document(PageSize.A4);
document.addCreationDate();
document.addCreator("jAnrufmonitor");
try {
PdfWriter.getInstance(document, new FileOutputStream(this.m_file));
document.open();
document.add(new Paragraph(this.getI18nManager().getString(getNamespce(), "pdftitle", "label", getLanguage()), FontFactory.getFont(FontFactory.HELVETICA, 16f, Font.BOLD | Font.UNDERLINE)));
document.add(new Paragraph(" "));
String msg = "";
ITableCellRenderer tcr = RendererRegistry.getInstance().getRenderer("name");
if (tcr != null) {
tcr.updateData(m_cc.getCaller());
msg += tcr.renderAsText();
}
tcr = RendererRegistry.getInstance().getRenderer("number");
if (tcr != null) {
tcr.updateData(m_cc.getCaller());
msg += "\n" + tcr.renderAsText() + "\n";
}
document.add(new Paragraph(msg));
document.add(new Paragraph(" "));
List comments = m_cc.getComments();
Collections.sort(comments, new CommentComparator());
IComment c = null;
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100f);
PdfPCell cp = null;
Paragraph pp = null;
Color iterateColor1 = new Color(0xDD, 0xDD, 0xDD);
Color iterateColor2 = new Color(0xFF, 0xFF, 0xFF);
for (int i = 0; i < comments.size(); i++) {
cp = new PdfPCell();
cp.setBackgroundColor((i % 2 == 0 ? iterateColor1 : iterateColor2));
pp = new Paragraph();
Paragraph p = new Paragraph();
c = (IComment) comments.get(i);
IAttribute att = c.getAttributes().get(IComment.COMMENT_ATTRIBUTE_SUBJECT);
if (att != null && att.getValue().length() > 0) {
p.add(new Chunk(this.getI18nManager().getString(getNamespce(), "pdfsubject", "label", getLanguage()), FontFactory.getFont(FontFactory.HELVETICA, 14f, Font.BOLD)));
p.add(new Chunk(att.getValue(), FontFactory.getFont(FontFactory.HELVETICA, 14f, Font.BOLD)));
pp.add(p);
p = new Paragraph();
}
p.add(new Chunk(this.getI18nManager().getString(getNamespce(), "pdfdate", "label", getLanguage()), FontFactory.getFont(FontFactory.HELVETICA, 12f, Font.BOLD)));
p.add(new Chunk(getFormatter().parse(IJAMConst.GLOBAL_VARIABLE_CALLTIME, c.getDate())));
pp.add(p);
p = new Paragraph();
p.add(new Chunk(this.getI18nManager().getString(getNamespce(), "pdfstatus", "label", getLanguage()), FontFactory.getFont(FontFactory.HELVETICA, 12f, Font.BOLD)));
p.add(new Chunk(c.getAttributes().get(IComment.COMMENT_ATTRIBUTE_STATUS).getValue()));
pp.add(p);
att = c.getAttributes().get(IComment.COMMENT_ATTRIBUTE_FOLLOWUP);
if (att != null && att.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_YES)) {
p = new Paragraph();
Chunk cu = new Chunk(this.getI18nManager().getString(getNamespce(), "pdffollowup", "label", getLanguage()), FontFactory.getFont(FontFactory.HELVETICA, 12f, Font.BOLD));
cu.setBackground(new Color(0xFF, 0xFF, 0x00));
p.add(cu);
pp.add(p);
}
pp.add(new Paragraph(" "));
p = new Paragraph(c.getText());
pp.add(p);
cp.addElement(pp);
table.addCell(cp);
}
document.add(table);
} catch (DocumentException de) {
this.m_logger.severe(de.getMessage());
} catch (IOException ioe) {
this.m_logger.severe(ioe.getMessage());
} finally {
document.close();
}
}
use of com.lowagie.text.DocumentException in project ofbiz-framework by apache.
the class PdfSurveyServices method setAcroFields.
/**
*/
public static Map<String, Object> setAcroFields(DispatchContext dctx, Map<String, ? extends Object> context) {
Map<String, Object> results = ServiceUtil.returnSuccess();
Delegator delegator = dctx.getDelegator();
try {
Map<String, Object> acroFieldMap = UtilGenerics.checkMap(context.get("acroFieldMap"));
ByteBuffer byteBuffer = getInputByteBuffer(context, delegator);
PdfReader r = new PdfReader(byteBuffer.array());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper s = new PdfStamper(r, baos);
AcroFields fs = s.getAcroFields();
Map<String, Object> map = UtilGenerics.checkMap(fs.getFields());
s.setFormFlattening(true);
for (String fieldName : map.keySet()) {
String fieldValue = fs.getField(fieldName);
Object obj = acroFieldMap.get(fieldName);
if (obj instanceof Date) {
Date d = (Date) obj;
fieldValue = UtilDateTime.toDateString(d);
} else if (obj instanceof Long) {
Long lg = (Long) obj;
fieldValue = lg.toString();
} else if (obj instanceof Integer) {
Integer ii = (Integer) obj;
fieldValue = ii.toString();
} else {
fieldValue = (String) obj;
}
if (UtilValidate.isNotEmpty(fieldValue)) {
fs.setField(fieldName, fieldValue);
}
}
s.close();
baos.close();
ByteBuffer outByteBuffer = ByteBuffer.wrap(baos.toByteArray());
results.put("outByteBuffer", outByteBuffer);
} catch (DocumentException | IOException | GeneralException e) {
Debug.logError(e, module);
results = ServiceUtil.returnError(e.getMessage());
}
return results;
}
Aggregations