Search in sources :

Example 1 with StyleSheet

use of com.lowagie.text.html.simpleparser.StyleSheet in project JavaClasses by genexuslabs.

the class PDFReportItext method GxDrawText.

public void GxDrawText(String sTxt, int left, int top, int right, int bottom, int align, int htmlformat, int border, int valign) {
    boolean printRectangle = false;
    if (props.getBooleanGeneralProperty(Const.BACK_FILL_IN_CONTROLS, true))
        printRectangle = true;
    if (printRectangle && (border == 1 || backFill)) {
        GxDrawRect(left, top, right, bottom, border, foreColor.getRed(), foreColor.getGreen(), foreColor.getBlue(), backFill ? 1 : 0, backColor.getRed(), backColor.getGreen(), backColor.getBlue(), 0, 0);
    }
    PdfContentByte cb = writer.getDirectContent();
    sTxt = CommonUtil.rtrim(sTxt);
    Font font = new Font(baseFont, fontSize);
    cb.setFontAndSize(baseFont, fontSize);
    cb.setColorFill(foreColor);
    int arabicOptions = 0;
    float captionHeight = baseFont.getFontDescriptor(baseFont.CAPHEIGHT, fontSize);
    float rectangleWidth = baseFont.getWidthPoint(sTxt, fontSize);
    float lineHeight = baseFont.getFontDescriptor(baseFont.BBOXURY, fontSize) - baseFont.getFontDescriptor(baseFont.BBOXLLY, fontSize);
    float textBlockHeight = (float) convertScale(bottom - top);
    int linesCount = (int) (textBlockHeight / lineHeight);
    int bottomOri = bottom;
    int topOri = top;
    // asignando al bottom el top mas el lineHeight
    if (linesCount >= 2 && !((align & 16) == 16) && htmlformat != 1) {
        if (valign == VerticalAlign.TOP.value())
            bottom = top + (int) reconvertScale(lineHeight);
        else if (valign == VerticalAlign.BOTTOM.value())
            top = bottom - (int) reconvertScale(lineHeight);
    // if valign == middle, no se cambia ni top ni bottom
    }
    float bottomAux = (float) convertScale(bottom) - ((float) convertScale(bottom - top) - captionHeight) / 2;
    // Al bottom de los textos se le resta espacio entre el texto y el borde del textblock,
    // porque en el reporte genexus la x,y de un
    // text es la x,y del cuadro que contiene el texto, y la api de itext espera la x,y del texto en si.
    // Generalmente el cuadro es mas grande que lo que ocupa el texto realmente (depende del tipo de font)
    // captionHeight esta convertido, bottom y top no.
    float topAux = (float) convertScale(top) + ((float) convertScale(bottom - top) - captionHeight) / 2;
    float startHeight = bottomAux - topAux - captionHeight;
    float leftAux = (float) convertScale(left);
    float rightAux = (float) convertScale(right);
    int alignment = align & 3;
    boolean autoResize = (align & 256) == 256;
    if (htmlformat == 1) {
        StyleSheet styles = new StyleSheet();
        Hashtable locations = getFontLocations();
        for (Enumeration e = locations.keys(); e.hasMoreElements(); ) {
            String fontName = (String) e.nextElement();
            String fontPath = (String) locations.get(fontName);
            if (fontPath.equals("")) {
                fontPath = PDFFontDescriptor.getTrueTypeFontLocation(fontName, props);
            }
            if (!fontPath.equals("")) {
                FontFactory.register(fontPath, fontName);
                styles.loadTagStyle("body", "face", fontName);
                if (isEmbeddedFont(fontName)) {
                    styles.loadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
                } else {
                    styles.loadTagStyle("body", "encoding", BaseFont.WINANSI);
                }
            }
        }
        // Bottom y top son los absolutos, sin considerar la altura real a la que se escriben las letras.
        bottomAux = (float) convertScale(bottom);
        topAux = (float) convertScale(top);
        ColumnText Col = new ColumnText(cb);
        int colAlignment = columnAlignment(alignment);
        if (colAlignment != 0)
            Col.setAlignment(colAlignment);
        ColumnText simulationCol = new ColumnText(null);
        float drawingPageHeight = (float) this.pageSize.getTop() - topMargin - bottomMargin;
        // Col.setSimpleColumn(llx, lly, urx, ury);
        Col.setSimpleColumn(leftAux + leftMargin, drawingPageHeight - bottomAux, rightAux + leftMargin, drawingPageHeight - topAux);
        simulationCol.setSimpleColumn(leftAux + leftMargin, drawingPageHeight - bottomAux, rightAux + leftMargin, drawingPageHeight - topAux);
        try {
            ArrayList objects = HTMLWorker.parseToList(new StringReader(sTxt), styles);
            for (int k = 0; k < objects.size(); ++k) {
                if (pageHeightExceeded(bottomAux, drawingPageHeight)) {
                    simulationCol.addElement((Element) objects.get(k));
                    simulationCol.go(true);
                    if (simulationCol.getYLine() < bottomMargin) {
                        GxEndPage();
                        GxStartPage();
                        simulationCol = new ColumnText(null);
                        simulationCol.setSimpleColumn(leftAux + leftMargin, drawingPageHeight - bottomAux, rightAux + leftMargin, drawingPageHeight - topAux);
                        simulationCol.addElement((Element) objects.get(k));
                        Col = new ColumnText(cb);
                        if (colAlignment != 0)
                            Col.setAlignment(colAlignment);
                        Col.setSimpleColumn(leftAux + leftMargin, drawingPageHeight - bottomAux, rightAux + leftMargin, drawingPageHeight - topAux);
                        bottomAux = bottomAux - drawingPageHeight;
                    }
                }
                if (objects.get(k) instanceof Paragraph && colAlignment != 0)
                    ((Paragraph) objects.get(k)).setAlignment(colAlignment);
                Col.addElement((Element) objects.get(k));
                Col.go();
            }
        } catch (Exception de) {
            System.out.println("ERROR printing HTML text " + de.getMessage());
        }
    } else if (barcode != null) {
        if (DEBUG)
            DEBUG_STREAM.println("Barcode: --> " + barcode.getClass().getName());
        try {
            barcode.setCode(sTxt);
            barcode.setTextAlignment(alignment);
            com.lowagie.text.Rectangle rectangle = new com.lowagie.text.Rectangle(0, 0);
            // El rectangulo tiene tamaño ok.
            switch(alignment) {
                case // Center Alignment
                1:
                    rectangle = new com.lowagie.text.Rectangle((leftAux + rightAux) / 2 + leftMargin - rectangleWidth / 2, (float) this.pageSize.getTop() - (float) convertScale(bottom) - topMargin - bottomMargin, (leftAux + rightAux) / 2 + leftMargin + rectangleWidth / 2, (float) this.pageSize.getTop() - (float) convertScale(top) - topMargin - bottomMargin);
                    break;
                case // Right Alignment
                2:
                    rectangle = new com.lowagie.text.Rectangle(rightAux + leftMargin - rectangleWidth, (float) this.pageSize.getTop() - (float) convertScale(bottom) - topMargin - bottomMargin, rightAux + leftMargin, (float) this.pageSize.getTop() - (float) convertScale(top) - topMargin - bottomMargin);
                    break;
                case // Left Alignment
                0:
                    rectangle = new com.lowagie.text.Rectangle(leftAux + leftMargin, (float) this.pageSize.getTop() - (float) convertScale(bottom) - topMargin - bottomMargin, leftAux + leftMargin + rectangleWidth, (float) this.pageSize.getTop() - (float) convertScale(top) - topMargin - bottomMargin);
                    break;
            }
            barcode.setAltText("");
            barcode.setBaseline(0);
            if (fontSize < Const.LARGE_FONT_SIZE)
                barcode.setX(Const.OPTIMAL_MINIMU_BAR_WIDTH_SMALL_FONT);
            else
                barcode.setX(Const.OPTIMAL_MINIMU_BAR_WIDTH_LARGE_FONT);
            Image imageCode = barcode.createImageWithBarcode(cb, backFill ? backColor : null, foreColor);
            imageCode.setAbsolutePosition(leftAux + leftMargin, rectangle.getBottom());
            barcode.setBarHeight(rectangle.getHeight());
            imageCode.scaleToFit(rectangle.getWidth(), rectangle.getHeight());
            document.add(imageCode);
        } catch (Exception ex) {
            if (DEBUG)
                DEBUG_STREAM.println("Error generating Barcode: --> " + barcode.getClass().getName() + ex.getMessage());
            if (DEBUG)
                ex.printStackTrace();
        }
    } else {
        if (backFill) {
            com.lowagie.text.Rectangle rectangle = new com.lowagie.text.Rectangle(0, 0);
            // Si el texto tiene background lo dibujo de esta forma
            switch(alignment) {
                case // Center Alignment
                1:
                    rectangle = new com.lowagie.text.Rectangle((leftAux + rightAux) / 2 + leftMargin - rectangleWidth / 2, (float) this.pageSize.getTop() - bottomAux - topMargin - bottomMargin, (leftAux + rightAux) / 2 + leftMargin + rectangleWidth / 2, (float) this.pageSize.getTop() - topAux - topMargin - bottomMargin);
                    break;
                case // Right Alignment
                2:
                    rectangle = new com.lowagie.text.Rectangle(rightAux + leftMargin - rectangleWidth, (float) this.pageSize.getTop() - bottomAux - topMargin - bottomMargin, rightAux + leftMargin, (float) this.pageSize.getTop() - topAux - topMargin - bottomMargin);
                    break;
                case // Left Alignment
                0:
                    rectangle = new com.lowagie.text.Rectangle(leftAux + leftMargin, (float) this.pageSize.getTop() - bottomAux - topMargin - bottomMargin, leftAux + leftMargin + rectangleWidth, (float) this.pageSize.getTop() - topAux - topMargin - bottomMargin);
                    break;
            }
            rectangle.setBackgroundColor(backColor);
            try {
                document.add(rectangle);
            } catch (DocumentException de) {
                System.err.println(de.getMessage());
            }
        }
        // Separacion entre el texto y la linea del subrayado
        float underlineSeparation = lineHeight / 5;
        int underlineHeight = (int) underlineSeparation + (int) (underlineSeparation / 4);
        com.lowagie.text.Rectangle underline;
        // Si el texto esta subrayado
        if (fontUnderline) {
            underline = new com.lowagie.text.Rectangle(0, 0);
            switch(alignment) {
                case // Center Alignment
                1:
                    underline = new com.lowagie.text.Rectangle((leftAux + rightAux) / 2 + leftMargin - rectangleWidth / 2, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineSeparation, (leftAux + rightAux) / 2 + leftMargin + rectangleWidth / 2, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineHeight);
                    break;
                case // Right Alignment
                2:
                    underline = new com.lowagie.text.Rectangle(rightAux + leftMargin - rectangleWidth, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineSeparation, rightAux + leftMargin, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineHeight);
                    break;
                case // Left Alignment
                0:
                    underline = new com.lowagie.text.Rectangle(leftAux + leftMargin, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineSeparation, leftAux + leftMargin + rectangleWidth, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineHeight);
                    break;
            }
            underline.setBackgroundColor(foreColor);
            try {
                document.add(underline);
            } catch (DocumentException de) {
                System.err.println(de.getMessage());
            }
        }
        // Si el texto esta tachado
        if (fontStrikethru) {
            underline = new com.lowagie.text.Rectangle(0, 0);
            float strikethruSeparation = lineHeight / 2;
            switch(alignment) {
                case // Center Alignment
                1:
                    underline = new com.lowagie.text.Rectangle((leftAux + rightAux) / 2 + leftMargin - rectangleWidth / 2, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineSeparation + strikethruSeparation, (leftAux + rightAux) / 2 + leftMargin + rectangleWidth / 2, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineHeight + strikethruSeparation);
                    break;
                case // Right Alignment
                2:
                    underline = new com.lowagie.text.Rectangle(rightAux + leftMargin - rectangleWidth, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineSeparation + strikethruSeparation, rightAux + leftMargin, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineHeight + strikethruSeparation);
                    break;
                case // Left Alignment
                0:
                    underline = new com.lowagie.text.Rectangle(leftAux + leftMargin, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineSeparation + strikethruSeparation, leftAux + leftMargin + rectangleWidth, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight - underlineHeight + strikethruSeparation);
                    break;
            }
            underline.setBackgroundColor(foreColor);
            try {
                document.add(underline);
            } catch (DocumentException de) {
                System.err.println(de.getMessage());
            }
        }
        if (sTxt.trim().equalsIgnoreCase("{{Pages}}")) {
            // Si el texto es la cantidad de páginas del documento
            if (!templateCreated) {
                template = cb.createTemplate(right - left, bottom - top);
                templateCreated = true;
            }
            cb.addTemplate(template, leftAux + leftMargin, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin);
            templateFont = baseFont;
            templateFontSize = fontSize;
            templateColorFill = foreColor;
            return;
        }
        float textBlockWidth = rightAux - leftAux;
        float TxtWidth = baseFont.getWidthPoint(sTxt, fontSize);
        boolean justified = (alignment == 3) && textBlockWidth < TxtWidth;
        boolean wrap = ((align & 16) == 16);
        // Justified
        if (wrap || justified) {
            // Bottom y top son los absolutos, sin considerar la altura real a la que se escriben las letras.
            bottomAux = (float) convertScale(bottomOri);
            topAux = (float) convertScale(topOri);
            // La constante 2 para LEADING indica la separacion que se deja entre un renglon y otro. (es lo que mas se asemeja a la api vieja).
            float leading = (float) (Double.valueOf(props.getGeneralProperty(Const.LEADING)).doubleValue());
            Paragraph p = new Paragraph(sTxt, font);
            float llx = leftAux + leftMargin;
            float lly = (float) this.pageSize.getTop() - bottomAux - topMargin - bottomMargin;
            float urx = rightAux + leftMargin;
            float ury = (float) this.pageSize.getTop() - topAux - topMargin - bottomMargin;
            try {
                DrawColumnText(cb, llx, lly, urx, ury, p, leading, runDirection, valign, alignment);
            } catch (DocumentException ex) {
                ex.printStackTrace(System.err);
            }
        } else // no wrap
        {
            startHeight = 0;
            if (!autoResize) {
                // Va quitando el ultimo char del texto hasta que llega a un string cuyo ancho se pasa solo por un caracter
                // del ancho del textblock ("se pasa solo por un caracter": esto es porque en el caso general es ese el texto que
                // mas se parece a lo que se disenia en genexus).
                String newsTxt = sTxt;
                while (TxtWidth > textBlockWidth && (newsTxt.length() - 1 >= 0)) {
                    sTxt = newsTxt;
                    newsTxt = newsTxt.substring(0, newsTxt.length() - 1);
                    TxtWidth = baseFont.getWidthPoint(newsTxt, fontSize);
                }
            }
            Phrase phrase = new Phrase(sTxt, font);
            switch(alignment) {
                case // Center Alignment
                1:
                    ColumnText.showTextAligned(cb, cb.ALIGN_CENTER, phrase, ((leftAux + rightAux) / 2) + leftMargin, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight, 0, runDirection, arabicOptions);
                    break;
                case // Right Alignment
                2:
                    ColumnText.showTextAligned(cb, cb.ALIGN_RIGHT, phrase, rightAux + leftMargin, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight, 0, runDirection, arabicOptions);
                    break;
                // Left Alignment
                case 0:
                case // Justified, only one text line
                3:
                    ColumnText.showTextAligned(cb, cb.ALIGN_LEFT, phrase, leftAux + leftMargin, this.pageSize.getTop() - bottomAux - topMargin - bottomMargin + startHeight, 0, runDirection, arabicOptions);
                    break;
            }
        }
    }
}
Also used : Rectangle(com.lowagie.text.Rectangle) Phrase(com.lowagie.text.Phrase) Image(com.lowagie.text.Image) BaseFont(com.lowagie.text.pdf.BaseFont) PDFFont(com.genexus.reports.fonts.PDFFont) Font(com.lowagie.text.Font) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) Paragraph(com.lowagie.text.Paragraph) ColumnText(com.lowagie.text.pdf.ColumnText) StyleSheet(com.lowagie.text.html.simpleparser.StyleSheet) Rectangle(com.lowagie.text.Rectangle) DocumentException(com.lowagie.text.DocumentException) StringReader(java.io.StringReader) PdfContentByte(com.lowagie.text.pdf.PdfContentByte) HttpContext(com.genexus.internet.HttpContext) ModelContext(com.genexus.ModelContext)

Example 2 with StyleSheet

use of com.lowagie.text.html.simpleparser.StyleSheet in project OpenPDF by LibrePDF.

the class ParseNestedHtmlList method main.

/**
 * Generates an HTML page with the text 'Hello World'
 *
 * @param args no arguments needed here
 */
public static void main(String[] args) {
    System.out.println("Parse Nested HTML List");
    try {
        final String htmlText = "<html>" + "<body>" + "<p>What should you say?</p>" + "<ul>" + "  <li>Hello</li>" + "  <li>World</li>" + "</ul>" + "<ol>" + "  <li>Element-1" + "    <ol>" + "      <li>Element-1-1</li>" + "      <li>Element-1-2</li>" + "    </ol>" + "  </li>" + "  <li>Element-2" + "    <ol>" + "      <li>Element-2-1" + "        <ol>" + "          <li>Element-2-1-1" + "            <ol>" + "              <li>Element-2-1-1-1</li>" + "              <li>Element-2-1-1-2</li>" + "            </ol>" + "          </li>" + "          <li>Element-2-1-2" + "            <ol>" + "              <li>Element-2-1-2-1</li>" + "              <li>Element-2-1-2-2</li>" + "            </ol>" + "          </li>" + "        </ol>" + "      </li>" + "      <li>Element-2-2</li>" + "    </ol>" + "  </li>" + "</ol>" + "</body>" + "</html>";
        final StringReader reader = new StringReader(htmlText);
        final StyleSheet styleSheet = new StyleSheet();
        final Map<String, Object> interfaceProps = new HashMap<>();
        final List<Element> elements = HTMLWorker.parseToList(reader, styleSheet, interfaceProps);
        printElement("", elements);
    } catch (DocumentException | IOException de) {
        System.err.println(de.getMessage());
    }
}
Also used : StyleSheet(com.lowagie.text.html.simpleparser.StyleSheet) HashMap(java.util.HashMap) Element(com.lowagie.text.Element) DocumentException(com.lowagie.text.DocumentException) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 3 with StyleSheet

use of com.lowagie.text.html.simpleparser.StyleSheet in project gdmatrix by gdmatrix.

the class P7MTransformer method addSignatureInfo.

private void addSignatureInfo(com.lowagie.text.Document pdfDoc, String signatureInfo) throws Exception {
    pdfDoc.newPage();
    StyleSheet sh = new StyleSheet();
    List<Element> objects = HTMLWorker.parseToList(new StringReader(signatureInfo), sh);
    // top margin
    Paragraph par = new Paragraph();
    par.add(new Paragraph());
    par.add(new Paragraph());
    for (Object object : objects) {
        Element e = (Element) object;
        par.add(e);
    }
    pdfDoc.add(par);
}
Also used : StyleSheet(com.lowagie.text.html.simpleparser.StyleSheet) Element(com.lowagie.text.Element) StringReader(java.io.StringReader) PdfObject(com.lowagie.text.pdf.PdfObject) Paragraph(com.lowagie.text.Paragraph)

Aggregations

StyleSheet (com.lowagie.text.html.simpleparser.StyleSheet)3 StringReader (java.io.StringReader)3 DocumentException (com.lowagie.text.DocumentException)2 Element (com.lowagie.text.Element)2 Paragraph (com.lowagie.text.Paragraph)2 IOException (java.io.IOException)2 ModelContext (com.genexus.ModelContext)1 HttpContext (com.genexus.internet.HttpContext)1 PDFFont (com.genexus.reports.fonts.PDFFont)1 Font (com.lowagie.text.Font)1 Image (com.lowagie.text.Image)1 Phrase (com.lowagie.text.Phrase)1 Rectangle (com.lowagie.text.Rectangle)1 BaseFont (com.lowagie.text.pdf.BaseFont)1 ColumnText (com.lowagie.text.pdf.ColumnText)1 PdfContentByte (com.lowagie.text.pdf.PdfContentByte)1 PdfObject (com.lowagie.text.pdf.PdfObject)1 HashMap (java.util.HashMap)1