Example usage for com.lowagie.text Chunk getWidthPoint

List of usage examples for com.lowagie.text Chunk getWidthPoint

Introduction

In this page you can find the example usage for com.lowagie.text Chunk getWidthPoint.

Prototype

public float getWidthPoint() 

Source Link

Document

Gets the width of the Chunk in points.

Usage

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

License:Open Source License

public static float getWidth(final Text htmlText) {
    final TagsManager tm = TagsManager.getInstance();
    final Chunk text = new Chunk(htmlText.getText(), tm.getFont());
    return text.getWidthPoint();
}

From source file:fr.opensagres.odfdom.converter.pdf.internal.stylable.StylableTab.java

License:Open Source License

public void draw(PdfContentByte canvas, float llx, float lly, float urx, float ury, float y) {
    if (font.getBaseFont() != null && font.getSize() > 0.0 && leaderText.trim().length() > 0) {
        // create text to fit tab width
        float width = urx - llx;
        String txt = "";
        while (true) {
            Chunk tmp = new Chunk(txt + leaderText, font);
            if (tmp.getWidthPoint() <= width) {
                txt += leaderText;//from   www  .  ja v  a2 s  .  c om
            } else {
                break;
            }
        }
        // compute x offset - as if tab were right aligned
        float xoffset = width - new Chunk(txt, font).getWidthPoint();
        // compute y offset - use StylableParagraph mechanism
        Chunk tmp = StylableParagraph.createAdjustedChunk(txt, font, lineHeight, lineHeightProportional);
        float yoffset = tmp.getTextRise();
        // draw
        canvas.saveState();
        canvas.beginText();
        canvas.setFontAndSize(font.getBaseFont(), font.getSize());
        canvas.showTextAligned(Element.ALIGN_LEFT, txt, llx + xoffset, y + yoffset, 0.0f);
        canvas.endText();
        canvas.restoreState();
    }
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.FastPdfMapper.java

License:Open Source License

private Chunk createTextChunk(String text, boolean pageNumber, Font currentRunFont,
        UnderlinePatterns currentRunUnderlinePatterns, Color currentRunBackgroundColor) {
    Chunk textChunk = pageNumber ? new ExtendedChunk(pdfDocument, true, currentRunFont)
            : new Chunk(text, currentRunFont);

    if (currentRunUnderlinePatterns != null) {
        // underlined
        boolean singleUnderlined = false;
        switch (currentRunUnderlinePatterns) {
        case SINGLE:
            singleUnderlined = true;/*from  w w  w  .j  a v a  2s.  com*/
            break;

        default:
            break;
        }
        if (singleUnderlined) {
            textChunk.setUnderline(1, -2);
        }
    }

    // background color
    if (currentRunBackgroundColor != null) {
        textChunk.setBackground(Converter.toAwtColor(currentRunBackgroundColor));
    }
    if (currentRunX != null) {
        this.currentRunX += textChunk.getWidthPoint();
    }
    return textChunk;
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.java

License:Open Source License

private Chunk createTextChunk(String text, boolean pageNumber, Font currentRunFont,
        UnderlinePatterns currentRunUnderlinePatterns, Color currentRunBackgroundColor) {
    // Chunk textChunk =
    // pageNumber ? new ExtendedChunk( pdfDocument, true, currentRunFont ) :
    // new Chunk( text, currentRunFont );

    Chunk textChunk = null;
    if (processingTotalPageCountField && expectedPageCount != null) {
        textChunk = new Chunk(String.valueOf(expectedPageCount), currentRunFont);
    } else {//from   w w  w. ja v  a  2 s.com
        textChunk = pageNumber ? new ExtendedChunk(pdfDocument, true, currentRunFont)
                : new Chunk(text, currentRunFont);
    }
    if (currentRunUnderlinePatterns != null) {
        // underlined
        boolean singleUnderlined = false;
        switch (currentRunUnderlinePatterns) {
        case SINGLE:
            singleUnderlined = true;
            break;

        default:
            break;
        }
        if (singleUnderlined) {
            textChunk.setUnderline(1, -2);
        }
    }

    // background color
    if (currentRunBackgroundColor != null) {
        textChunk.setBackground(Converter.toAwtColor(currentRunBackgroundColor));
    }
    if (currentRunX != null) {
        this.currentRunX += textChunk.getWidthPoint();
    }
    return textChunk;
}

From source file:fr.opensagres.poi.xwpf.converter.pdf.internal.PdfMapper.java

License:Open Source License

@Override
protected void visitTabs(CTTabs tabs, IITextContainer pdfParagraphContainer) throws Exception {
    if (currentRunX == null) {
        Paragraph paragraph = null;//w w w  .jav  a 2s  .co  m
        if (pdfParagraphContainer instanceof Paragraph) {
            paragraph = (Paragraph) pdfParagraphContainer;
        } else {
            paragraph = (Paragraph) ((StylableAnchor) pdfParagraphContainer).getITextContainer();
        }
        currentRunX = paragraph.getFirstLineIndent();
        List<Chunk> chunks = paragraph.getChunks();
        for (Chunk chunk : chunks) {
            currentRunX += chunk.getWidthPoint();
        }
    } else {
        if (currentRunX >= pdfDocument.getPageWidth()) {
            currentRunX = 0f;
        }
    }

    Float tabPosition = null;
    STTabTlc.Enum tabLeader = null;
    STTabJc.Enum tabVal = null;
    boolean useDefaultTabStop = false;
    if (tabs != null) {
        List<CTTabStop> tabList = tabs.getTabList();

        CTTabStop tabStop = getTabStop(tabList);
        if (tabStop != null) {

            float lastX = DxaUtil.dxa2points(tabStop.getPos().floatValue());
            if (lastX > currentRunX) {
                tabPosition = lastX;
                tabLeader = tabStop.getLeader();
                tabVal = tabStop.getVal();
            } else {
                useDefaultTabStop = true;
            }
        }
    }
    if (tabs == null || useDefaultTabStop) {
        // default tab
        float defaultTabStop = stylesDocument.getDefaultTabStop();
        float pageWidth = pdfDocument.getPageWidth();
        int nbInterval = (int) (pageWidth / defaultTabStop);
        Float lastX = getTabStopPosition(currentRunX, defaultTabStop, nbInterval);
        if (lastX != null) {
            tabPosition = lastX;
        }
    }

    if (tabPosition != null) {
        currentRunX = tabPosition;
        // tab leader : Specifies the character which shall be used to fill
        // in the space created by a tab
        // which
        // ends
        // at this custom tab stop. This character shall be repeated as
        // required to completely fill the
        // tab spacing generated by the tab character.
        VerticalPositionMark mark = createVerticalPositionMark(tabLeader);
        Chunk pdfTab = null;
        if (STTabJc.RIGHT.equals(tabVal)) {
            pdfTab = new Chunk(mark);
        } else {
            pdfTab = new Chunk(mark, currentRunX);
        }
        pdfParagraphContainer.addElement(pdfTab);
    }
}

From source file:org.posterita.core.CrossTabReportGenerator.java

License:Open Source License

protected void writeDocument(Document document) throws OperationException {
    try {/*from   ww  w .  j ava  2 s  . c  om*/
        int noOfRows = dataSource.size();
        String longestText = "";
        int columnCount = 0;

        Object[] obj = null;

        Object[] header = (Object[]) dataSource.get(0);
        columnCount = header.length;

        PdfPTable table = new PdfPTable(columnCount);
        table.setWidthPercentage(100);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.getDefaultCell().setPaddingBottom(5);
        table.getDefaultCell().setPaddingTop(5);

        //adding the headers
        for (int i = 0; i < columnCount; i++) {
            if (i == 0) {
                longestText = header[i].toString();
                table.addCell(new Paragraph(header[i].toString()));
            } else {
                Image img = getTextAsImage(header[i].toString());
                img.setRotationDegrees(90);
                img.setAlignment(Image.ALIGN_BOTTOM);

                PdfPCell cell = new PdfPCell(img);
                cell.setPadding(4);
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
                //cell.setBackgroundColor(new Color(0, 0, 255));

                table.addCell(cell);
            }
        }

        //adding the data 
        for (int j = 1; j < noOfRows; j++) {
            obj = (Object[]) dataSource.get(j);

            for (int k = 0; k < columnCount; k++) {
                if (k == 0) {
                    String text = obj[0].toString();

                    if (new Chunk(text, HEADER_FONT).getWidthPoint() > new Chunk(longestText, HEADER_FONT)
                            .getWidthPoint()) {
                        longestText = text;
                    }

                    Chunk txtck = new Chunk(obj[0].toString(), HEADER_FONT);
                    PdfPCell cell = new PdfPCell(new Paragraph(txtck));

                    if (j == noOfRows - 1) {
                        cell.setBackgroundColor(new Color(170, 170, 170));
                    }

                    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                    cell.setPaddingBottom(5);
                    cell.setPaddingTop(5);
                    cell.setPaddingLeft(5);

                    table.addCell(cell);
                } else {
                    Chunk txtck = new Chunk(obj[k].toString(), DATA_FONT);

                    if (k == columnCount - 1) {
                        PdfPCell cell = new PdfPCell(new Paragraph(txtck));
                        cell.setBackgroundColor(new Color(170, 170, 170));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setPaddingBottom(5);
                        cell.setPaddingTop(5);

                        table.addCell(cell);
                    } else {
                        PdfPCell cell = new PdfPCell(new Paragraph(txtck));
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        cell.setPaddingBottom(5);
                        cell.setPaddingTop(5);

                        if (j == noOfRows - 1) {
                            cell.setBackgroundColor(new Color(170, 170, 170));
                        }

                        table.addCell(cell);
                    }
                }

            }

        }

        //setting table width
        Chunk dataChk = new Chunk("9999", DATA_FONT);
        Chunk headerChk = new Chunk(longestText, HEADER_FONT);

        float dataChkLength = dataChk.getWidthPoint() + 2 * CELLPADDING;
        float headerChkLength = headerChk.getWidthPoint() + 2 * CELLPADDING;

        float tableWidth = headerChkLength + dataChkLength * columnCount;
        float actualTableWidth = document.getPageSize().getWidth() - 2 * MARGIN;

        float columnWidth = dataChkLength;

        if (tableWidth < actualTableWidth) {
            columnWidth = (actualTableWidth - headerChkLength) / (columnCount - 1);
        }

        float[] widths = new float[columnCount];
        widths[0] = headerChkLength + 2 * CELLPADDING;

        for (int i = 1; i < columnCount; i++) {
            widths[i] = columnWidth;
        }

        table.setWidths(widths);

        //writing the table      
        document.add(table);
    } catch (DocumentException e) {
        throw new OperationException(e);
    }

}

From source file:org.posterita.core.CrossTabReportGenerator.java

License:Open Source License

protected Rectangle getDocumentDimension() {
    Document document = new Document(PAGE_SIZE, MARGIN, MARGIN, MARGIN, MARGIN);

    String longestText = "";
    int columnCount = 0;
    Object[] obj = null;//ww w  .  ja  v  a 2  s . c  o  m

    //Getting column count
    obj = (Object[]) dataSource.get(0);
    columnCount = obj.length;

    //Getting longest text
    Iterator iter = dataSource.iterator();

    while (iter.hasNext()) {
        obj = (Object[]) iter.next();

        String header = obj[0].toString();

        if (header.length() > longestText.length()) {
            longestText = header;
        }
    }

    //setting the table width
    Chunk dataChk = new Chunk("9999", DATA_FONT);
    Chunk headerChk = new Chunk(longestText, HEADER_FONT);

    float dataChkLength = dataChk.getWidthPoint() + 2 * CELLPADDING;
    float headerChkLength = headerChk.getWidthPoint() + 2 * CELLPADDING;

    float tableWidth = headerChkLength + dataChkLength * columnCount;

    float actualTableWidth = document.getPageSize().getWidth() - 2 * MARGIN;
    //float actualTableHeight = document.getPageSize().height() - 2*MARGIN;

    //if the table size is greater than that of the page we should 
    //scale the page

    if (tableWidth > actualTableWidth) {
        float documentWidth = document.getPageSize().getWidth();
        float documentHeight = document.getPageSize().getHeight();

        float newDocumentWidth = tableWidth + 2 * MARGIN;
        float newDocumentHeight = (documentHeight * newDocumentWidth) / documentWidth;

        return new Rectangle(newDocumentWidth, newDocumentHeight);
    }

    return document.getPageSize();
}