Example usage for com.lowagie.text.pdf MultiColumnText addElement

List of usage examples for com.lowagie.text.pdf MultiColumnText addElement

Introduction

In this page you can find the example usage for com.lowagie.text.pdf MultiColumnText addElement.

Prototype

public void addElement(Element element) throws DocumentException 

Source Link

Document

Add an element to be rendered in a column.

Usage

From source file:com.aryjr.nheengatu.pdf.PDFDocument.java

License:Open Source License

private void extractVisibleComponents(final Tag tag, final Document doc, final MultiColumnText mct,
        final Paragraph paragraph, final List list) throws DocumentException {
    final Iterator tags = tag.tags();
    Object component;/*from w w w . j  a va  2 s .  c  om*/
    Image image;
    PDFTable table;
    final TagsManager tm = TagsManager.getInstance();

    PDFDocument.log.info("extractVisibleComponents");
    // PDFDocument.log.info(tm.states.size());
    // PDFDocument.log.info(tm.getTextIndent());
    // if (paragraph != null)
    // PDFDocument.log.info(paragraph.getFirstLineIndent());

    while (tags.hasNext()) {
        component = tags.next();
        if (component instanceof Text) {
            System.out.println("Processamento: Iniciou while -> if instanceof text");
            String s = ((Text) component).getText();
            if (s.contains("\\\"")) {
                s = s.replace("\\\"", "\"");
                ((Text) component).setText(s);
            }
            PDFDocument.log.info("text: " + ((Text) component).getText());
            // PDFDocument.log.info(tm.states.size());
            // PDFDocument.log.info(tm.getTextIndent());
            // PDFDocument.log.info(tm.getSpacingBefore());
            // PDFDocument.log.info(tm.getSpacingAfter());

            // If it's a text, create a iText text component for it
            if (paragraph != null)
                paragraph.add(PDFText.createChunk((Text) component));
            else if (list != null)
                list.add(PDFText.createParagraph((Text) component, tm));
            else
                mct.addElement(PDFText.createParagraph((Text) component, tm));
            System.out.println("Processamento: terminou while -> if instanceof text");
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("br")) {
            // PDFDocument.log.info("br");
            // PDFDocument.log.info(tm.states.size());
            // PDFDocument.log.info(tm.getTextIndent());

            // If it's a HTML line break
            if (paragraph == null) {
                mct.addElement(new Paragraph("\n"));
            } else {
                paragraph.add("\n");
            }
            System.out.println("Processamento: Iniciou while -> if instanceof tag br");
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("p")) {
            // If it's a HTML paragraph, create a iText paragraph for it

            tm.checkTag((Tag) component);
            final Paragraph p = PDFText.createParagraph(null, tm);

            PDFDocument.log.info("p");
            PDFDocument.log.info(tm.getFont().getSize());
            PDFDocument.log.info(p.getLeading());
            // PDFDocument.log.info(tm.states.size());
            // PDFDocument.log.info(tm.getTextIndent());
            // PDFDocument.log.info("align:");
            // PDFDocument.log.info(((Tag)
            // component).getPropertyValue("align"));
            // PDFDocument.log.info(tm.getAlign());

            // Paragraph p = new Paragraph();
            // p.setAlignment(tm.getAlign());
            // p.setKeepTogether(true);
            // // float b = tm.getSpacingBefore();
            // // float a = tm.getSpacingAfter();
            // p.setSpacingBefore(tm.getSpacingBefore());
            // p.setSpacingAfter(tm.getSpacingAfter());
            // p.setFirstLineIndent(tm.getTextIndent());
            extractVisibleComponents((Tag) component, doc, mct, p, list);
            if (paragraph != null)
                paragraph.add(p);
            else
                mct.addElement(p);

            // String align = ((Tag) component).getPropertyValue("align");
            // if (align != null) {
            // p.setAlignment(align.toLowerCase());
            // }
            tm.back();
            System.out.println("Processamento: Iniciou while -> if instanceof tag p");
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("ol")) {
            // If it's a HTML paragraph, create a iText paragraph for it
            tm.checkTag((Tag) component);
            if (tm.getListStyleType() == null) {
                ((GraphicsState) tm.states.get(tm.states.size() - 1)).setListStyleType("upper-roman");
            } else if (tm.getListStyleType().equals("upper-roman")) {
                ((GraphicsState) tm.states.get(tm.states.size() - 1)).setListStyleType("lower-alpha");
            }
            final List l = new RomanList(tm.getListStyleType(), 30);
            if (list != null)
                list.add(l);
            else
                mct.addElement(l);
            extractVisibleComponents((Tag) component, doc, mct, null, l);
            tm.back();
            System.out.println("Processamento: Iniciou while -> if instanceof tag ol");
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("ul")) {
            // If it's a HTML paragraph, create a iText paragraph for it
            final List l = new List(false, false, 20.0f);
            tm.checkTag((Tag) component);
            if (paragraph != null)
                paragraph.add(l);
            else
                mct.addElement(l);
            extractVisibleComponents((Tag) component, doc, mct, null, l);
            tm.back();
            System.out.println("Processamento: Iniciou while -> if instanceof tag ul");
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("li")) {
            // If it's a HTML paragraph, create a iText paragraph for it
            final ListItem li = new ListItem(tm.getFont().getSize() * 1.25f);
            li.setSpacingAfter(tm.getFont().getSize() * 0.5f);

            PDFDocument.log.info("li");
            PDFDocument.log.info(tm.getFont().getSize());
            PDFDocument.log.info(li.getLeading());

            tm.checkTag((Tag) component);
            if (list == null)
                mct.addElement(li);
            else
                list.add(li);
            extractVisibleComponents((Tag) component, doc, mct, li, list);
            tm.back();
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("img")) {
            // If it's a HTML image, create a iText image component for it
            try {
                // TODO the image path can't be static
                image = PDFImage.createImage((Tag) component);
                if (paragraph == null) {
                    mct.addElement(image);
                } else {
                    paragraph.add(image);
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
            System.out.println("Processamento: Iniciou while -> if instanceof tag img");
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("table")) {
            // If it's a HTML table, create a iText table component for it
            try {
                table = PDFTable.createTable((Tag) component);
                mct.addElement(table);
            } catch (final Exception e) {
                e.printStackTrace();
            }
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("div")) {
            final String s = ((Tag) component).getPropertyValue("style");
            if (s != null && s.equals("PAGE-BREAK-AFTER: always")) {
                doc.add(mct);
                mct.nextColumn();
            }
            tm.checkTag((Tag) component);
            extractVisibleComponents((Tag) component, doc, mct, paragraph, list);
            tm.back();
            System.out.println("Processamento: Iniciou while -> if instanceof tag div");
        } else {
            // If it's an another tag, check the name and call this method
            // again

            // PDFDocument.log.info("other!");
            // PDFDocument.log.info(tm.states.size());
            // PDFDocument.log.info(tm.getTextIndent());

            tm.checkTag((Tag) component);
            extractVisibleComponents((Tag) component, doc, mct, paragraph, list);
            tm.back();
            System.out.println("Processamento: Iniciou while -> else if");
        }
    }
}

From source file:com.preparatic.archivos.PdfGenerator.java

License:Apache License

/**
  * Genera un pdf para las preguntas y otro para las soluciones.
  * //www. ja v a  2 s.  com
  * @throws Exception
  */
 public String guardarPDF() throws Exception {

     // Abrir documentos
     docpregs = new Document();
     docsols = new Document();

     String pregsFilename = FactoriaArchivo.pdfTestFilename(test.getTipoTest(), test.getIdBloqueTematicaAnho(),
             test.getIdTest());
     PdfWriter writerPregs = PdfWriter.getInstance(docpregs,
             FactoriaArchivo.pdfTest(test.getTipoTest(), test.getIdBloqueTematicaAnho(), test.getIdTest()));
     TestFooter footerPregs = new TestFooter(test.getTitulo()); //mod_AZ_2018-02-04
     writerPregs.setPageEvent(footerPregs);

     PdfWriter.getInstance(docsols,
             FactoriaArchivo.pdfSol(test.getTipoTest(), test.getIdBloqueTematicaAnho(), test.getIdTest()));

     docpregs.open();

     docsols.open();
     docpregs.setMargins(80, 80, 72, 36);
     docsols.setMargins(80, 80, 36, 36);

     // Aadir logo
     Image gif = Image.getInstance(ConfigProperties.getProperty("files.rootDir") + "/images/logo-alpha.png"); //mod_AZE_2018-02-04 (logo con fondo blanco)

     gif.setAlignment(Image.LEFT);
     gif.scaleAbsolute(148, 46); //mod_AZE_2018-02-04 (para que entre bien el logo nuevo)
     docpregs.add(gif);
     docsols.add(gif);

     ponerTitulo(docpregs, docsols);

     // Adimos las preguntas al documento de preguntas
     docpregs.add(listaPreguntas);

     /*
      * Aadimos las respuestas, en varias columnas al documento de
      * respuestas.
      */
     MultiColumnText multiColumnTextSoluciones = new MultiColumnText();
     multiColumnTextSoluciones.addRegularColumns(docsols.left(), docsols.right(), 20f,
             Integer.parseInt(ConfigProperties.getProperty("tests.solucion.columnas")));
     multiColumnTextSoluciones.addElement(listaSoluciones);
     docsols.add(multiColumnTextSoluciones);

     // Cerramos los documentos
     docpregs.close();
     docsols.close();

     return pregsFilename;
 }