Example usage for com.lowagie.text Paragraph setAlignment

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

Introduction

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

Prototype

public void setAlignment(String alignment) 

Source Link

Document

Sets the alignment of this paragraph.

Usage

From source file:com.afunms.report.abstraction.ExcelReport1.java

public void createReport_cabinetWord(String filename, String room) throws DocumentException, IOException {
    if (impReport.getTable() == null) {
        fileName = null;//from www  . j a v a2 s  .com
        return;
    }
    try {
        // 
        Document document = new Document(PageSize.A4);
        // (Writer)document(Writer)
        RtfWriter2.getInstance(document, new FileOutputStream(filename));
        document.open();
        // new File(filename)
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);
        // 
        Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
        // title.setFont(titleFont);
        List cabinetlist = (List) reportHash.get("cabinetlist");

        Paragraph context = new Paragraph();
        // 
        context.setAlignment(Element.ALIGN_CENTER);
        //         context.setFont(contextFont);
        // 
        //         context.setSpacingBefore(0);
        // 
        //         context.setFirstLineIndent(6);
        //         document.add(context);
        //         document.add(new Paragraph("\n"));
        Table aTable = new Table(6);
        //         float[] widths = { 220f, 220f, 220f, 110f, 110f, 110f, 110f, 220f };
        //         aTable.setWidths(widths);
        aTable.setWidth(100); //  90%
        aTable.setAlignment(Element.ALIGN_CENTER);// 
        aTable.setAutoFillEmptyCells(true); // 
        //         aTable.setBorderWidth(1); // 
        //         aTable.setBorderColor(new Color(0, 125, 255)); // 
        //         aTable.setPadding(2);// 
        //         aTable.setSpacing(4);// 
        //         aTable.setBorder(2);// 
        Cell cell = null;
        cell = new Cell(new Phrase(room + "", titleFont));
        cell.setRowspan(2);
        cell.setColspan(6);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);
        cell = new Cell(new Phrase("", titleFont));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("U", titleFont));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("U", titleFont));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("U", titleFont));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("U(%)", titleFont));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);
        Cabinet cabinet = null;
        if (cabinetlist != null && cabinetlist.size() > 0) {
            for (int i = 0; i < cabinetlist.size(); i++) {

                cabinet = (Cabinet) cabinetlist.get(i);
                cell = new Cell(new Phrase(1 + i + "", contextFont));
                cell.setRowspan(2);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(cabinet.getName(), contextFont));
                cell.setRowspan(2);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

                cell = new Cell(new Phrase(cabinet.getAllu(), contextFont));
                cell.setRowspan(2);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(cabinet.getUseu(), contextFont));
                cell.setRowspan(2);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(cabinet.getTempu(), contextFont));
                cell.setRowspan(2);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(cabinet.getRateu(), contextFont));
                cell.setRowspan(2);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

            }
        }
        document.add(aTable);
        document.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
}

From source file:com.afunms.report.abstraction.ExcelReport1.java

public void createReport_equipmentWord(String filename, String roomname, String cabinetname, String uselect,
        String unumber, String rate) throws DocumentException, IOException {
    if (impReport.getTable() == null) {
        fileName = null;//w  w  w  .j a  v a2s  . co  m
        return;
    }
    try {
        // 
        Document document = new Document(PageSize.A4);
        // (Writer)document(Writer)
        RtfWriter2.getInstance(document, new FileOutputStream(filename));
        document.open();
        // new File(filename)
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);
        // 
        Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
        // title.setFont(titleFont);
        List equipmentlist = (List) reportHash.get("equipmentlist");

        Paragraph context = new Paragraph();
        // 
        context.setAlignment(Element.ALIGN_CENTER);
        //         context.setFont(contextFont);
        // 
        //         context.setSpacingBefore(0);
        // 
        //         context.setFirstLineIndent(6);
        //         document.add(context);
        //         document.add(new Paragraph("\n"));
        Table aTable = new Table(6);
        //         float[] widths = { 220f, 220f, 220f, 110f, 110f, 110f, 110f, 220f };
        //         aTable.setWidths(widths);
        aTable.setWidth(100); //  90%
        aTable.setAlignment(Element.ALIGN_CENTER);// 
        aTable.setAutoFillEmptyCells(true); // 
        //         aTable.setBorderWidth(1); // 
        //         aTable.setBorderColor(new Color(0, 125, 255)); // 
        //         aTable.setPadding(2);// 
        //         aTable.setSpacing(4);// 
        //         aTable.setBorder(2);// 
        Cell cell = null;
        cell = new Cell(new Phrase(roomname + cabinetname + "", titleFont));
        cell.setColspan(6);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + roomname, titleFont));
        cell.setColspan(3);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + cabinetname, titleFont));
        cell.setColspan(3);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("U:" + uselect, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + unumber, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + rate, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);
        EquipmentReport equipmentReport = null;
        if (equipmentlist != null && equipmentlist.size() > 0) {
            for (int i = 0; i < equipmentlist.size(); i++) {

                equipmentReport = (EquipmentReport) equipmentlist.get(i);
                cell = new Cell(new Phrase(1 + i + "", contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(equipmentReport.getEquipmentname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

                cell = new Cell(new Phrase(equipmentReport.getEquipmentdesc(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(equipmentReport.getOperation(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(equipmentReport.getContactname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(equipmentReport.getContactphone(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

            }
        }
        document.add(aTable);
        document.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
}

From source file:com.afunms.report.abstraction.ExcelReport1.java

public void createReport_OperEquipmentWord(String filename, String opername, String contactname,
        String contactphone) throws DocumentException, IOException {
    if (impReport.getTable() == null) {
        fileName = null;/*from   ww  w  . j  a  v  a 2  s .  com*/
        return;
    }
    try {
        // 
        Document document = new Document(PageSize.A4);
        // (Writer)document(Writer)
        RtfWriter2.getInstance(document, new FileOutputStream(filename));
        document.open();
        // new File(filename)
        BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 
        Font titleFont = new Font(bfChinese, 12, Font.BOLD);
        // 
        Font contextFont = new Font(bfChinese, 12, Font.NORMAL);
        // title.setFont(titleFont);
        List equipmentlist = (List) reportHash.get("equipmentlist");

        Paragraph context = new Paragraph();
        // 
        context.setAlignment(Element.ALIGN_CENTER);
        //         context.setFont(contextFont);
        // 
        //         context.setSpacingBefore(0);
        // 
        //         context.setFirstLineIndent(6);
        //         document.add(context);
        //         document.add(new Paragraph("\n"));
        Table aTable = new Table(7);
        //         float[] widths = { 220f, 220f, 220f, 110f, 110f, 110f, 110f, 220f };
        //         aTable.setWidths(widths);
        aTable.setWidth(100); //  90%
        aTable.setAlignment(Element.ALIGN_CENTER);// 
        aTable.setAutoFillEmptyCells(true); // 
        //         aTable.setBorderWidth(1); // 
        //         aTable.setBorderColor(new Color(0, 125, 255)); // 
        //         aTable.setPadding(2);// 
        //         aTable.setSpacing(4);// 
        //         aTable.setBorder(2);// 
        Cell cell = null;
        cell = new Cell(new Phrase(opername + "", titleFont));
        cell.setColspan(7);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + opername, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + contactname, titleFont));
        cell.setColspan(2);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase(":" + contactphone, titleFont));
        cell.setColspan(3);
        this.setCellFormat(cell, false);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("U", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("IP", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);

        cell = new Cell(new Phrase("", titleFont));

        cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
        aTable.addCell(cell);
        OperCabinet operCabinet = null;
        if (equipmentlist != null && equipmentlist.size() > 0) {
            for (int i = 0; i < equipmentlist.size(); i++) {

                operCabinet = (OperCabinet) equipmentlist.get(i);
                cell = new Cell(new Phrase(operCabinet.getRoomname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getCabinetname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

                cell = new Cell(new Phrase(operCabinet.getUseu(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getEquipmentname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getIpaddress(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getCabinetname(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);
                cell = new Cell(new Phrase(operCabinet.getContactphone(), contextFont));

                cell.setHorizontalAlignment(Element.ALIGN_CENTER); // 
                cell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 
                aTable.addCell(cell);

            }
        }
        document.add(aTable);
        document.close();
    } catch (Exception e) {
        // TODO: handle exception
    }
}

From source file:com.amphisoft.epub2pdf.content.TextFactory.java

License:Open Source License

public Paragraph newParagraph() {
    Paragraph p = new Paragraph();
    p.setAlignment(alignment.value);
    p.setFont(_defaultFont);/*from  ww  w .java2s .c  o  m*/
    currentFont = _defaultFont;
    p.setHyphenation(new HyphenationAuto("en", "US", 2, 2));
    p.setLeading(0F, getCurrentLeadingMultiplier());
    p.setSpacingAfter(_defaultFont.getSize() * 0.33F);
    currentParagraph = p;
    return p;
}

From source file:com.amphisoft.epub2pdf.content.TextFactory.java

License:Open Source License

public Paragraph newParagraphPre() {
    Paragraph p = new Paragraph();
    p.setAlignment(ITAlignment.LEFT.value);
    p.setFont(_defaultFontMono);//w w  w .  j  a  v a  2  s.c o m
    currentFont = _defaultFontMono;
    p.setLeading(0F, getCurrentLeadingMultiplier());
    p.setSpacingAfter(_defaultFont.getSize() * 0.33F);
    currentParagraph = p;
    return p;
}

From source file:com.amphisoft.epub2pdf.content.TextFactory.java

License:Open Source License

public Paragraph newHeadline(int i) {
    if (i < 1) {
        i = 1;//from   w  ww  .j  a  va  2  s.  c  o m
    }
    if (i > 6) {
        i = 6;
    }
    Font hFont = FontFactory.getFont(_defaultFont.getFamilyname(), FontFactory.defaultEncoding,
            BaseFont.EMBEDDED, baseFontSize * HMULTS[i - 1], Font.BOLD);
    Paragraph h = new Paragraph();
    h.setAlignment(ITAlignment.CENTER.value);
    h.setFont(hFont);
    currentFont = hFont;
    h.setLeading(0F, getCurrentLeadingMultiplier());
    h.setSpacingAfter(_defaultFont.getSize() * 0.33F);
    currentParagraph = h;
    return h;
}

From source file:com.amphisoft.epub2pdf.content.XhtmlHandler.java

License:Open Source License

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
    /*//from w  ww .  j  a va 2 s  .co m
    if("ol".equals(qName) || "ul".equals(qName) || "li".equals(qName)) {
       System.err.print(qName + " ");
    }
     */
    currentSaxElemId = saxElemIdCounter;

    Map<String, String> attrMap = new HashMap<String, String>();
    // parse attributes
    for (int ai = 0; ai < attributes.getLength(); ai++) {
        attrMap.put(attributes.getQName(ai), attributes.getValue(ai));
    }
    String idAttr = attrMap.get("id");
    if (idAttr == null) {
        idAttr = "";
    }
    String className = attrMap.get("class");
    if (className == null) {
        className = "";
    }
    SaxElement sE = new SaxElement(qName, saxElemIdCounter++, className, idAttr, currentITextStyle);
    //printlnerr("startElement: " + sE.toString());
    saxElementStack.push(sE);

    try {
        if (attrMap.get("class") != null) {
            String[] elemClasses = attrMap.get("class").split(" ");

            for (String eClass : elemClasses) {

                StyleSpecText classTextStyles = styleMap.getTextStyleSpecFor(qName, eClass);
                if (classTextStyles != null) {
                    sE.applyTextStyles(classTextStyles);
                }

            }
        }
        if (attrMap.get("style") != null) {
            // TODO this needs more thought, and careful tracking of which tags are still open, etc.
            //String styleSource = attrMap.get("style");
            //CssStyleMap styleTagStyles = cssParser.getStylesFromStyleTag(styleSource);
            // ...
        }
        if (sE.textStyles == null) {
            try {
                int stackSize = saxElementStack.size();
                if (stackSize > 1) {
                    SaxElement enclosingElement = saxElementStack.elementAt(stackSize - 2);
                    StyleSpecText enclosingSST = enclosingElement.textStyles;
                    if (enclosingSST != null)
                        sE.applyTextStyles(enclosingSST);
                }
            } catch (Exception e) {
            }
        }

        StyleSpecText currentTextStyles = sE.textStyles;
        if (currentTextStyles != null) {
            if (currentTextStyles.isBold()) {
                currentITextStyle |= Font.BOLD;
            }
            if (currentTextStyles.isItalic()) {
                currentITextStyle |= Font.ITALIC;
            }
        }

        //System.err.println("PUSH -> " + saxElementStack);

        previousTag = currentTag;
        currentTag = qName;
        if (document.isOpen()) {
            if (XhtmlTags.NEWLINE.equals(qName)) {
                if (stack.size() > 0) {
                    TextElementArray currentTEA = (TextElementArray) stack.peek();
                    currentTEA.add(Chunk.NEWLINE);
                } else if (specialParagraph != null) {
                    specialParagraph.add(Chunk.NEWLINE);
                }
            }

            updateStack();

            String xmlElementId = attrMap.get("id");

            if (XhtmlTags.ANCHOR.equals(qName)) {
                //concession to nonconformists...
                if (xmlElementId == null) {
                    xmlElementId = attrMap.get("name");
                }
                Anchor anchor = textFactory.newAnchor();
                String ref = attrMap.get(XhtmlTags.REFERENCE);

                if (ref != null) {
                    int aNameStartIdx = ref.lastIndexOf("#") + 1;
                    ref = ref.substring(aNameStartIdx);
                    anchor.setReference(ref);
                }
                if (xmlElementId != null) {
                    anchor.setName(xmlElementId);
                }
                pushToStack(anchor);
            } else {
                if (xmlElementId != null) {
                    //flushStack();
                    Anchor dest = textFactory.newAnchor();
                    dest.setName(xmlElementId);
                    pushToStack(dest);
                    //flushStack();
                }
                for (int i = 0; i < 6; i++) {
                    if (XhtmlTags.H[i].equals(qName)) {
                        flushStack();
                        freshParagraph = true;
                        currentITextStyle |= Font.BOLD;
                        specialParagraph = textFactory.newHeadline(i + 1);
                        return;
                    }
                }
                if ("blockquote".equals(qName)) {
                    flushStack();
                    freshParagraph = true;
                    Paragraph p = textFactory.newParagraph();
                    p.setIndentationLeft(50);
                    p.setIndentationRight(20);
                    p.setAlignment(defaultAlignment);
                    pushToStack(p);
                } else if (XhtmlTags.PARAGRAPH.equals(qName)) {
                    flushStack();
                    freshParagraph = true;
                    Paragraph p = textFactory.newParagraph();
                    pushToStack(p);
                } else if (XhtmlTags.DIV.equals(qName)) {
                    if (stack.size() > 0 && stack.peek().getChunks().size() > 0) {
                        flushStack();
                    }
                    if (stack.size() == 0) {
                        Paragraph brandNewParagraph = textFactory.newParagraph();
                        pushToStack(brandNewParagraph);
                        freshParagraph = true;
                    }
                } else if (XhtmlTags.PRE.equals(qName)) {
                    flushStack();
                    freshParagraph = true;
                    Paragraph p = textFactory.newParagraphPre();
                    pushToStack(p);
                } else if (XhtmlTags.ORDEREDLIST.equals(qName)) {
                    flushStack();
                    List oList = new List(List.ORDERED, 10);
                    pushToStack(oList);
                } else if (XhtmlTags.UNORDEREDLIST.equals(qName)) {
                    flushStack();
                    List uList = new List(List.UNORDERED, 10);
                    pushToStack(uList);
                } else if (XhtmlTags.LISTITEM.equals(qName)) {
                    freshParagraph = true;
                    ListItem listItem = new ListItem();
                    pushToStack(listItem);
                } else if (XhtmlTags.IMAGE.equals(qName)) {
                    handleImage(attributes);
                } else if (qName != null && qName.endsWith("image")) {
                    handleSvgImage(attributes);
                } else if (XhtmlTags.LINK.equals(qName)) {
                    // if it's a stylesheet, parse it & update current-style
                    if ("stylesheet".equals(attrMap.get("rel")) && "text/css".equals(attrMap.get("type"))
                            && attrMap.get("href") != null) {
                        String cssHref = xhtmlDir.getAbsoluteFile().toURI().toString() + attrMap.get("href");
                        CssStyleMap stylesFromLink = cssParser.getStylesFromFileURI(cssHref);
                        if (stylesFromLink != null) {
                            styleMap.updateWith(stylesFromLink);
                        }
                    }
                } else if (XhtmlTags.STYLE.equals(qName)) {
                    inStyleTag = true;
                } else if (XhtmlTags.EM.equals(qName) || "I".equals(qName.toUpperCase())) {
                    currentITextStyle |= Font.ITALIC;
                } else if (XhtmlTags.STRONG.equals(currentTag) || "B".equals(qName.toUpperCase())) {
                    currentITextStyle |= Font.BOLD;
                }

            }

        } else if (XhtmlTags.BODY.equals(qName)) {
            document.open();
            freshParagraph = true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    //printlnerr("leaving startElement " + localName + "; stack: " + stackStatus());
}

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

License:Open Source License

private static float extractVisibleComponents(final Tag tag, final PdfPCell cell, final Paragraph paragraph,
        float cellWidth) throws DocumentException {
    final Iterator tags = tag.tags();// tags inside the cell
    Object component;/*from w  w  w . ja va  2s  .  c o m*/
    final TagsManager tm = TagsManager.getInstance();
    // Seting the HTML row or table background color
    if (tag.getName().equalsIgnoreCase("TD")) {
        cell.setBorderColor(tm.getPreviousState(2).getBgcolor());
        cell.setBorderWidth(tag.getPropertyValue(PDFTable.CELLSPACING) == null ? 1
                : Integer.parseInt(tag.getPropertyValue(PDFTable.CELLSPACING)));
        //cell.setBackgroundColor(tm.getBgcolor());
        cell.setBackgroundColor(new Color(255, 255, 255));
        cell.setVerticalAlignment(tm.getValign());
        cell.setHorizontalAlignment(tm.getAlign());
        paragraph.setAlignment(tm.getAlign());
    }
    while (tags.hasNext()) {
        component = tags.next();
        if (component instanceof Text) {
            String s = ((Text) component).getText();
            if (s.contains("\\\"")) {
                s = s.replace("\\\"", "\"");
                ((Text) component).setText(s);
            }
            // If it's a text, create a iText text component for it
            paragraph.add(PDFText.createChunk((Text) component));
            cellWidth += PDFText.getWidth((Text) component);
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("br")) {
            // If it's a HTML line break
            paragraph.add("\n");
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("img")) {
            // If it's a HTML image, create a iText image component for it
            try {
                final Image img = PDFImage.createImage((Tag) component);
                //Nato: paragraph.add(new Chunk(img, 0, -2));
                paragraph.add(new Chunk(img, 0, 0));
                cellWidth += img.getScaledWidth();
                // TODO Is there an iText bug here?
                //if (img.scaledHeight() == 1f) {
                //cell.setFixedHeight(img.getScaledHeight());
                //}
            } catch (final Exception e) {
                e.printStackTrace();
            }
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("table")) {
            // If it's a HTML table, create a iText table component for it
            try {
                // TODO the default value of nowrap here will be true or
                // false??? If true, there is a problem with the cell width
                cell.setNoWrap(false);
                final PDFTable t = PDFTable.createTable((Tag) component);
                cell.addElement(t);
                final float[] cw = t.getWidths();
                for (final float element : cw) {
                    cellWidth += element;
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
        } else if (component instanceof Tag && ((Tag) component).getName().equalsIgnoreCase("select")) {
            // If it's a HTML select field, I will get only the selected
            // option
            final Tag select = (Tag) component;
            Tag opt;
            tm.checkTag(select);
            final Iterator opts = select.tags();
            boolean selected = false;
            while (opts.hasNext()) {
                opt = (Tag) opts.next();
                if (opt.getPropertyValue("selected") != null) {
                    tm.checkTag(opt);
                    cellWidth = PDFTable.extractVisibleComponents(opt, cell, paragraph, cellWidth);
                    selected = true;
                    break;
                }
            }
            if (!selected) {
                // if none option have the selected attribute the first will
                // be shown
                opt = select.getFirstTag("option");
                tm.checkTag(opt);
                cellWidth = PDFTable.extractVisibleComponents(opt, cell, paragraph, cellWidth);
            }
        } else {
            // If it's an another tag, check the name and call this method
            // again
            tm.checkTag((Tag) component);
            cellWidth = PDFTable.extractVisibleComponents((Tag) component, cell, paragraph, cellWidth);
            tm.back();
        }
    }
    return cellWidth;
}

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

License:Open Source License

public static Paragraph createParagraph(final Text htmlText, TagsManager tm) {

    if (tm == null)
        tm = TagsManager.getInstance();// w  ww . j  a  v  a2 s.co m

    final Paragraph text = new Paragraph(htmlText != null ? htmlText.getText() : null, tm.getFont());
    text.setAlignment(tm.getAlign());
    text.setKeepTogether(true);
    // float b = tm.getSpacingBefore();
    // float a = tm.getSpacingAfter();
    text.setSpacingBefore(tm.getSpacingBefore());
    text.setSpacingAfter(tm.getSpacingAfter());
    text.setFirstLineIndent(tm.getTextIndent());
    text.setIndentationLeft(tm.getMarginLeft());
    // text.setFirstLineIndent(20f);
    text.setLeading(tm.getFont().getSize() * 1.5f);
    return text;
}

From source file:com.aurel.track.util.PdfUtils.java

License:Open Source License

public static void createPdfFromText(StringBuilder text, File pdfFile) {
    Document output = null;/*from  w  ww  .  j  a v a 2 s  .c  o  m*/
    try {
        BufferedReader input = new BufferedReader(new StringReader(text.toString()));
        // Size DIN A4
        //    see com.lowagie.text.PageSize for a complete list of page-size constants.
        output = new Document(PageSize.A4, 40, 40, 40, 40);
        float fntSize, lineSpacing;
        fntSize = 9f;
        lineSpacing = 11f;
        Font font1 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        Font font2 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        font2.setColor(Color.BLUE);
        Font font3 = FontFactory.getFont(FontFactory.COURIER, fntSize);
        font3.setColor(Color.RED);

        PdfWriter.getInstance(output, new FileOutputStream(pdfFile));

        output.open();
        output.addAuthor("Steinbeis");
        output.addSubject("Debug Info");
        output.addTitle(pdfFile.getName());

        String line = "";
        while (null != (line = input.readLine())) {
            Font ft = font1;
            if (line.startsWith("%")) {
                ft = font2;
            }
            if (line.startsWith("% ^^^") || line.startsWith("% vvv")) {
                ft = font3;
            }
            Paragraph p = new Paragraph(lineSpacing, line, ft);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            output.add(p);
        }
        output.close();
        input.close();
    } catch (Exception e) {
        LOGGER.debug("Problem creating debug info pdf file", e);
    }
}