Example usage for com.lowagie.text Paragraph add

List of usage examples for com.lowagie.text Paragraph add

Introduction

In this page you can find the example usage for com.lowagie.text Paragraph add.

Prototype

public boolean add(Object o) 

Source Link

Document

Adds an Object to the Paragraph.

Usage

From source file:org.sakaiproject.tool.assessment.pdf.itext.HTMLWorker.java

License:Mozilla Public License

public void endElement(String tag) {
    if (!tagsSupported.containsKey(tag))
        return;//  w  w  w .  j a v  a 2  s .  c  o m
    try {
        String follow = (String) FactoryProperties.followTags.get(tag);
        if (follow != null) {
            cprops.removeChain(follow);
            return;
        }
        if (tag.equals("font") || tag.equals("span")) {
            cprops.removeChain(tag);
            return;
        }
        if (tag.equals("a")) {
            if (currentParagraph == null)
                currentParagraph = new Paragraph();
            ALink i = null;
            boolean skip = false;
            if (interfaceProps != null) {
                i = (ALink) interfaceProps.get("alink_interface");
                if (i != null)
                    skip = i.process(currentParagraph, cprops);
            }
            if (!skip) {
                String href = cprops.getProperty("href");
                if (href != null) {
                    ArrayList chunks = currentParagraph.getChunks();
                    for (int k = 0; k < chunks.size(); ++k) {
                        Chunk ck = (Chunk) chunks.get(k);
                        ck.setAnchor(href);
                    }
                }
            }
            Paragraph tmp = (Paragraph) stack.pop();
            Phrase tmp2 = new Phrase();
            tmp2.add(currentParagraph);
            tmp.add(tmp2);
            currentParagraph = tmp;
            cprops.removeChain("a");
            return;
        }
        if (tag.equals("blockquote")) {
            cprops.removeChain(tag);
            currentParagraph = new Paragraph();
            currentParagraph.add(factoryProperties.createChunk("\n", cprops));
            inBLOCK = false;
            return;
        }
        if (tag.equals("br")) {
            return;
        }
        if (tag.equals("hr")) {
            return;
        }
        if (currentParagraph != null) {
            if (stack.empty())
                document.add(currentParagraph);
            else {
                Object obj = stack.pop();
                if (obj instanceof TextElementArray) {
                    TextElementArray current = (TextElementArray) obj;
                    current.add(currentParagraph);
                }
                stack.push(obj);
            }
        }
        currentParagraph = null;
        if (tag.equals("ul") || tag.equals("ol")) {
            if (pendingLI)
                endElement("li");
            skipText = false;
            cprops.removeChain(tag);
            if (stack.empty())
                return;
            Object obj = stack.pop();
            if (!(obj instanceof com.lowagie.text.List)) {
                stack.push(obj);
                return;
            }
            if (stack.empty())
                document.add((Element) obj);
            else
                ((TextElementArray) stack.peek()).add(obj);
            return;
        }
        if (tag.equals("li")) {
            pendingLI = false;
            skipText = true;
            cprops.removeChain(tag);
            if (stack.empty())
                return;
            Object obj = stack.pop();
            if (!(obj instanceof ListItem)) {
                stack.push(obj);
                return;
            }
            if (stack.empty()) {
                document.add((Element) obj);
                return;
            }
            Object list = stack.pop();
            if (!(list instanceof com.lowagie.text.List)) {
                stack.push(list);
                return;
            }
            ListItem item = (ListItem) obj;
            ((com.lowagie.text.List) list).add(item);
            ArrayList cks = item.getChunks();
            if (!cks.isEmpty())
                item.getListSymbol().setFont(((Chunk) cks.get(0)).getFont());
            stack.push(list);
            return;
        }
        if (tag.equals("div") || tag.equals("body")) {
            cprops.removeChain(tag);
            return;
        }
        if (tag.equals("pre")) {
            cprops.removeChain(tag);
            isPRE = false;
            return;
        }
        if (tag.equals("p")) {
            cprops.removeChain(tag);
            return;
        }
        if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5")
                || tag.equals("h6")) {
            cprops.removeChain(tag);
            return;
        }
        if (tag.equals("table")) {
            if (pendingTR)
                endElement("tr");
            cprops.removeChain("table");
            IncTable table = (IncTable) stack.pop();
            if (table.getRows() == null || table.getRows().isEmpty()) {
                // we have an empty table skip it 
                return;
            }
            PdfPTable tb = table.buildTable();
            tb.setSplitRows(true);
            if (stack.empty())
                document.add(tb);
            else
                ((TextElementArray) stack.peek()).add(tb);
            boolean state[] = (boolean[]) tableState.pop();
            pendingTR = state[0];
            pendingTD = state[1];
            skipText = false;
            return;
        }
        if (tag.equals("tr")) {
            if (pendingTD)
                endElement("td");
            pendingTR = false;
            cprops.removeChain("tr");
            ArrayList cells = new ArrayList();
            IncTable table = null;
            while (true) {
                Object obj = stack.pop();
                if (obj instanceof IncCell) {
                    cells.add(((IncCell) obj).getCell());
                }
                if (obj instanceof IncTable) {
                    table = (IncTable) obj;
                    break;
                }
            }
            if (cells.size() > 0) {
                table.addCols(cells);
                table.endRow();
            }
            stack.push(table);
            skipText = true;
            return;
        }
        if (tag.equals("td") || tag.equals("th")) {
            pendingTD = false;
            cprops.removeChain("td");
            skipText = true;
            return;
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:org.sigmah.server.endpoint.export.sigmah.exporter.ProjectReportExporter.java

License:Open Source License

@Override
public void export(OutputStream output) throws ExportException {

    loadReport();// ww  w.ja  v a2 s  . co  m

    // Label displayed instead of the Table of Contents during the export.
    final String tocLabel = localize("projectReportTableOfContents");

    if (report != null) {
        final ProjectReportVersion version = report.getCurrentVersion();

        final ProjectReportDTO reportDTO = GetProjectReportHandler.toDTO(report, version);

        // Generating the RTF
        try {
            final Document document = new Document();
            final RtfWriter2 writer = RtfWriter2.getInstance(document, output);

            writer.setAutogenerateTOCEntries(true);

            document.open();

            // Title
            final Paragraph titleParagraph = new Paragraph(report.getName());
            titleParagraph.getFont().setSize(24);
            titleParagraph.getFont().setStyle(Font.BOLD);
            document.add(titleParagraph);

            document.add(new Paragraph()); // Empty paragraph

            // Table of contents
            final Paragraph tocParagraph = new Paragraph();
            final RtfTableOfContents toc = new RtfTableOfContents(tocLabel);
            tocParagraph.add(toc);
            document.add(tocParagraph);

            // Sections
            final List<ProjectReportSectionDTO> sections = reportDTO.getSections();
            final StringBuilder prefix = new StringBuilder();

            for (int index = 0; index < sections.size(); index++) {
                final ProjectReportSectionDTO section = sections.get(index);

                prefix.append(index + 1).append('.');
                addSection(section, prefix, index + 1, document);

                prefix.setLength(0);
            }

            document.close();

        } catch (DocumentException ex) {
            LOG.error("An error occured while generating the RTF.", ex);

        } catch (IOException e) {
            LOG.debug("An error occured while converting HTML to RTF.");
        }

    } else {
        final String idAsString = requireParameter(ExportUtils.PARAM_EXPORT_PROJECT_ID);

        LOG.error("[export] No project report is identified by '" + idAsString + "'.");
        throw new ExportException("[export] No project report is identified by '" + idAsString + "'.");
    }
}

From source file:org.tellervo.desktop.print.BasicBoxLabel.java

License:Open Source License

public void generateBoxLabel(OutputStream output) {

    try {//from   w ww.j  a  v  a 2  s .co  m

        PdfWriter writer = PdfWriter.getInstance(document, output);

        document.setPageSize(PageSize.LETTER);

        document.open();

        cb = writer.getDirectContent();

        // Set basic metadata
        document.addAuthor("Tellervo");
        document.addSubject("Tellervo Box Labels");

        PdfPTable table = new PdfPTable(2);
        table.setTotalWidth(495f);
        table.setLockedWidth(true);

        for (WSIBox b : boxlist) {
            Paragraph p = new Paragraph();

            p.add(new Chunk(b.getTitle() + Chunk.NEWLINE, labelTitleFont));
            p.add(new Chunk(Chunk.NEWLINE + b.getComments() + Chunk.NEWLINE, bodyFont));
            p.add(new Chunk(App.getLabName() + Chunk.NEWLINE + Chunk.NEWLINE, bodyFont));
            p.add(new Chunk(this.getBarCode(b), 0, 0, true));

            PdfPCell cell = new PdfPCell(p);
            cell.setPaddingLeft(15f);
            cell.setPaddingRight(15f);
            cell.setBorderColor(Color.LIGHT_GRAY);

            table.addCell(cell);

        }

        PdfPCell cell = new PdfPCell(new Paragraph());
        cell.setBorderColor(Color.LIGHT_GRAY);

        table.addCell(cell);
        document.add(table);
        document.close();

        /*float top = document.top(15);
        int row = 1;
                
        for(int i = 0; i< boxlist.size(); i = i+2)
        {
                   
           log.debug("Document left : "+document.left());
           log.debug("Document right: "+document.right());
           log.debug("Top           : "+top);
                   
                   
                   
                   
                   
                   
          // Column 1      
          ColumnText ct1a = new ColumnText(cb);
          ct1a.setSimpleColumn(document.left(), 
                   top-210, 
                   368, 
                   top, 
                   20, 
                   Element.ALIGN_LEFT);
                  
          ColumnText ct1b = new ColumnText(cb);
          ct1b.setSimpleColumn(document.left(), 
             top-70, 
             document.left()+206, 
             top-150, 
             20, 
             Element.ALIGN_LEFT);
                  
          try{
        WSIBox b1 = boxlist.get(i);
        ct1a.addText(getTitlePDF(b1));
        ct1a.go();
                     
                
        ct1b.addElement(getBarCode(b1));
        ct1b.go();
                
          } catch (Exception e)
          {
             log.debug("Failed writing box label in left column where i="+i);
          }
                  
                  
          // Column 2      
          ColumnText ct2a = new ColumnText(cb);
          ct2a.setSimpleColumn(306, 
                   top-210, 
                   document.right(), 
                   top, 
                   20, 
                   Element.ALIGN_LEFT);
                  
          ColumnText ct2b = new ColumnText(cb);
          ct2b.setSimpleColumn(306, 
             top-70, 
             512,  
             top-80, 
             20, 
             Element.ALIGN_LEFT);
                  
          try{
        WSIBox b2 = boxlist.get(i+1);
        ct2a.addText(getTitlePDF(b2));
        ct2a.go();
                     
                
        ct2b.addElement(getBarCode(b2));
        ct2b.go();
                
          } catch (Exception e)
          {
             log.debug("Failed writing box label in right column where i="+i);
             //e.printStackTrace();
          }
                  
                  
          // Column 2
        /*   ColumnText ct2 = new ColumnText(cb);
          ct2.setSimpleColumn(370,     //llx 
          top-100,            //lly   
          document.right(0),   //urx
          top+15,            //ury
          20,               //leading
          Element.ALIGN_RIGHT  //alignment
          );
                  
          try{
          WSIBox b2 = boxlist.get(i+1);
          ct2.addText(getTitlePDF(b2));
          ct2.addElement(getBarCode(b2));
          ct2.go();
          } catch (Exception e)
          {
             log.debug("Failed writing box label where i="+i+1);
          }
          */
        /*
                
        top = top-160;
                
        if(row==5)
        {
           top = document.top(15);
        document.newPage();
        row=1;
        }
        else
        {
           row++;
        }
                
                
                
                
                
        }*/

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    }

    // Close the document
    document.close();
}

From source file:org.tellervo.desktop.print.BasicBoxLabel.java

License:Open Source License

/**
 * Get an iText Paragraph for the Title 
 * /*from ww w . ja  v a2s . c  o m*/
 * @return Paragraph
 */
private Paragraph getTitlePDF(WSIBox b) {
    Paragraph p = new Paragraph();
    p.setLeading(0f);
    p.setMultipliedLeading(1.2f);
    p.add(new Phrase(10, b.getTitle() + "\n", monsterFont));
    p.add(new Phrase(10, b.getComments() + " " + App.getLabName()
            + "fdas dfsa fds sdfalkdsf jlasdj fkljkldsa jfdsklaj fdksaj flkdsaj lkfdsalk fjdsal fjdklaj fkldsajkldsfalkjsdf asdlkj dsajlk",
            bodyFont));

    //p.add(new Chunk(b.getCurationLocation(), bodyFontLarge));

    return p;
}

From source file:org.tellervo.desktop.print.BasicBoxLabel.java

License:Open Source License

/**
 * iText paragraph containing created and lastmodified timestamps
 * //from  w w w.  j a  va  2  s .c o m
 * @return Paragraph
 */
private Paragraph getTimestampPDF(WSIBox b) {
    // Set up calendar
    Date createdTimestamp = b.getCreatedTimestamp().getValue().toGregorianCalendar().getTime();
    Date nowTimestamp = new Date();

    DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);

    Paragraph p = new Paragraph();

    p.add(new Chunk("Created: ", subSubSectionFont));
    p.add(new Chunk(df1.format(createdTimestamp), bodyFont));
    //p.add(new Chunk("\nLast Modified: ", subSubSectionFont));
    //p.add(new Chunk(df1.format(lastModifiedTimestamp), bodyFontLarge));
    p.add(new Chunk("\nLabel updated: ", subSubSectionFont));
    p.add(new Chunk(df1.format(nowTimestamp), bodyFont));

    return p;

}

From source file:org.tellervo.desktop.print.BasicBoxLabel.java

License:Open Source License

private Paragraph getComments(WSIBox b) throws DocumentException {

    Paragraph p = new Paragraph();
    p.setLeading(0, 1.2f);/*from w  w  w .  ja v  a 2  s .c o  m*/

    p.add(new Chunk("Comments: \n", subSubSectionFont));
    if (b.getComments() != null) {
        p.add(new Chunk(b.getComments(), bodyFont));
    } else {
        p.add(new Chunk("No comments recorded", bodyFont));
    }

    return (p);
}

From source file:org.tellervo.desktop.print.CompleteBoxLabel.java

License:Open Source License

public void generateBoxLabel(OutputStream output) {

    try {/*from w  w  w  . ja v  a  2s  .co m*/

        PdfWriter writer = PdfWriter.getInstance(document, output);

        document.setPageSize(PageSize.LETTER);
        document.open();

        cb = writer.getDirectContent();

        // Set basic metadata
        document.addAuthor("Peter Brewer");
        document.addSubject("Box Label");

        for (WSIBox b : this.boxlist) {

            // Title Left      
            ColumnText ct = new ColumnText(cb);
            ct.setSimpleColumn(document.left(), document.top(15) - 210, 368, document.top(15), 20,
                    Element.ALIGN_LEFT);
            ct.addText(getTitlePDF(b));
            ct.go();

            // Barcode
            ColumnText ct2 = new ColumnText(cb);
            ct2.setSimpleColumn(370, document.top(15) - 100, document.right(0), document.top(0), 20,
                    Element.ALIGN_RIGHT);
            ct2.addElement(getBarCode(b));
            ct2.go();

            // Timestamp
            ColumnText ct3 = new ColumnText(cb);
            ct3.setSimpleColumn(document.left(), document.top(15) - 223, 350, document.top(15) - 60, 20,
                    Element.ALIGN_LEFT);
            ct3.setLeading(0, 1.2f);
            ct3.addText(getTimestampPDF(b));
            ct3.go();

            // Pad text
            document.add(new Paragraph(" "));
            Paragraph p2 = new Paragraph();
            p2.setSpacingBefore(70);
            p2.setSpacingAfter(10);
            p2.add(new Chunk(" ", bodyFontLarge));
            document.add(new Paragraph(p2));

            // Ring samples table
            addTable(b);
            document.add(getParagraphSpace());

            document.add(getComments(b));

            document.newPage();

        }

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    }

    // Close the document
    document.close();
}

From source file:org.tellervo.desktop.print.CompleteBoxLabel.java

License:Open Source License

/**
 * Get an iText Paragraph for the Title 
 * //from ww  w  .j a  v  a2  s .c  o  m
 * @return Paragraph
 */
private Paragraph getTitlePDF(WSIBox b) {
    Paragraph p = new Paragraph();

    p.add(new Chunk(b.getTitle() + "\n", monsterFont));
    p.add(new Chunk(App.getLabName(), subTitleFont));

    //p.add(new Chunk(b.getCurationLocation(), bodyFontLarge));

    return p;
}

From source file:org.tellervo.desktop.print.ProSheet.java

License:Open Source License

private void generateProSheet(OutputStream output) {

    Paragraph spacingPara = new Paragraph();
    spacingPara.setSpacingBefore(10);/*w  w w  .j  a va  2  s  .  c  o  m*/
    spacingPara.add(new Chunk(" ", bodyFont));

    try {

        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.setPageSize(PageSize.LETTER);

        // Set basic metadata
        document.addAuthor("Peter Brewer");
        document.addSubject("Corina Provenience Sheet for " + o.getTitle());

        HeaderFooter footer = new HeaderFooter(new Phrase(""), new Phrase(""));
        footer.setAlignment(Element.ALIGN_RIGHT);
        footer.setBorder(0);
        document.setFooter(footer);

        HeaderFooter header = new HeaderFooter(new Phrase(o.getLabCode() + " - " + o.getTitle(), bodyFont),
                false);
        header.setAlignment(Element.ALIGN_RIGHT);
        header.setBorder(0);
        document.setHeader(header);

        document.open();
        cb = writer.getDirectContent();

        // Title Left      
        ColumnText ct = new ColumnText(cb);
        ct.setSimpleColumn(document.left(), document.top() - 193, document.right(), document.top() - 20, 20,
                Element.ALIGN_LEFT);
        ct.addText(getTitlePDF());
        ct.go();

        // Timestamp
        ColumnText ct3 = new ColumnText(cb);
        ct3.setSimpleColumn(document.left(), document.top() - 223, 283, document.top() - 60, 20,
                Element.ALIGN_LEFT);
        ct3.setLeading(0, 1.2f);
        ct3.addText(getTimestampPDF());
        ct3.go();

        // Pad text
        document.add(spacingPara);
        document.add(getObjectDescription());
        document.add(getObjectComments());

        document.add(spacingPara);

        getElementTable();

    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    }

    // Close the document
    document.close();
}

From source file:org.tellervo.desktop.print.ProSheet.java

License:Open Source License

/**
 * iText paragraph containing created and lastmodified timestamps
 * /*from   ww  w  .j av a 2 s.c o  m*/
 * @return Paragraph
 */
private Paragraph getTimestampPDF() {

    Date now = new Date();

    DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);

    Paragraph p = new Paragraph();

    p.add(new Chunk("Printed: ", subSubSectionFont));
    p.add(new Chunk(df1.format(now), bodyFont));

    return p;

}