Search in sources :

Example 11 with Chunk

use of com.itextpdf.text.Chunk in project ASCIIGenome by dariober.

the class Pdf method ansiFileToPdfParagraphs.

/**
 * Read ansi formatted file line by lone and convert each line to
 * a iText paragraph.
 * @throws IOException
 * @throws InvalidColourException
 */
private List<Paragraph> ansiFileToPdfParagraphs(float fontSize) throws IOException, InvalidColourException {
    List<Paragraph> paraList = new ArrayList<Paragraph>();
    // Files.readAllBytes(Paths.get(this.ansiInput.toURI()));
    byte[] encoded = this.ansiInput.getBytes();
    String str = new String(encoded);
    // Each string in this list is a sequence of a characters sharing the same formatting.
    List<String> ansiList = Splitter.on("\033").splitToList(str);
    Paragraph pdfLine = new Paragraph();
    int nChars = 0;
    int currentMax = 0;
    for (String xv : ansiList) {
        // if(xv.equals("[48;5;231m")){
        // continue;
        // }
        BaseColor fgBaseCol = new BaseColor(this.xterm256ToColor(xv, false).getRGB());
        BaseColor bgBaseCol = new BaseColor(this.xterm256ToColor(xv, true).getRGB());
        if (this.extractAnsiCodes(xv).size() != 0) {
            // This string begins with ansi sequence and the color has been extracted.
            // So remove the ansi sequence at the beginnig and the end, we don't need them anymore
            xv = xv.replaceAll("^.+?m", "");
        }
        for (int i = 0; i < xv.length(); i++) {
            char c = xv.charAt(i);
            nChars += 1;
            Chunk chunk = new Chunk(c, new Font(Font.FontFamily.COURIER, fontSize, Font.NORMAL, fgBaseCol));
            chunk.setBackground(bgBaseCol);
            pdfLine.add(chunk);
            if (c == '\n') {
                // Newline found: Start a new parapgrah
                paraList.add(pdfLine);
                if (nChars > currentMax) {
                    currentMax = nChars;
                }
                nChars = 0;
                pdfLine = new Paragraph();
            }
        }
    }
    Font f = new Font(Font.FontFamily.COURIER, fontSize);
    Chunk chunk = new Chunk("X", f);
    this.setMaxWidth((int) ((currentMax) * chunk.getWidthPoint()));
    for (Paragraph p : paraList) {
        p.setSpacingBefore(-3);
        p.setSpacingAfter(-3);
    }
    this.setMaxHeight((int) Math.rint((paraList.size() + 1) * fontSize));
    return paraList;
}
Also used : BaseColor(com.itextpdf.text.BaseColor) ArrayList(java.util.ArrayList) Chunk(com.itextpdf.text.Chunk) Font(com.itextpdf.text.Font) Paragraph(com.itextpdf.text.Paragraph)

Aggregations

Chunk (com.itextpdf.text.Chunk)11 Paragraph (com.itextpdf.text.Paragraph)6 Phrase (com.itextpdf.text.Phrase)6 ArrayList (java.util.ArrayList)5 Font (com.itextpdf.text.Font)4 BaseColor (com.itextpdf.text.BaseColor)3 List (com.itextpdf.text.List)3 ListItem (com.itextpdf.text.ListItem)3 PdfPCell (com.itextpdf.text.pdf.PdfPCell)3 PdfPTable (com.itextpdf.text.pdf.PdfPTable)3 Document (com.itextpdf.text.Document)2 BaseFont (com.itextpdf.text.pdf.BaseFont)2 ObjetivoDisciplinaDTO (com.tomasio.projects.trainning.dto.ObjetivoDisciplinaDTO)2 SubunidadeDidaticaDTO (com.tomasio.projects.trainning.dto.SubunidadeDidaticaDTO)2 TextStyleTag (eu.transkribus.core.model.beans.customtags.TextStyleTag)2 Point (java.awt.Point)2 ScriptCoverageStatistics (com.github.timurstrekalov.saga.core.model.ScriptCoverageStatistics)1 Chapter (com.itextpdf.text.Chapter)1 ObjetivoOperacionalizadoDTO (com.tomasio.projects.trainning.dto.ObjetivoOperacionalizadoDTO)1 ObjetivoUnidadeDidaticaDTO (com.tomasio.projects.trainning.dto.ObjetivoUnidadeDidaticaDTO)1