clean up pdfbox layout tables
This commit is contained in:
parent
813a781d25
commit
11254626e6
5 changed files with 30 additions and 209 deletions
|
@ -4,4 +4,20 @@ https://github.com/ralfstuckert/pdfbox-layout
|
||||||
|
|
||||||
(MIT License)
|
(MIT License)
|
||||||
|
|
||||||
as of October, 2020
|
as of October, 2020
|
||||||
|
|
||||||
|
and extended by "easytable"
|
||||||
|
|
||||||
|
https://github.com/vandeseer/easytable
|
||||||
|
|
||||||
|
(MIT License)
|
||||||
|
|
||||||
|
as of March, 2021
|
||||||
|
|
||||||
|
and extended by "boxable"
|
||||||
|
|
||||||
|
https://github.com/dhorions/boxable
|
||||||
|
|
||||||
|
(Apache 2.0 License)
|
||||||
|
|
||||||
|
as of March, 2021
|
||||||
|
|
|
@ -211,119 +211,4 @@ public abstract class AbstractCell {
|
||||||
public boolean isVerticallyAligned(VerticalAlignment alignment) {
|
public boolean isVerticallyAligned(VerticalAlignment alignment) {
|
||||||
return getSettings().getVerticalAlignment() == alignment;
|
return getSettings().getVerticalAlignment() == alignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public abstract static class AbstractCellBuilder<C extends AbstractCell, B extends AbstractCell.AbstractCellBuilder<C, B>> {
|
|
||||||
|
|
||||||
protected Settings settings = new Settings();
|
|
||||||
|
|
||||||
// We don't want to expose settings directly!
|
|
||||||
private void settings(Settings settings) {}
|
|
||||||
|
|
||||||
public B borderWidth(final float borderWidth) {
|
|
||||||
settings.setBorderWidthTop(borderWidth);
|
|
||||||
settings.setBorderWidthBottom(borderWidth);
|
|
||||||
settings.setBorderWidthLeft(borderWidth);
|
|
||||||
settings.setBorderWidthRight(borderWidth);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderWidthTop(final float borderWidth) {
|
|
||||||
settings.setBorderWidthTop(borderWidth);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderWidthBottom(final float borderWidth) {
|
|
||||||
settings.setBorderWidthBottom(borderWidth);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderWidthLeft(final float borderWidth) {
|
|
||||||
settings.setBorderWidthLeft(borderWidth);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderWidthRight(final float borderWidth) {
|
|
||||||
settings.setBorderWidthRight(borderWidth);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderStyleTop(final BorderStyleInterface style) {
|
|
||||||
settings.setBorderStyleTop(style);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderStyleBottom(final BorderStyleInterface style) {
|
|
||||||
settings.setBorderStyleBottom(style);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderStyleLeft(final BorderStyleInterface style) {
|
|
||||||
settings.setBorderStyleLeft(style);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderStyleRight(final BorderStyleInterface style) {
|
|
||||||
settings.setBorderStyleRight(style);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderStyle(final BorderStyleInterface style) {
|
|
||||||
return this.borderStyleLeft(style)
|
|
||||||
.borderStyleRight(style)
|
|
||||||
.borderStyleBottom(style)
|
|
||||||
.borderStyleTop(style);
|
|
||||||
}
|
|
||||||
|
|
||||||
public B padding(final float padding) {
|
|
||||||
return this.paddingTop(padding)
|
|
||||||
.paddingBottom(padding)
|
|
||||||
.paddingLeft(padding)
|
|
||||||
.paddingRight(padding);
|
|
||||||
}
|
|
||||||
|
|
||||||
public B paddingTop(final float padding) {
|
|
||||||
settings.setPaddingTop(padding);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B paddingBottom(final float padding) {
|
|
||||||
settings.setPaddingBottom(padding);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B paddingLeft(final float padding) {
|
|
||||||
settings.setPaddingLeft(padding);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B paddingRight(final float padding) {
|
|
||||||
settings.setPaddingRight(padding);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B horizontalAlignment(final HorizontalAlignment alignment) {
|
|
||||||
settings.setHorizontalAlignment(alignment);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B verticalAlignment(final VerticalAlignment alignment) {
|
|
||||||
settings.setVerticalAlignment(alignment);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B backgroundColor(final Color backgroundColor) {
|
|
||||||
settings.setBackgroundColor(backgroundColor);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B borderColor(final Color borderColor) {
|
|
||||||
settings.setBorderColor(borderColor);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B wordBreak(final Boolean wordBreak) {
|
|
||||||
settings.setWordBreak(wordBreak);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,23 +78,4 @@ public abstract class AbstractTextCell extends AbstractCell {
|
||||||
public float getMaxWidth() {
|
public float getMaxWidth() {
|
||||||
return getMaxWidthOfText() - getHorizontalPadding();
|
return getMaxWidthOfText() - getHorizontalPadding();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public abstract static class AbstractTextCellBuilder<C extends AbstractTextCell, B extends AbstractTextCell.AbstractTextCellBuilder<C, B>> extends AbstractCellBuilder<C, B> {
|
|
||||||
|
|
||||||
public B font(final Font font) {
|
|
||||||
settings.setFont(font);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B fontSize(final Integer fontSize) {
|
|
||||||
settings.setFontSize(fontSize);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B textColor(final Color textColor) {
|
|
||||||
settings.setTextColor(textColor);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,59 +76,5 @@ public class ParagraphCell extends AbstractCell {
|
||||||
public org.xbib.graphics.pdfbox.layout.elements.Paragraph getWrappedParagraph() {
|
public org.xbib.graphics.pdfbox.layout.elements.Paragraph getWrappedParagraph() {
|
||||||
return wrappedParagraph;
|
return wrappedParagraph;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public static class ParagraphBuilder {
|
|
||||||
|
|
||||||
private final List<ParagraphProcessable> processables = new LinkedList<>();
|
|
||||||
|
|
||||||
private ParagraphBuilder() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParagraphBuilder append(StyledText styledText) {
|
|
||||||
processables.add(styledText);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParagraphBuilder append(Hyperlink hyperlink) {
|
|
||||||
processables.add(hyperlink);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParagraphBuilder append(Markup markup) {
|
|
||||||
processables.add(markup);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ParagraphBuilder appendNewLine(Font font, float fontSize) {
|
|
||||||
processables.add(new NewLine(font, fontSize));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Paragraph build() {
|
|
||||||
return new Paragraph(processables);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ParagraphBuilder builder() {
|
|
||||||
return new ParagraphBuilder();
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*public abstract static class ParagraphCellBuilder<C extends ParagraphCell, B extends ParagraphCell.ParagraphCellBuilder<C, B>> extends AbstractCellBuilder<C, B> {
|
|
||||||
|
|
||||||
public B font(final Font font) {
|
|
||||||
settings.setFont(font);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B fontSize(final Integer fontSize) {
|
|
||||||
settings.setFontSize(fontSize);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public B textColor(final Color textColor) {
|
|
||||||
settings.setTextColor(textColor);
|
|
||||||
return (B) this;
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ public final class PdfUtil {
|
||||||
* @param fontSize FontSize of String
|
* @param fontSize FontSize of String
|
||||||
* @return Width (in points)
|
* @return Width (in points)
|
||||||
*/
|
*/
|
||||||
public static float getStringWidth(final String text, final Font font, final int fontSize) {
|
public static float getStringWidth(String text, Font font, int fontSize) {
|
||||||
return Arrays.stream(text.split(NEW_LINE_REGEX))
|
return Arrays.stream(text.split(NEW_LINE_REGEX))
|
||||||
.max(Comparator.comparing(String::length))
|
.max(Comparator.comparing(String::length))
|
||||||
.map(x -> {
|
.map(x -> {
|
||||||
|
@ -40,25 +40,20 @@ public final class PdfUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static float getWidthOfStringWithoutNewlines(String text, Font font, int fontSize) throws IOException {
|
private static float getWidthOfStringWithoutNewlines(String text, Font font, int fontSize) throws IOException {
|
||||||
|
List<String> codePointsAsString = text.codePoints()
|
||||||
final List<String> codePointsAsString = text.codePoints()
|
|
||||||
.mapToObj(codePoint -> new String(new int[]{codePoint}, 0, 1))
|
.mapToObj(codePoint -> new String(new int[]{codePoint}, 0, 1))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
List<Float> widths = new ArrayList<>();
|
List<Float> widths = new ArrayList<>();
|
||||||
|
for (String codepoint : codePointsAsString) {
|
||||||
for (final String codepoint : codePointsAsString) {
|
|
||||||
try {
|
try {
|
||||||
widths.add(font.getRegularFont().getStringWidth(codepoint) * fontSize / 1000F);
|
widths.add(font.getRegularFont().getStringWidth(codepoint) * fontSize / 1000F);
|
||||||
} catch (final IllegalArgumentException | IOException e) {
|
} catch (final IllegalArgumentException | IOException e) {
|
||||||
widths.add(font.getRegularFont().getStringWidth("–") * fontSize / 1000F);
|
widths.add(font.getRegularFont().getStringWidth("–") * fontSize / 1000F);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return widths.stream().reduce(0.0f, Float::sum);
|
return widths.stream().reduce(0.0f, Float::sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the height of a font.
|
* Computes the height of a font.
|
||||||
*
|
*
|
||||||
|
@ -66,7 +61,7 @@ public final class PdfUtil {
|
||||||
* @param fontSize FontSize
|
* @param fontSize FontSize
|
||||||
* @return Height of font
|
* @return Height of font
|
||||||
*/
|
*/
|
||||||
public static float getFontHeight(Font font, final int fontSize) {
|
public static float getFontHeight(Font font, int fontSize) {
|
||||||
return font.getRegularFont().getFontDescriptor().getCapHeight() * fontSize / 1000F;
|
return font.getRegularFont().getFontDescriptor().getCapHeight() * fontSize / 1000F;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,21 +74,20 @@ public final class PdfUtil {
|
||||||
* @param maxWidth Maximal width of resulting text-lines
|
* @param maxWidth Maximal width of resulting text-lines
|
||||||
* @return A list of lines, where all are smaller than maxWidth
|
* @return A list of lines, where all are smaller than maxWidth
|
||||||
*/
|
*/
|
||||||
public static List<String> getOptimalTextBreakLines(final String text, final Font font, final int fontSize, final float maxWidth) {
|
public static List<String> getOptimalTextBreakLines(String text, Font font, int fontSize, float maxWidth) {
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
for (final String line : text.split(NEW_LINE_REGEX)) {
|
for (String line : text.split(NEW_LINE_REGEX)) {
|
||||||
if (PdfUtil.doesTextLineFit(line, font, fontSize, maxWidth)) {
|
if (PdfUtil.doesTextLineFit(line, font, fontSize, maxWidth)) {
|
||||||
result.add(line);
|
result.add(line);
|
||||||
} else {
|
} else {
|
||||||
result.addAll(PdfUtil.wrapLine(line, font, fontSize, maxWidth));
|
result.addAll(PdfUtil.wrapLine(line, font, fontSize, maxWidth));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> wrapLine(final String line, final Font font, final int fontSize, final float maxWidth) {
|
private static List<String> wrapLine(String line, Font font, int fontSize, float maxWidth) {
|
||||||
if (PdfUtil.doesTextLineFit(line, font, fontSize, maxWidth)) {
|
if (doesTextLineFit(line, font, fontSize, maxWidth)) {
|
||||||
return Collections.singletonList(line);
|
return Collections.singletonList(line);
|
||||||
}
|
}
|
||||||
List<String> goodLines = new ArrayList<>();
|
List<String> goodLines = new ArrayList<>();
|
||||||
|
@ -106,7 +100,7 @@ public final class PdfUtil {
|
||||||
return goodLines;
|
return goodLines;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<String> splitBySize(final String line, final Font font, final int fontSize, final float maxWidth) {
|
private static List<String> splitBySize(String line, Font font, int fontSize, float maxWidth) {
|
||||||
List<String> returnList = new ArrayList<>();
|
List<String> returnList = new ArrayList<>();
|
||||||
for (int i = line.length() - 1; i > 0; i--) {
|
for (int i = line.length() - 1; i > 0; i--) {
|
||||||
String fittedNewLine = line.substring(0, i) + "-";
|
String fittedNewLine = line.substring(0, i) + "-";
|
||||||
|
@ -120,11 +114,10 @@ public final class PdfUtil {
|
||||||
return returnList;
|
return returnList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String buildALine(Stack<String> words,
|
||||||
private static String buildALine(final Stack<String> words,
|
Font font,
|
||||||
final Font font,
|
int fontSize,
|
||||||
final int fontSize,
|
float maxWidth) {
|
||||||
final float maxWidth) {
|
|
||||||
StringBuilder line = new StringBuilder();
|
StringBuilder line = new StringBuilder();
|
||||||
float width = 0;
|
float width = 0;
|
||||||
while (!words.empty()) {
|
while (!words.empty()) {
|
||||||
|
|
Loading…
Reference in a new issue