Search in sources :

Example 6 with LineSeparator

use of com.lowagie.text.pdf.draw.LineSeparator in project mes by qcadoo.

the class PalletNumberReportPdf method addPalletNumber.

private void addPalletNumber(final Document document, final String number) throws DocumentException {
    LineSeparator lineSeparator = new LineSeparator(1, 100f, ColorUtils.getLineDarkColor(), Element.ALIGN_LEFT, 0);
    Paragraph numberParagraph = new Paragraph(new Phrase(number, FontUtils.getDejavuBold70Dark()));
    numberParagraph.setAlignment(Element.ALIGN_CENTER);
    numberParagraph.setSpacingBefore(70F);
    numberParagraph.setSpacingAfter(180F);
    document.add(Chunk.NEWLINE);
    document.add(numberParagraph);
    document.add(lineSeparator);
}
Also used : Phrase(com.lowagie.text.Phrase) LineSeparator(com.lowagie.text.pdf.draw.LineSeparator) Paragraph(com.lowagie.text.Paragraph)

Example 7 with LineSeparator

use of com.lowagie.text.pdf.draw.LineSeparator in project itext2 by albfernandez.

the class SAXiTextHandler method handleStartingTags.

/**
 * This method deals with the starting tags.
 *
 * @param name
 *            the name of the tag
 * @param attributes
 *            the list of attributes
 */
public void handleStartingTags(String name, Properties attributes) {
    // System.err.println("Start: " + name);
    if (ignore || ElementTags.IGNORE.equals(name)) {
        ignore = true;
        return;
    }
    // maybe there is some meaningful data that wasn't between tags
    if (currentChunk != null) {
        TextElementArray current;
        try {
            current = (TextElementArray) stack.pop();
        } catch (EmptyStackException ese) {
            if (bf == null) {
                current = new Paragraph("", new Font());
            } else {
                current = new Paragraph("", new Font(this.bf));
            }
        }
        current.add(currentChunk);
        stack.push(current);
        currentChunk = null;
    }
    // chunks
    if (ElementTags.CHUNK.equals(name)) {
        currentChunk = ElementFactory.getChunk(attributes);
        if (bf != null) {
            currentChunk.setFont(new Font(this.bf));
        }
        return;
    }
    // symbols
    if (ElementTags.ENTITY.equals(name)) {
        Font f = new Font();
        if (currentChunk != null) {
            handleEndingTags(ElementTags.CHUNK);
            f = currentChunk.getFont();
        }
        currentChunk = EntitiesToSymbol.get(attributes.getProperty(ElementTags.ID), f);
        return;
    }
    // phrases
    if (ElementTags.PHRASE.equals(name)) {
        stack.push(ElementFactory.getPhrase(attributes));
        return;
    }
    // anchors
    if (ElementTags.ANCHOR.equals(name)) {
        stack.push(ElementFactory.getAnchor(attributes));
        return;
    }
    // paragraphs and titles
    if (ElementTags.PARAGRAPH.equals(name) || ElementTags.TITLE.equals(name)) {
        stack.push(ElementFactory.getParagraph(attributes));
        return;
    }
    // lists
    if (ElementTags.LIST.equals(name)) {
        stack.push(ElementFactory.getList(attributes));
        return;
    }
    // listitems
    if (ElementTags.LISTITEM.equals(name)) {
        stack.push(ElementFactory.getListItem(attributes));
        return;
    }
    // cells
    if (ElementTags.CELL.equals(name)) {
        stack.push(ElementFactory.getCell(attributes));
        return;
    }
    // tables
    if (ElementTags.TABLE.equals(name)) {
        Table table = ElementFactory.getTable(attributes);
        float[] widths = table.getProportionalWidths();
        for (int i = 0; i < widths.length; i++) {
            if (widths[i] == 0) {
                widths[i] = 100.0f / widths.length;
            }
        }
        try {
            table.setWidths(widths);
        } catch (BadElementException bee) {
            // this shouldn't happen
            throw new ExceptionConverter(bee);
        }
        stack.push(table);
        return;
    }
    // sections
    if (ElementTags.SECTION.equals(name)) {
        Element previous = (Element) stack.pop();
        Section section;
        try {
            section = ElementFactory.getSection((Section) previous, attributes);
        } catch (ClassCastException cce) {
            throw new ExceptionConverter(cce);
        }
        stack.push(previous);
        stack.push(section);
        return;
    }
    // chapters
    if (ElementTags.CHAPTER.equals(name)) {
        stack.push(ElementFactory.getChapter(attributes));
        return;
    }
    // images
    if (ElementTags.IMAGE.equals(name)) {
        try {
            Image img = ElementFactory.getImage(attributes);
            try {
                addImage(img);
                return;
            } catch (EmptyStackException ese) {
                // to the document
                try {
                    document.add(img);
                } catch (DocumentException de) {
                    throw new ExceptionConverter(de);
                }
                return;
            }
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }
    // annotations
    if (ElementTags.ANNOTATION.equals(name)) {
        Annotation annotation = ElementFactory.getAnnotation(attributes);
        TextElementArray current;
        try {
            try {
                current = (TextElementArray) stack.pop();
                try {
                    current.add(annotation);
                } catch (Exception e) {
                    document.add(annotation);
                }
                stack.push(current);
            } catch (EmptyStackException ese) {
                document.add(annotation);
            }
            return;
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
    // newlines
    if (isNewline(name)) {
        TextElementArray current;
        try {
            current = (TextElementArray) stack.pop();
            current.add(Chunk.NEWLINE);
            stack.push(current);
        } catch (EmptyStackException ese) {
            if (currentChunk == null) {
                try {
                    document.add(Chunk.NEWLINE);
                } catch (DocumentException de) {
                    throw new ExceptionConverter(de);
                }
            } else {
                currentChunk.append("\n");
            }
        }
        return;
    }
    // newpage
    if (isNewpage(name)) {
        TextElementArray current;
        try {
            current = (TextElementArray) stack.pop();
            Chunk newPage = new Chunk("");
            newPage.setNewPage();
            if (bf != null) {
                newPage.setFont(new Font(this.bf));
            }
            current.add(newPage);
            stack.push(current);
        } catch (EmptyStackException ese) {
            document.newPage();
        }
        return;
    }
    if (ElementTags.HORIZONTALRULE.equals(name)) {
        TextElementArray current;
        LineSeparator hr = new LineSeparator(1.0f, 100.0f, null, Element.ALIGN_CENTER, 0);
        try {
            current = (TextElementArray) stack.pop();
            current.add(hr);
            stack.push(current);
        } catch (EmptyStackException ese) {
            try {
                document.add(hr);
            } catch (DocumentException de) {
                throw new ExceptionConverter(de);
            }
        }
        return;
    }
    // documentroot
    if (isDocumentRoot(name)) {
        String key;
        String value;
        // pagesize and orientation specific code suggested by Samuel Gabriel
        // Updated by Ricardo Coutinho. Only use if set in html!
        Rectangle pageSize = null;
        String orientation = null;
        for (Iterator i = attributes.keySet().iterator(); i.hasNext(); ) {
            key = (String) i.next();
            value = attributes.getProperty(key);
            try {
                // margin specific code suggested by Reza Nasiri
                if (ElementTags.LEFT.equalsIgnoreCase(key))
                    leftMargin = Float.parseFloat(value + "f");
                if (ElementTags.RIGHT.equalsIgnoreCase(key))
                    rightMargin = Float.parseFloat(value + "f");
                if (ElementTags.TOP.equalsIgnoreCase(key))
                    topMargin = Float.parseFloat(value + "f");
                if (ElementTags.BOTTOM.equalsIgnoreCase(key))
                    bottomMargin = Float.parseFloat(value + "f");
            } catch (Exception ex) {
                throw new ExceptionConverter(ex);
            }
            if (ElementTags.PAGE_SIZE.equals(key)) {
                try {
                    String pageSizeName = value;
                    Field pageSizeField = PageSize.class.getField(pageSizeName);
                    pageSize = (Rectangle) pageSizeField.get(null);
                } catch (Exception ex) {
                    throw new ExceptionConverter(ex);
                }
            } else if (ElementTags.ORIENTATION.equals(key)) {
                try {
                    if ("landscape".equals(value)) {
                        orientation = "landscape";
                    }
                } catch (Exception ex) {
                    throw new ExceptionConverter(ex);
                }
            } else {
                try {
                    document.add(new Meta(key, value));
                } catch (DocumentException de) {
                    throw new ExceptionConverter(de);
                }
            }
        }
        if (pageSize != null) {
            if ("landscape".equals(orientation)) {
                pageSize = pageSize.rotate();
            }
            document.setPageSize(pageSize);
        }
        document.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);
        if (controlOpenClose)
            document.open();
    }
}
Also used : Meta(com.lowagie.text.Meta) Table(com.lowagie.text.Table) BadElementException(com.lowagie.text.BadElementException) Element(com.lowagie.text.Element) Rectangle(com.lowagie.text.Rectangle) Image(com.lowagie.text.Image) Chunk(com.lowagie.text.Chunk) Section(com.lowagie.text.Section) LineSeparator(com.lowagie.text.pdf.draw.LineSeparator) BaseFont(com.lowagie.text.pdf.BaseFont) Font(com.lowagie.text.Font) BadElementException(com.lowagie.text.BadElementException) EmptyStackException(java.util.EmptyStackException) DocumentException(com.lowagie.text.DocumentException) Annotation(com.lowagie.text.Annotation) Paragraph(com.lowagie.text.Paragraph) EmptyStackException(java.util.EmptyStackException) ExceptionConverter(com.lowagie.text.ExceptionConverter) Field(java.lang.reflect.Field) TextElementArray(com.lowagie.text.TextElementArray) DocumentException(com.lowagie.text.DocumentException) Iterator(java.util.Iterator)

Example 8 with LineSeparator

use of com.lowagie.text.pdf.draw.LineSeparator in project itext2 by albfernandez.

the class HTMLWorker method startElement.

public void startElement(String tag, HashMap h) {
    if (!tagsSupported.containsKey(tag))
        return;
    try {
        if (!pendingLI && pendingUL && !tag.equals(HtmlTags.LISTITEM)) {
            endElement(HtmlTags.UNORDEREDLIST);
            pendingUL = false;
        }
        style.applyStyle(tag, h);
        String follow = (String) FactoryProperties.followTags.get(tag);
        if (follow != null) {
            HashMap prop = new HashMap();
            prop.put(follow, null);
            cprops.addToChain(follow, prop);
            return;
        }
        FactoryProperties.insertStyle(h, cprops);
        if (tag.equals(HtmlTags.ANCHOR)) {
            cprops.addToChain(tag, h);
            if (currentParagraph == null) {
                currentParagraph = new Paragraph();
            }
            stack.push(currentParagraph);
            currentParagraph = new Paragraph();
            return;
        }
        if (tag.equals(HtmlTags.NEWLINE)) {
            if (currentParagraph == null) {
                currentParagraph = new Paragraph();
            }
            currentParagraph.add(factoryProperties.createChunk("\n", cprops));
            return;
        }
        if (tag.equals(HtmlTags.HORIZONTALRULE)) {
            // Attempting to duplicate the behavior seen on Firefox with
            // http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_hr_test
            // where an initial break is only inserted when the preceding element doesn't
            // end with a break, but a trailing break is always inserted.
            boolean addLeadingBreak = true;
            if (currentParagraph == null) {
                currentParagraph = new Paragraph();
                addLeadingBreak = false;
            }
            if (addLeadingBreak) {
                // Not a new paragraph
                int numChunks = currentParagraph.getChunks().size();
                if (numChunks == 0 || ((Chunk) (currentParagraph.getChunks().get(numChunks - 1))).getContent().endsWith("\n"))
                    addLeadingBreak = false;
            }
            String align = (String) h.get("align");
            int hrAlign = Element.ALIGN_CENTER;
            if (align != null) {
                if (align.equalsIgnoreCase("left"))
                    hrAlign = Element.ALIGN_LEFT;
                if (align.equalsIgnoreCase("right"))
                    hrAlign = Element.ALIGN_RIGHT;
            }
            String width = (String) h.get("width");
            float hrWidth = 1;
            if (width != null) {
                float tmpWidth = Markup.parseLength(width, Markup.DEFAULT_FONT_SIZE);
                if (tmpWidth > 0)
                    hrWidth = tmpWidth;
                if (!width.endsWith("%"))
                    // Treat a pixel width as 100% for now.
                    hrWidth = 100;
            }
            String size = (String) h.get("size");
            float hrSize = 1;
            if (size != null) {
                float tmpSize = Markup.parseLength(size, Markup.DEFAULT_FONT_SIZE);
                if (tmpSize > 0)
                    hrSize = tmpSize;
            }
            if (addLeadingBreak)
                currentParagraph.add(Chunk.NEWLINE);
            currentParagraph.add(new LineSeparator(hrSize, hrWidth, null, hrAlign, currentParagraph.getLeading() / 2));
            currentParagraph.add(Chunk.NEWLINE);
            return;
        }
        if (tag.equals(HtmlTags.CHUNK) || tag.equals(HtmlTags.SPAN)) {
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals(HtmlTags.IMAGE)) {
            String src = (String) h.get(ElementTags.SRC);
            if (src == null)
                return;
            cprops.addToChain(tag, h);
            Image img = null;
            if (interfaceProps != null) {
                ImageProvider ip = (ImageProvider) interfaceProps.get("img_provider");
                if (ip != null)
                    img = ip.getImage(src, h, cprops, document);
                if (img == null) {
                    HashMap images = (HashMap) interfaceProps.get("img_static");
                    if (images != null) {
                        Image tim = (Image) images.get(src);
                        if (tim != null)
                            img = Image.getInstance(tim);
                    } else {
                        if (!src.startsWith("http")) {
                            // relative src references only
                            String baseurl = (String) interfaceProps.get("img_baseurl");
                            if (baseurl != null) {
                                src = baseurl + src;
                                img = Image.getInstance(src);
                            }
                        }
                    }
                }
            }
            if (img == null) {
                if (!src.startsWith("http")) {
                    String path = cprops.getProperty("image_path");
                    if (path == null)
                        path = "";
                    src = new File(path, src).getPath();
                }
                img = Image.getInstance(src);
            }
            String align = (String) h.get("align");
            String width = (String) h.get("width");
            String height = (String) h.get("height");
            String before = cprops.getProperty("before");
            String after = cprops.getProperty("after");
            if (before != null)
                img.setSpacingBefore(Float.parseFloat(before));
            if (after != null)
                img.setSpacingAfter(Float.parseFloat(after));
            float actualFontSize = Markup.parseLength(cprops.getProperty(ElementTags.SIZE), Markup.DEFAULT_FONT_SIZE);
            if (actualFontSize <= 0f)
                actualFontSize = Markup.DEFAULT_FONT_SIZE;
            float widthInPoints = Markup.parseLength(width, actualFontSize);
            float heightInPoints = Markup.parseLength(height, actualFontSize);
            if (widthInPoints > 0 && heightInPoints > 0) {
                img.scaleAbsolute(widthInPoints, heightInPoints);
            } else if (widthInPoints > 0) {
                heightInPoints = img.getHeight() * widthInPoints / img.getWidth();
                img.scaleAbsolute(widthInPoints, heightInPoints);
            } else if (heightInPoints > 0) {
                widthInPoints = img.getWidth() * heightInPoints / img.getHeight();
                img.scaleAbsolute(widthInPoints, heightInPoints);
            }
            img.setWidthPercentage(0);
            if (align != null) {
                endElement("p");
                int ralign = Image.MIDDLE;
                if (align.equalsIgnoreCase("left"))
                    ralign = Image.LEFT;
                else if (align.equalsIgnoreCase("right"))
                    ralign = Image.RIGHT;
                img.setAlignment(ralign);
                Img i = null;
                boolean skip = false;
                if (interfaceProps != null) {
                    i = (Img) interfaceProps.get("img_interface");
                    if (i != null)
                        skip = i.process(img, h, cprops, document);
                }
                if (!skip)
                    document.add(img);
                cprops.removeChain(tag);
            } else {
                cprops.removeChain(tag);
                if (currentParagraph == null) {
                    currentParagraph = FactoryProperties.createParagraph(cprops);
                }
                currentParagraph.add(new Chunk(img, 0, 0));
            }
            return;
        }
        endElement("p");
        if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5") || tag.equals("h6")) {
            if (!h.containsKey(ElementTags.SIZE)) {
                int v = 7 - Integer.parseInt(tag.substring(1));
                h.put(ElementTags.SIZE, Integer.toString(v));
            }
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals(HtmlTags.UNORDEREDLIST)) {
            if (pendingLI)
                endElement(HtmlTags.LISTITEM);
            skipText = true;
            cprops.addToChain(tag, h);
            List list = new List(false);
            try {
                list.setIndentationLeft(new Float(cprops.getProperty("indent")).floatValue());
            } catch (Exception e) {
                list.setAutoindent(true);
            }
            list.setListSymbol("\u2022");
            stack.push(list);
            return;
        }
        if (tag.equals(HtmlTags.ORDEREDLIST)) {
            if (pendingLI)
                endElement(HtmlTags.LISTITEM);
            skipText = true;
            cprops.addToChain(tag, h);
            List list = new List(true);
            try {
                list.setIndentationLeft(new Float(cprops.getProperty("indent")).floatValue());
            } catch (Exception e) {
                list.setAutoindent(true);
            }
            stack.push(list);
            return;
        }
        if (tag.equals(HtmlTags.LISTITEM)) {
            if (pendingLI)
                endElement(HtmlTags.LISTITEM);
            Object list = stack.size() > 0 ? stack.peek() : null;
            if ((!(list instanceof List) && !(list instanceof ListItem)) || list == null) {
                startElement(HtmlTags.UNORDEREDLIST, h);
                pendingUL = true;
            }
            skipText = false;
            pendingLI = true;
            cprops.addToChain(tag, h);
            ListItem item = FactoryProperties.createListItem(cprops);
            stack.push(item);
            return;
        }
        if (tag.equals(HtmlTags.DIV) || tag.equals(HtmlTags.BODY) || tag.equals("p")) {
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals(HtmlTags.PRE)) {
            if (!h.containsKey(ElementTags.FACE)) {
                h.put(ElementTags.FACE, "Courier");
            }
            cprops.addToChain(tag, h);
            isPRE = true;
            return;
        }
        if (tag.equals("tr")) {
            if (pendingTR)
                endElement("tr");
            skipText = true;
            pendingTR = true;
            cprops.addToChain("tr", h);
            return;
        }
        if (tag.equals("td") || tag.equals("th")) {
            if (pendingTD)
                endElement(tag);
            skipText = false;
            pendingTD = true;
            cprops.addToChain("td", h);
            stack.push(new IncCell(tag, cprops));
            return;
        }
        if (tag.equals("table")) {
            cprops.addToChain("table", h);
            IncTable table = new IncTable(h);
            stack.push(table);
            tableState.push(new boolean[] { pendingTR, pendingTD });
            pendingTR = pendingTD = false;
            skipText = true;
            return;
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}
Also used : HashMap(java.util.HashMap) Chunk(com.lowagie.text.Chunk) Image(com.lowagie.text.Image) LineSeparator(com.lowagie.text.pdf.draw.LineSeparator) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) Paragraph(com.lowagie.text.Paragraph) ExceptionConverter(com.lowagie.text.ExceptionConverter) ArrayList(java.util.ArrayList) List(com.lowagie.text.List) ListItem(com.lowagie.text.ListItem) File(java.io.File)

Example 9 with LineSeparator

use of com.lowagie.text.pdf.draw.LineSeparator in project qcadoo by qcadoo.

the class PdfHelperImpl method addDocumentHeaderThin.

@Override
public void addDocumentHeaderThin(final Document document, final String name, final String documentTitle, final String documentAuthor, final Date date) throws DocumentException {
    SimpleDateFormat df = new SimpleDateFormat(DateUtils.L_DATE_TIME_FORMAT, getLocale());
    LineSeparator line = new LineSeparator(2, 100f, ColorUtils.getLineDarkColor(), Element.ALIGN_LEFT, 0);
    Paragraph title = new Paragraph(new Phrase(documentTitle, FontUtils.getDejavuBold14Light()));
    title.add(new Phrase(" " + name, FontUtils.getDejavuBold14Dark()));
    title.setSpacingAfter(7f);
    document.add(title);
    document.add(line);
    PdfPTable userAndDate = new PdfPTable(2);
    userAndDate.setWidthPercentage(100f);
    userAndDate.setHorizontalAlignment(Element.ALIGN_LEFT);
    userAndDate.getDefaultCell().setBorderWidth(0);
    Paragraph userParagraph = new Paragraph(new Phrase(documentAuthor, FontUtils.getDejavuRegular9Light()));
    userParagraph.add(new Phrase(" " + getDocumentAuthor(), FontUtils.getDejavuRegular9Dark()));
    Paragraph dateParagraph = new Paragraph(df.format(date), FontUtils.getDejavuRegular9Light());
    userAndDate.addCell(userParagraph);
    userAndDate.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
    userAndDate.addCell(dateParagraph);
    userAndDate.setSpacingAfter(10f);
    document.add(userAndDate);
}
Also used : PdfPTable(com.lowagie.text.pdf.PdfPTable) Phrase(com.lowagie.text.Phrase) SimpleDateFormat(java.text.SimpleDateFormat) LineSeparator(com.lowagie.text.pdf.draw.LineSeparator) Paragraph(com.lowagie.text.Paragraph)

Aggregations

LineSeparator (com.lowagie.text.pdf.draw.LineSeparator)9 Paragraph (com.lowagie.text.Paragraph)7 Chunk (com.lowagie.text.Chunk)4 DocumentException (com.lowagie.text.DocumentException)4 ExceptionConverter (com.lowagie.text.ExceptionConverter)4 Image (com.lowagie.text.Image)4 Phrase (com.lowagie.text.Phrase)4 File (java.io.File)3 Annotation (com.lowagie.text.Annotation)2 BadElementException (com.lowagie.text.BadElementException)2 Element (com.lowagie.text.Element)2 Font (com.lowagie.text.Font)2 ListItem (com.lowagie.text.ListItem)2 Meta (com.lowagie.text.Meta)2 Rectangle (com.lowagie.text.Rectangle)2 Section (com.lowagie.text.Section)2 Table (com.lowagie.text.Table)2 TextElementArray (com.lowagie.text.TextElementArray)2 BaseFont (com.lowagie.text.pdf.BaseFont)2 PdfPTable (com.lowagie.text.pdf.PdfPTable)2