Example usage for com.lowagie.text Chunk setBackground

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

Introduction

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

Prototype

public Chunk setBackground(Color color) 

Source Link

Document

Sets the color of the background Chunk.

Usage

From source file:com.develog.utils.report.engine.export.JRPdfExporter.java

License:Open Source License

/**
 *
 *///from  w ww  .  jav a 2  s  . c  o  m
protected Chunk getChunk(Map attributes, String text) throws JRException, DocumentException, IOException {
    JRFont jrFont = new JRBaseFont(attributes);

    BaseFont baseFont = null;
    Exception initialException = null;

    try {
        baseFont = BaseFont.createFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded(),
                true, null, null);
    } catch (Exception e) {
        initialException = e;
    }

    if (baseFont == null) {
        byte[] bytes = null;

        try {
            bytes = JRLoader.loadBytesFromLocation(jrFont.getPdfFontName());
        } catch (JRException e) {
            throw new JRException("Could not load the following font : " + "\npdfFontName   : "
                    + jrFont.getPdfFontName() + "\npdfEncoding   : " + jrFont.getPdfEncoding()
                    + "\nisPdfEmbedded : " + jrFont.isPdfEmbedded(), initialException);
        }

        baseFont = BaseFont.createFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded(),
                true, bytes, null);
    }

    Color forecolor = (Color) attributes.get(TextAttribute.FOREGROUND);
    Color backcolor = (Color) attributes.get(TextAttribute.BACKGROUND);
    /*
    if (forecolor == null)
    {
       forecolor = Color.black;
    }
    */

    Font font = new Font(baseFont, (float) jrFont.getSize(),
            //((jrFont.isBold())?Font.BOLD:0) +
            //((jrFont.isItalic())?Font.ITALIC:0) +
            (jrFont.isUnderline() ? Font.UNDERLINE : 0) | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0),
            forecolor);

    Chunk chunk = new Chunk(text, font);

    if (backcolor != null) {
        chunk.setBackground(backcolor);
    }

    return chunk;
}

From source file:com.dlya.facturews.DlyaPdfExporter2.java

License:Open Source License

/**
 *
 *///from  ww  w . j  a  v a2 s. c om
protected Chunk getChunk(Map<Attribute, Object> attributes, String text, Locale locale) {
    // underline and strikethrough are set on the chunk below
    Font font = getFont(attributes, locale, false);

    Chunk chunk = new Chunk(text, font);

    if (hasUnderline(attributes)) {
        // using the same values as sun.font.Fond2D
        chunk.setUnderline(null, 0, 1f / 18, 0, -1f / 12, 0);
    }

    if (hasStrikethrough(attributes)) {
        // using the same thickness as sun.font.Fond2D.
        // the position is calculated in Fond2D based on the ascent, defaulting 
        // to iText default position which depends on the font size
        chunk.setUnderline(null, 0, 1f / 18, 0, 1f / 3, 0);
    }

    Color backcolor = (Color) attributes.get(TextAttribute.BACKGROUND);
    if (backcolor != null) {
        chunk.setBackground(backcolor);
    }

    Object script = attributes.get(TextAttribute.SUPERSCRIPT);
    if (script != null) {
        if (TextAttribute.SUPERSCRIPT_SUPER.equals(script)) {
            chunk.setTextRise(font.getCalculatedSize() / 2);
        } else if (TextAttribute.SUPERSCRIPT_SUB.equals(script)) {
            chunk.setTextRise(-font.getCalculatedSize() / 2);
        }
    }

    if (splitCharacter != null) {
        //TODO use line break offsets if available?
        chunk.setSplitCharacter(splitCharacter);
    }

    return chunk;
}

From source file:com.geek.tutorial.itext.text.Chunk_Example.java

License:Open Source License

public Chunk_Example() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("chunk_example.pdf"));
    document.open();/*from  ww  w .  j  av a  2  s.c o m*/

    Font font = new Font(Font.COURIER, 10, Font.BOLD); // 1
    font.setColor(new Color(0x92, 0x90, 0x83));

    Chunk chunk = new Chunk("testing text element", font); // 2

    chunk.setBackground(new Color(0xff, 0xe4, 0x00)); // 3

    document.add(chunk); // 4
    document.close();

}

From source file:com.geek.tutorial.itext.text.Paragraph_Example.java

License:Open Source License

public Paragraph_Example() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("paragraph_example.pdf"));
    document.open();//from www  . java2s.  c om

    Font font = new Font(Font.COURIER, 10, Font.BOLD);
    font.setColor(new Color(0x92, 0x90, 0x83));
    Chunk chunk = new Chunk("testing text element ", font);
    chunk.setBackground(new Color(0xff, 0xe4, 0x00));

    Phrase phrase = new Phrase(20, "This is initial text. ");

    for (int i = 0; i < 10; i++) {
        phrase.add(chunk);
    }

    Paragraph paragraph = new Paragraph(); // 1
    paragraph.add(phrase); // 2

    document.add(paragraph); // 3   

    document.add(paragraph); // 4   

    document.close();
}

From source file:com.geek.tutorial.itext.text.Phrase_Example.java

License:Open Source License

public Phrase_Example() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("phrase_example.pdf"));
    document.open();//from  w w w  .jav  a2  s  .  c o  m

    Font font = new Font(Font.COURIER, 10, Font.BOLD);
    font.setColor(new Color(0x92, 0x90, 0x83));
    Chunk chunk = new Chunk("testing text element ", font);
    chunk.setBackground(new Color(0xff, 0xe4, 0x00));

    Phrase phrase = new Phrase(20, "This is initial text. "); // 1      

    for (int i = 0; i < 10; i++) {
        phrase.add(chunk); // 2
    }

    document.add(phrase); // 3

    document.close();
}

From source file:Driver.RunTestCases.java

License:Open Source License

public void generateReport() {

    File file = new File("Report1.pdf");
    FileOutputStream fileout = null;
    try {//from  ww  w .j av  a  2  s .  c o  m
        fileout = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Document document = new Document();
    try {
        PdfWriter.getInstance(document, fileout);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    document.addAuthor("AGTT");
    document.addTitle("AGTT Report");

    document.open();
    Boolean status = true;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader("Results.txt"));
        String line = null;
        String testCase = null;
        Chunk chunk = null;
        int index = 0;
        List list = new List();
        while ((line = reader.readLine()) != null) {
            if (line.contains("Case")) {

                if (index > 0) {
                    System.out.println(line + status + list.size());
                    if (status == false) {
                        chunk.setBackground(Color.RED);
                    }
                    if (status == true) {
                        chunk.setBackground(Color.GREEN);
                    }
                    document.add(chunk);
                    document.add((Element) list);
                    status = true;
                    list = new List();
                    testCase = null;
                }
                chunk = new Chunk(line + "\n");
                Font font = new Font(Font.TIMES_ROMAN);
                font.setSize(18);
                chunk.setFont(font);
                index++;
            } else {
                if (line.contains("not")) {
                    status = false;
                }
                list.add(line);
            }
            //   document.add(chunk);
        }
    } catch (IOException | DocumentException e) {
        e.printStackTrace();
    }
    document.close();
}

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;//w  ww  .  jav a2  s  .  c o m
            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  ww  w  .j av  a  2  s .  co  m
        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:jdbreport.model.io.pdf.itext2.PdfWriter.java

License:Apache License

protected void writeHTMLText(CellStyle parentStyle, jdbreport.model.Cell cell, PdfPCell pdfCell) {
    if (cell.isNull() || cell.isChild())
        return;//from   w  ww.java  2  s. c  o m

    JTextComponent tc = getHTMLReportRenderer();
    tc.setText(cell.getText());
    List<Content> contentList = Content.getHTMLContentList((HTMLDocument) tc.getDocument());
    if (contentList != null) {
        Phrase phrase = new Phrase();
        for (Content content : contentList) {
            CellStyle newStyle = content.createTextStyle(parentStyle, parentStyle);
            if (newStyle == null) {
                newStyle = parentStyle;
            }
            if (newStyle != null) {
                if (newStyle.getTypeOffset() == CellStyle.SS_SUPER
                        || newStyle.getTypeOffset() == CellStyle.SS_SUB) {
                    newStyle = newStyle.deriveFont((float) newStyle.getSize() / 2);
                }
                int i = textStyles.indexOf(newStyle);
                if (i < 0) {
                    textStyles.add(newStyle);
                    i = textStyles.size() - 1;
                }
                Font font;
                String styleId = "T" + (i + 1);
                if (fonts.containsKey(styleId)) {
                    font = fonts.get(styleId);
                } else {
                    font = getFontMapper().styleToPdf(newStyle);
                    fonts.put(styleId, font);
                }
                Chunk chunk = new Chunk(content.getText(), font);
                chunk.setBackground(newStyle.getBackground());
                if (newStyle.getTypeOffset() == CellStyle.SS_SUPER) {
                    chunk.setTextRise(newStyle.getSize() / 2);
                } else if (newStyle.getTypeOffset() == CellStyle.SS_SUB) {
                    chunk.setTextRise(-newStyle.getSize() / 2);
                }
                phrase.add(chunk);
            } else {
                phrase.add(new Chunk(content.getText()));
            }
        }
        pdfCell.setPhrase(phrase);
    }
}

From source file:net.sf.jasperreports.engine.export.JRPdfExporter.java

License:LGPL

/**
 *
 *//*from  w w w. j a va 2s .  c o  m*/
protected Chunk getChunk(Map attributes, String text) {
    Font font = getFont(attributes);

    Chunk chunk = new Chunk(text, font);

    Color backcolor = (Color) attributes.get(TextAttribute.BACKGROUND);
    if (backcolor != null) {
        chunk.setBackground(backcolor);
    }

    Object script = attributes.get(TextAttribute.SUPERSCRIPT);
    if (script != null) {
        if (TextAttribute.SUPERSCRIPT_SUPER.equals(script)) {
            chunk.setTextRise(font.leading(1f) / 2);
        } else if (script != null && TextAttribute.SUPERSCRIPT_SUB.equals(script)) {
            chunk.setTextRise(-font.leading(1f) / 2);
        }
    }

    if (splitCharacter != null) {
        chunk.setSplitCharacter(splitCharacter);
    }

    return chunk;
}