List of usage examples for com.lowagie.text.pdf PdfPTable setWidthPercentage
public void setWidthPercentage(float widthPercentage)
From source file:airportpainter.util.AirportPainter.java
License:Open Source License
private void createSheets2Freqs(Airport airport, Document document) { // FREQs Nearest Navaid notifyAction("createSheets2Freqs"); PdfPTable table = new PdfPTable(2); {// ww w. ja v a 2s.com PdfPCell cell = new PdfPCell(); { Paragraph p = new Paragraph("Within Airspace:", new com.lowagie.text.Font( com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.NORMAL)); cell.addElement(p); } cell.setBorderWidth(borderWidth); cell.setBorderWidthBottom(borderWidthThin); //cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_TOP); table.addCell(cell); } { PdfPCell cell = new PdfPCell(); com.lowagie.text.Font fontBold = new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, com.lowagie.text.Font.DEFAULTSIZE, com.lowagie.text.Font.BOLD); com.lowagie.text.Font font = new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, com.lowagie.text.Font.DEFAULTSIZE, com.lowagie.text.Font.NORMAL); { Paragraph p = new Paragraph(airport.getId(), fontBold); cell.addElement(p); } { Paragraph p = new Paragraph(); p.add(new Chunk("Elev ", font)); int feets = (int) (airport.getElevation()); p.add(new Chunk(new Integer(feets).toString() + "'", fontBold)); int meters = (int) (feets / FEET_PER_METER); p.add(new Chunk("/" + new Integer(meters).toString() + "m", font)); cell.addElement(p); } { Paragraph p = new Paragraph(); p.add(new Chunk(airport.getLatitudeString1(airport.getLat(), "N", "S", 2, false) + "\r\n" + airport.getLatitudeString1(airport.getLong(), "W", "E", 3, true), font)); cell.addElement(p); } cell.setBorder(com.lowagie.text.Rectangle.NO_BORDER); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); table.addCell(cell); } table.setWidthPercentage(100); try { document.add(table); } catch (DocumentException ex) { Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:airportpainter.util.AirportPainter.java
License:Open Source License
private void createSheets2ILS(Airport airport, Document document) { // FREQs Nearest Navaid notifyAction("createSheets2ILS"); PdfPTable table = new PdfPTable(2); {/* w w w. j a va2s .c om*/ PdfPCell cell = new PdfPCell(); { Paragraph p = new Paragraph("(TWR)", new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.NORMAL)); p.setLeading((p.leading() / 2) + 2); cell.addElement(p); } List<ATCFreq> tempFreqsTwr = _airport.getATCFreqs("54"); List<ATCFreq> tempFreqsGnd = _airport.getATCFreqs("53"); if (tempFreqsTwr.size() > 0 || tempFreqsGnd.size() > 0) { if (tempFreqsTwr.size() > 0) { Paragraph p = new Paragraph(); p.setLeading((p.leading() / 2) + 6); Chunk c = new Chunk(airport.getName().toUpperCase() + " APPROACH ", new com.lowagie.text.Font( com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.NORMAL)); p.add(c); for (final ATCFreq tower : tempFreqsTwr) { Chunk c2 = new Chunk((tower.getFreq() + " ").substring(0, 9) + " ", new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.BOLD)); p.add(c2); } cell.addElement(p); } if (tempFreqsGnd.size() > 0) { Paragraph p = new Paragraph(); p.setLeading((p.leading() / 2) + 6); Chunk c = new Chunk("GROUND ", new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.NORMAL)); p.add(c); for (final ATCFreq tower : tempFreqsGnd) { Chunk c2 = new Chunk((tower.getFreq() + " ").substring(0, 9) + " ", new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.BOLD)); p.add(c2); } cell.addElement(p); } } cell.setBorderWidth(borderWidth); cell.setBorderWidthTop(borderWidthThin); cell.setBorderWidthRight(borderWidthThin); //cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setVerticalAlignment(Element.ALIGN_TOP); table.addCell(cell); } { com.lowagie.text.Font font = new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, com.lowagie.text.Font.DEFAULTSIZE, com.lowagie.text.Font.NORMAL); PdfPCell cell = new PdfPCell(); Paragraph p = new Paragraph(); p.setLeading((p.leading()) + 6); Chunk c = new Chunk("ATIS ", new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.NORMAL)); p.add(c); for (final Atis atis : _airport.getAtises()) { Chunk c2 = new Chunk((atis.getFreq() + " ").substring(0, 9) + " ", new com.lowagie.text.Font( com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.BOLD)); p.add(c2); } cell.addElement(p); /// ILS table { height1 = 0; PdfPTable tableILS = new PdfPTable(4); tableILS.setWidthPercentage(100); // titles com.lowagie.text.Font fontTitle = new com.lowagie.text.Font(com.lowagie.text.Font.TIMES_ROMAN, 8, com.lowagie.text.Font.NORMAL); { PdfPCell cellTitle = new PdfPCell(); cellTitle.setVerticalAlignment(Element.ALIGN_CENTER); cellTitle.setBorderWidth(borderWidthThin); Paragraph pt = new Paragraph("RWY", fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellTitle.addElement(pt); tableILS.addCell(cellTitle); } { PdfPCell cellTitle = new PdfPCell(); cellTitle.setVerticalAlignment(Element.ALIGN_CENTER); cellTitle.setBorderWidth(borderWidthThin); Paragraph pt = new Paragraph("ILS", fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellTitle.addElement(pt); tableILS.addCell(cellTitle); } { PdfPCell cellTitle = new PdfPCell(); cellTitle.setVerticalAlignment(Element.ALIGN_CENTER); cellTitle.setBorderWidth(borderWidthThin); Paragraph pt = new Paragraph("RWY", fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellTitle.addElement(pt); tableILS.addCell(cellTitle); } { PdfPCell cellTitle = new PdfPCell(); cellTitle.setVerticalAlignment(Element.ALIGN_CENTER); cellTitle.setBorderWidth(borderWidthThin); Paragraph pt = new Paragraph("ILS", fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellTitle.addElement(pt); tableILS.addCell(cellTitle); } int pos = 0; int rs = airport.getRunways().size(); for (final Runway runway : airport.getRunways()) { if (runway.getIlsFreq() != 0) { height1 = height1 + 10; { PdfPCell cellFreq = new PdfPCell(); cellFreq.setVerticalAlignment(Element.ALIGN_CENTER); cellFreq.setBorderWidth(borderWidthThin); if (pos > 0) { cellFreq.setBorderWidthTop(0); } if (pos < rs - 1) { cellFreq.setBorderWidthBottom(0); } Paragraph pt = new Paragraph(runway.getNumber(), fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellFreq.addElement(pt); tableILS.addCell(cellFreq); } { PdfPCell cellFreq = new PdfPCell(); cellFreq.setVerticalAlignment(Element.ALIGN_CENTER); cellFreq.setBorderWidth(borderWidthThin); if (pos > 0) { cellFreq.setBorderWidthTop(0); } if (pos < rs - 1) { cellFreq.setBorderWidthBottom(0); } Paragraph pt = new Paragraph( runway.getIlsFreq() + " " + "... " + new Integer((int) runway.getHeading()).toString() + degrees, fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellFreq.addElement(pt); tableILS.addCell(cellFreq); } } /*else { { PdfPCell cellFreq = new PdfPCell(); cellFreq.setVerticalAlignment(Element.ALIGN_CENTER); cellFreq.setBorderWidth(borderWidthThin); if (pos>0) { cellFreq.setBorderWidthTop(0); } if (pos<rs-1) { cellFreq.setBorderWidthBottom(0); } Paragraph pt=new Paragraph( "",fontTitle); pt.setLeading((pt.leading()/2)+2); pt.setAlignment(Element.ALIGN_CENTER); cellFreq.addElement(pt); tableILS.addCell(cellFreq); } { PdfPCell cellFreq = new PdfPCell(); cellFreq.setVerticalAlignment(Element.ALIGN_CENTER); cellFreq.setBorderWidth(borderWidthThin); if (pos>0) { cellFreq.setBorderWidthTop(0); } if (pos<rs-1) { cellFreq.setBorderWidthBottom(0); } Paragraph pt=new Paragraph( "",fontTitle); pt.setLeading((pt.leading()/2)+2); pt.setAlignment(Element.ALIGN_CENTER); cellFreq.addElement(pt); tableILS.addCell(cellFreq); } }*/ if (runway.getIlsOppositeFreq() != 0) { { PdfPCell cellFreq = new PdfPCell(); cellFreq.setVerticalAlignment(Element.ALIGN_CENTER); cellFreq.setBorderWidth(borderWidthThin); if (pos > 0) { cellFreq.setBorderWidthTop(0); } if (pos < rs - 1) { cellFreq.setBorderWidthBottom(0); } Paragraph pt = new Paragraph(runway.getOppositeNumber(), fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellFreq.addElement(pt); tableILS.addCell(cellFreq); } { PdfPCell cellFreq = new PdfPCell(); cellFreq.setVerticalAlignment(Element.ALIGN_CENTER); cellFreq.setBorderWidth(borderWidthThin); if (pos > 0) { cellFreq.setBorderWidthTop(0); } if (pos < rs - 1) { cellFreq.setBorderWidthBottom(0); } Paragraph pt = new Paragraph(runway.getIlsOppositeFreq() + " " + "... " + new Integer((int) (runway.getHeading() + 180) % 180).toString() + degrees, fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellFreq.addElement(pt); tableILS.addCell(cellFreq); } } else { { PdfPCell cellFreq = new PdfPCell(); cellFreq.setVerticalAlignment(Element.ALIGN_CENTER); cellFreq.setBorderWidth(borderWidthThin); if (pos > 0) { cellFreq.setBorderWidthTop(0); } if (pos < rs - 1) { cellFreq.setBorderWidthBottom(0); } Paragraph pt = new Paragraph("", fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellFreq.addElement(pt); tableILS.addCell(cellFreq); } { PdfPCell cellFreq = new PdfPCell(); cellFreq.setVerticalAlignment(Element.ALIGN_CENTER); cellFreq.setBorderWidth(borderWidthThin); if (pos > 0) { cellFreq.setBorderWidthTop(0); } if (pos < rs - 1) { cellFreq.setBorderWidthBottom(0); } Paragraph pt = new Paragraph("", fontTitle); pt.setLeading((pt.leading() / 2) + 2); pt.setAlignment(Element.ALIGN_CENTER); cellFreq.addElement(pt); tableILS.addCell(cellFreq); } } pos++; } float[] widths2 = { 1f, 3f, 1f, 3f }; try { tableILS.setWidths(widths2); } catch (DocumentException ex) { Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex); } cell.addElement(tableILS); } cell.setBorderWidth(borderWidth); cell.setVerticalAlignment(Element.ALIGN_TOP); table.addCell(cell); } table.setWidthPercentage(100); try { document.add(table); } catch (DocumentException ex) { Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ambit.data.qmrf.Qmrf_Xml_Pdf.java
License:Open Source License
public void xml2pdf(InputSource xml, OutputStream pdf) { try {/* w w w . j av a2s . co m*/ Document document = new Document(PageSize.A4, 80, 50, 30, 65); PdfWriter writer = PdfWriter.getInstance(document, pdf); //writer.setViewerPreferences(PdfWriter.HideMenubar| PdfWriter.HideToolbar); writer.setViewerPreferences(PdfWriter.PageModeUseThumbs | PdfWriter.PageModeUseOutlines); //PdfOutline root = writer.getDirectContent().getRootOutline(); //new PdfOutline(root, new PDFAction("http://nina.acad.bg/qmrf"), "a bookmark"); //writer.addFileAttachment(arg0, arg1, arg2, arg3) if (docBuilder == null) docBuilder = docBuilderFactory.newDocumentBuilder(); docBuilder.setErrorHandler(new SimpleErrorHandler(getClass().getName())); QMRFSchemaResolver resolver = new QMRFSchemaResolver("http://ambit.acad.bg/qmrf/qmrf.dtd", null); resolver.setIgnoreSystemID(true); docBuilder.setEntityResolver(resolver); org.w3c.dom.Document doc = null; try { doc = docBuilder.parse(xml); } catch (Exception x) { document.addCreationDate(); document.addCreator(getClass().getName()); document.open(); document.add(new Paragraph(new Chunk(x.getMessage()))); document.close(); return; } document.addCreationDate(); document.addCreator(getClass().getName()); document.addKeywords(replaceTags(findNodeValue("keywords", doc))); document.addTitle(replaceTags(findNodeValue("QSAR_title", doc))); try { NodeList info = doc.getElementsByTagName("QMRF"); for (int i = 0; i < info.getLength(); i++) document.addSubject(findAttributeValue("name", info.item(i)) + '.' + findAttributeValue("version", info.item(i))); } catch (Exception x) { document.addSubject("QMRF"); } try { document.addAuthor(listNodeAttributes(doc, "qmrf_authors", "author_ref", "author", att_author, new Boolean(true))); } catch (Exception x) { document.addAuthor(getClass().getName()); } document.open(); PdfContentByte cb = writer.getDirectContent(); try { headerTable(document, doc); } catch (Exception x) { document.add(new Paragraph(new Chunk(x.getMessage()))); document.close(); return; } PdfOutline root = writer.getDirectContent().getRootOutline(); for (int i = 0; i < subchapters.length; i++) try { int align = Paragraph.ALIGN_LEFT; if (Mode.chapter == (Mode) subchapters[i][2]) { document.add(new Paragraph(new Chunk('\n'))); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); StringBuffer b = new StringBuffer(); b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc)); b.append('.'); b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc)); String bookmark = b.toString(); Chunk title = new Chunk(bookmark); title.setLocalDestination(bookmark); title.setFont(bfont); PdfDestination destination = new PdfDestination(PdfDestination.FITH); PdfOutline outline = new PdfOutline(root, destination, bookmark); Paragraph p = new Paragraph(title); PdfPCell cell = new PdfPCell(p); cell.setBackgroundColor(chapterColor); table.addCell(cell); document.add(table); float pos = writer.getVerticalPosition(false); if (pos < 90) document.newPage(); } else { Phrase phrase = new Phrase(); switch ((Mode) subchapters[i][2]) { case title: { StringBuffer b = new StringBuffer(); String cn = findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc); if (cn == null) break; b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc)); b.append('.'); b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc)); String subchapterBookmark = b.toString(); b.append(':'); Chunk title = new Chunk(b.toString()); title.setLocalDestination(subchapterBookmark); title.setFont(bfont); phrase.add(title); PdfDestination destination = new PdfDestination(PdfDestination.FITBH); PdfOutline outline = new PdfOutline(root, destination, subchapterBookmark); break; } case text: { createNodePhrase(subchapters[i][0].toString(), doc, phrase, font); align = Paragraph.ALIGN_JUSTIFIED; break; } case answer: { String a = findAnswer(subchapters[i][0].toString(), doc); if (a != null) { Chunk answer = new Chunk(a); answer.setFont(font); phrase.add(answer); } break; } case dataset: { StringBuffer b = new StringBuffer(); b.append(findDataAvailable(subchapters[i][0].toString(), doc)); Chunk dataset = new Chunk(b.toString()); dataset.setFont(font); phrase.add(dataset); break; } case attachments: { PdfPTable table = getAttachmentsAsTable(doc, "attachment_training_data"); if (table != null) { phrase.add(new Paragraph("Training set(s)")); phrase.add(table); } table = getAttachmentsAsTable(doc, "attachment_validation_data"); if (table != null) { phrase.add(new Paragraph("Test set(s)")); phrase.add(table); } table = getAttachmentsAsTable(doc, "attachment_documents"); if (table != null) { phrase.add(new Paragraph("Supporting information")); phrase.add(table); } break; /* StringBuffer b = new StringBuffer(); b.append("Training set(s)\n"); b.append(listAttachments(doc,"attachment_training_data")); b.append("Test set(s)\n"); b.append(listAttachments(doc,"attachment_validation_data")); b.append("Supporting information\n"); b.append(listAttachments(doc,"attachment_documents")); Chunk attachments = new Chunk(b.toString()); attachments.setFont(font); phrase.add(attachments); break; */ } case reference: { try { String value = listNodeAttributes(doc, subchapters[i][0].toString(), subchapters[i][3].toString(), subchapters[i][4].toString(), (String[]) subchapters[i][5], (Boolean) subchapters[i][6]); Chunk reference = new Chunk(value); reference.setFont(font); align = Paragraph.ALIGN_JUSTIFIED; phrase.add(reference); } catch (Exception x) { } break; } } Paragraph p = new Paragraph(phrase); p.setAlignment(align); document.add(p); float pos = writer.getVerticalPosition(false); /* cb.moveTo(0, pos); cb.lineTo(PageSize.A4.width(), pos); cb.stroke(); */ if (pos < 90) document.newPage(); } } catch (Exception x) { x.printStackTrace(); } document.close(); } catch (Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } }
From source file:ambit.data.qmrf.Qmrf_Xml_Pdf.java
License:Open Source License
protected PdfPTable getAttachmentsAsTable(org.w3c.dom.Document doc, String attachments) { NodeList nodes = doc.getElementsByTagName(attachments); if (nodes.getLength() > 0) { float[] widths = { 3f, 3f }; PdfPTable table = new PdfPTable(widths); table.setWidthPercentage(100); for (int i = 0; i < nodes.getLength(); i++) if (nodes.item(i) instanceof org.w3c.dom.Element) { NodeList attachment = nodes.item(i).getChildNodes(); if (attachment.getLength() == 0) ;//b.append("N/A\n"); else for (int j = 0; j < attachment.getLength(); j++) if (attachment.item(j) instanceof org.w3c.dom.Element) { org.w3c.dom.Element e = ((org.w3c.dom.Element) attachment.item(j)); Paragraph p = new Paragraph(org.apache.commons.lang.StringEscapeUtils .unescapeXml(e.getAttribute("description"))); PdfPCell cell = new PdfPCell(p); cell.setBackgroundColor(Color.white); table.addCell(cell); String c = org.apache.commons.lang.StringEscapeUtils .unescapeXml(e.getAttribute("url")); PdfAction action = new PdfAction(c); p = new Paragraph(); p.add(new Chunk(c).setAction(action)); cell = new PdfPCell(p); cell.setBackgroundColor(Color.white); table.addCell(cell); }//w w w. j a v a 2 s.co m } return table; } else return null; }
From source file:ambit.data.qmrf.Qmrf_Xml_Pdf.java
License:Open Source License
public void headerTable(Document pdfdoc, org.w3c.dom.Document xmldoc) { try {/* w w w . jav a2 s . co m*/ int header_font_size = 10; Image png_left = Image .getInstance(Qmrf_Xml_Pdf.class.getClassLoader().getResource("ambit/data/qmrf/logo.png")); Image png_right = Image .getInstance(Qmrf_Xml_Pdf.class.getClassLoader().getResource("ambit/data/qmrf/logo.png")); png_left.setAlignment(Image.LEFT); png_right.setAlignment(Image.RIGHT); png_left.scalePercent(60); png_right.scalePercent(60); //png.scaleAbsolute(76, 67); //png.setAlignment(Image.MIDDLE); PdfPCell cell; float[] widths = { 1f, 5f, 1f }; float[] widths1 = { 1f }; PdfPTable table = new PdfPTable(widths); PdfPTable table1 = new PdfPTable(widths1); table.setWidthPercentage(100); cell = new PdfPCell(); cell.setMinimumHeight(70); cell.addElement(new Chunk(png_left, +14, -40)); table.addCell(cell); String Text = ""; try { Text = findNodeValue(xml_QMRF_number, xmldoc); Chunk ident_title = new Chunk("QMRF identifier (JRC Inventory):"); Chunk ident_text = new Chunk(Text); Font bi_font = new Font(baseFont, header_font_size, Font.BOLDITALIC); Font i_font = new Font(baseFont, header_font_size, Font.ITALIC); ident_title.setFont(bi_font); ident_text.setFont(i_font); Paragraph p = new Paragraph(); p.add(ident_title); p.add(ident_text); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table1.addCell(cell); ident_title = new Chunk("QMRF Title:"); Phrase textPhrase = new Phrase(); createNodePhrase("QSAR_title", xmldoc, textPhrase, i_font); ident_title.setFont(bi_font); p = new Paragraph(); p.add(ident_title); p.add(textPhrase); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table1.addCell(cell); java.util.Date now = new java.util.Date(); java.text.DateFormat df = java.text.DateFormat.getDateInstance(); String date = df.format(now); ident_title = new Chunk("Printing Date:"); ident_text = new Chunk(date); ident_title.setFont(bi_font); ident_text.setFont(i_font); p = new Paragraph(); p.add(ident_title); p.add(ident_text); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table1.addCell(cell); p = new Paragraph(""); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table1.addCell(cell); cell = new PdfPCell(table1); cell.setPadding(0f); table.addCell(cell); } catch (Throwable x) { x.printStackTrace(); } //table.addCell(new PdfPCell(png, true)); cell.addElement(new Chunk(png_right, +14, -40)); table.addCell(cell); pdfdoc.add(table); pdfdoc.add(new Paragraph(16)); } catch (Exception x) { x.printStackTrace(); } }
From source file:ambit2.data.qmrf.Qmrf_Xml_Pdf.java
License:Open Source License
public void xml2pdf(InputSource xml, OutputStream pdf) { try {//w w w. j ava 2 s .c om Document document = new Document(PageSize.A4, 80, 50, 30, 65); PdfWriter writer = PdfWriter.getInstance(document, pdf); //writer.setViewerPreferences(PdfWriter.HideMenubar| PdfWriter.HideToolbar); writer.setViewerPreferences(PdfWriter.PageModeUseThumbs | PdfWriter.PageModeUseOutlines); //PdfOutline root = writer.getDirectContent().getRootOutline(); //new PdfOutline(root, new PDFAction("http://nina.acad.bg/qmrf"), "a bookmark"); //writer.addFileAttachment(arg0, arg1, arg2, arg3) if (docBuilder == null) docBuilder = docBuilderFactory.newDocumentBuilder(); docBuilder.setErrorHandler(new SimpleErrorHandler(getClass().getName())); QMRFSchemaResolver resolver = new QMRFSchemaResolver("http://ambit2.acad.bg/qmrf/qmrf.dtd", null); resolver.setIgnoreSystemID(true); docBuilder.setEntityResolver(resolver); org.w3c.dom.Document doc = null; try { doc = docBuilder.parse(xml); } catch (Exception x) { document.addCreationDate(); document.addCreator(getClass().getName()); document.open(); document.add(new Paragraph(new Chunk(x.getMessage()))); document.close(); return; } document.addCreationDate(); document.addCreator(getClass().getName()); document.addKeywords(replaceTags(findNodeValue("keywords", doc))); document.addTitle(replaceTags(findNodeValue("QSAR_title", doc))); try { NodeList info = doc.getElementsByTagName("QMRF"); for (int i = 0; i < info.getLength(); i++) document.addSubject(findAttributeValue("name", info.item(i)) + '.' + findAttributeValue("version", info.item(i))); } catch (Exception x) { document.addSubject("QMRF"); } try { document.addAuthor(listNodeAttributes(doc, "qmrf_authors", "author_ref", "author", att_author, new Boolean(true))); } catch (Exception x) { document.addAuthor(getClass().getName()); } document.open(); PdfContentByte cb = writer.getDirectContent(); try { headerTable(document, doc); } catch (Exception x) { document.add(new Paragraph(new Chunk(x.getMessage()))); document.close(); return; } PdfOutline root = writer.getDirectContent().getRootOutline(); for (int i = 0; i < subchapters.length; i++) try { int align = Paragraph.ALIGN_LEFT; if (Mode.chapter == (Mode) subchapters[i][2]) { document.add(new Paragraph(new Chunk('\n'))); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); StringBuffer b = new StringBuffer(); b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc)); b.append('.'); b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc)); String bookmark = b.toString(); Chunk title = new Chunk(bookmark); title.setLocalDestination(bookmark); title.setFont(bfont); PdfDestination destination = new PdfDestination(PdfDestination.FITH); PdfOutline outline = new PdfOutline(root, destination, bookmark); Paragraph p = new Paragraph(title); PdfPCell cell = new PdfPCell(p); cell.setBackgroundColor(chapterColor); table.addCell(cell); document.add(table); float pos = writer.getVerticalPosition(false); if (pos < 90) document.newPage(); } else { Phrase phrase = new Phrase(); switch ((Mode) subchapters[i][2]) { case title: { StringBuffer b = new StringBuffer(); String cn = findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc); if (cn == null) break; b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_chapter, doc)); b.append('.'); b.append(findAttributeValue(subchapters[i][0].toString(), xml_attribute_name, doc)); String subchapterBookmark = b.toString(); b.append(':'); Chunk title = new Chunk(b.toString()); title.setLocalDestination(subchapterBookmark); title.setFont(bfont); phrase.add(title); PdfDestination destination = new PdfDestination(PdfDestination.FITBH); PdfOutline outline = new PdfOutline(root, destination, subchapterBookmark); break; } case text: { createNodePhrase(subchapters[i][0].toString(), doc, phrase, font); align = Paragraph.ALIGN_JUSTIFIED; break; } case answer: { String a = findAnswer(subchapters[i][0].toString(), doc); if (a != null) { Chunk answer = new Chunk(a); answer.setFont(font); phrase.add(answer); } break; } case dataset: { StringBuffer b = new StringBuffer(); b.append(findDataAvailable(subchapters[i][0].toString(), doc)); Chunk dataset = new Chunk(b.toString()); dataset.setFont(font); phrase.add(dataset); break; } case attachments: { PdfPTable table = getAttachmentsAsTable(doc, "attachment_training_data"); if (table != null) { phrase.add(new Paragraph("Training set(s)")); phrase.add(table); } table = getAttachmentsAsTable(doc, "attachment_validation_data"); if (table != null) { phrase.add(new Paragraph("Test set(s)")); phrase.add(table); } table = getAttachmentsAsTable(doc, "attachment_documents"); if (table != null) { phrase.add(new Paragraph("Supporting information")); phrase.add(table); } break; /* StringBuffer b = new StringBuffer(); b.append("Training set(s)\n"); b.append(listAttachments(doc,"attachment_training_data")); b.append("Test set(s)\n"); b.append(listAttachments(doc,"attachment_validation_data")); b.append("Supporting information\n"); b.append(listAttachments(doc,"attachment_documents")); Chunk attachments = new Chunk(b.toString()); attachments.setFont(font); phrase.add(attachments); break; */ } case reference: { try { String value = listNodeAttributes(doc, subchapters[i][0].toString(), subchapters[i][3].toString(), subchapters[i][4].toString(), (String[]) subchapters[i][5], (Boolean) subchapters[i][6]); Chunk reference = new Chunk(value); reference.setFont(font); align = Paragraph.ALIGN_JUSTIFIED; phrase.add(reference); } catch (Exception x) { } break; } } Paragraph p = new Paragraph(phrase); p.setAlignment(align); document.add(p); float pos = writer.getVerticalPosition(false); /* cb.moveTo(0, pos); cb.lineTo(PageSize.A4.width(), pos); cb.stroke(); */ if (pos < 90) document.newPage(); } } catch (Exception x) { x.printStackTrace(); } document.close(); } catch (Exception e) { e.printStackTrace(); System.err.println(e.getMessage()); } }
From source file:ambit2.data.qmrf.Qmrf_Xml_Pdf.java
License:Open Source License
public void headerTable(Document pdfdoc, org.w3c.dom.Document xmldoc) { try {//from w w w .j a v a 2 s . co m int header_font_size = 10; Image png_left = Image .getInstance(Qmrf_Xml_Pdf.class.getClassLoader().getResource("ambit/data/qmrf/logo.png")); Image png_right = Image .getInstance(Qmrf_Xml_Pdf.class.getClassLoader().getResource("ambit/data/qmrf/logo.png")); png_left.setAlignment(Image.LEFT); png_right.setAlignment(Image.RIGHT); png_left.scalePercent(60); png_right.scalePercent(60); //png.scaleAbsolute(76, 67); //png.setAlignment(Image.MIDDLE); PdfPCell cell; float[] widths = { 1f, 5f, 1f }; float[] widths1 = { 1f }; PdfPTable table = new PdfPTable(widths); PdfPTable table1 = new PdfPTable(widths1); table.setWidthPercentage(100); cell = new PdfPCell(); cell.setMinimumHeight(70); cell.addElement(new Chunk(png_left, +14, -40)); table.addCell(cell); String Text = ""; try { Text = findNodeValue(xml_QMRF_number, xmldoc); Chunk ident_title = new Chunk("QMRF identifier (ECB Inventory):"); Chunk ident_text = new Chunk(Text); Font bi_font = new Font(baseFont, header_font_size, Font.BOLDITALIC); Font i_font = new Font(baseFont, header_font_size, Font.ITALIC); ident_title.setFont(bi_font); ident_text.setFont(i_font); Paragraph p = new Paragraph(); p.add(ident_title); p.add(ident_text); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table1.addCell(cell); ident_title = new Chunk("QMRF Title:"); Phrase textPhrase = new Phrase(); createNodePhrase("QSAR_title", xmldoc, textPhrase, i_font); ident_title.setFont(bi_font); p = new Paragraph(); p.add(ident_title); p.add(textPhrase); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table1.addCell(cell); java.util.Date now = new java.util.Date(); java.text.DateFormat df = java.text.DateFormat.getDateInstance(); String date = df.format(now); ident_title = new Chunk("Printing Date:"); ident_text = new Chunk(date); ident_title.setFont(bi_font); ident_text.setFont(i_font); p = new Paragraph(); p.add(ident_title); p.add(ident_text); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table1.addCell(cell); p = new Paragraph(""); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_LEFT); table1.addCell(cell); cell = new PdfPCell(table1); cell.setPadding(0f); table.addCell(cell); } catch (Throwable x) { x.printStackTrace(); } //table.addCell(new PdfPCell(png, true)); cell.addElement(new Chunk(png_right, +14, -40)); table.addCell(cell); pdfdoc.add(table); pdfdoc.add(new Paragraph(16)); } catch (Exception x) { x.printStackTrace(); } }
From source file:at.htlpinkafeld.beans.JahresuebersichtBean.java
/** * pre processes the PDF for creating/* w w w. j a v a 2 s .co m*/ * * @param document pdf-doc * @throws DocumentException may be thrown */ public void preProcessPDF(Object document) throws DocumentException { Document pdf = (Document) document; pdf.open(); pdf.setPageSize(PageSize.A4); PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100); table.addCell(getCell("Jahresbersicht - " + selectedYear.getYear(), PdfPCell.ALIGN_LEFT)); table.addCell(getCell("", PdfPCell.ALIGN_CENTER)); table.addCell(getCell("von " + selectedUser.getPersName(), PdfPCell.ALIGN_RIGHT)); pdf.add(table); pdf.add(new Paragraph("\n")); }
From source file:at.htlpinkafeld.beans.UserDetailsBean.java
public void preProcessPDF(Object document) throws DocumentException { Document pdf = (Document) document; pdf.setPageSize(PageSize.A4.rotate()); pdf.open();/*from ww w . j a va 2 s . c o m*/ PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100); table.addCell(getCell("Monatsbersicht - " + selectedDate.format(DateTimeFormatter.ofPattern("MM.yyyy")), PdfPCell.ALIGN_LEFT)); table.addCell(getCell("", PdfPCell.ALIGN_CENTER)); table.addCell(getCell("von " + selectedUser, PdfPCell.ALIGN_RIGHT)); pdf.add(table); pdf.add(new Paragraph("\n")); }
From source file:be.fedict.eid.tsl.Tsl2PdfExporter.java
License:Open Source License
protected PdfPTable createInfoTable() { final float alpha = 0.22f; final PdfPTable t = new PdfPTable(new float[] { alpha, 1.0f - alpha }); t.getDefaultCell().setBorder(BORDER); t.setWidthPercentage(101f); return t;/*from w ww .j a v a2s . co m*/ }