vandeseer / easytable

Small table drawing library built upon Apache PDFBox
MIT License
239 stars 91 forks source link

New Feature Request - Underline annotation StyleText #117

Open fgarcia-student opened 3 years ago

fgarcia-student commented 3 years ago

pdfbox-layout supports underline styled text. I would like this feature exposed by easy-table through an AnnotationStyleTextBuilder.

grasshopper7 commented 3 years ago

I have coded something similar for annotations with destination. U can find it here - https://github.com/grasshopper7/cucumber-pdf-report/tree/master/cucumber-pdf-report/src/main/java/tech/grasshopper/pdf/annotation/cell. This is kind of coupled to other code but if u r interested to build upon it, feel free.

jesudornenkrone commented 3 years ago

Perhaps, we could have something like this:

package org.vandeseer.easytable.structure.cell.paragraph;

import lombok.Builder; import lombok.Getter; import lombok.NonNull; import lombok.SneakyThrows; import lombok.experimental.SuperBuilder; import org.apache.pdfbox.pdmodel.font.PDFont; import org.vandeseer.easytable.settings.Settings; import org.vandeseer.easytable.util.PdfUtil; import rst.pdfbox.layout.elements.Paragraph; import rst.pdfbox.layout.text.FontDescriptor; import rst.pdfbox.layout.text.NewLine;

import java.awt.*;

@SuperBuilder(toBuilder = true) @Getter public class StyledText implements ParagraphProcessable {

@NonNull
private String text;

private Float fontSize;

private PDFont font;

private Color color;

@SneakyThrows
@Override
public void process(Paragraph paragraph, Settings settings) {
    final Float actualFontSize = getFontSize() != null ? getFontSize() : settings.getFontSize();
    final PDFont actualFont = getFont() != null ? getFont() : settings.getFont();
    final Color actualColor = getColor() != null ? getColor() : settings.getTextColor();

    // Handle new lines properly ...
    String[] lines = getText().split(PdfUtil.NEW_LINE_REGEX);
    for (int i = 0; i < lines.length; i++) {
        paragraph.add(new rst.pdfbox.layout.text.StyledText(lines[i], actualFontSize, actualFont, actualColor));
        if (i < lines.length - 1) {
            paragraph.add(new NewLine(new FontDescriptor(actualFont, actualFontSize)));
        }
    }

}

}

PS: I am very grateful for this great API. Thank you!