phax / ph-pdf-layout

Java library for creating fluid page layouts with Apache PDFBox. Supporting multi-page tables, different page layouts etc.
Apache License 2.0
64 stars 11 forks source link

How to make indent of multiline text #9

Closed RangerMak closed 4 years ago

RangerMak commented 4 years ago

I need to make paragraphs and lists (bulleted and numbered). How to make indentation of multiline text with prefix (bullet symbol or another text)?

phax commented 4 years ago

That is a very good question - I haven't done this myself yet.... If you are willing to brainstorm with me, we could use this thread.

RangerMak commented 4 years ago

For simple indent i'm use function

private PLHBox makeIndentedPLHBox(String prefix, EHorzAlignment prefixHorizAlignment,
                                    String text, EHorzAlignment textHorizAlignment,
                                    FontSpec fontSpec, float indentSize) {

    EHorzAlignment prefHAl = prefixHorizAlignment;
    if (prefHAl == null) {
      prefHAl = EHorzAlignment.DEFAULT;
    }

    EHorzAlignment txtHAL = textHorizAlignment;
    if (txtHAL == null) {
      txtHAL = EHorzAlignment.DEFAULT;
    }

    PLHBox plhBox = new PLHBox();
    plhBox.addColumn(new PLBox(new PLText(prefix, fontSpec)).setHorzAlign(prefHAl), WidthSpec.abs(indentSize));
    plhBox.addColumn(new PLText(text, fontSpec).setHorzAlign(txtHAL), WidthSpec.auto());

    return plhBox;
  }

With it i can make lists.

But i still have problem with text flows around indent box.

phax commented 4 years ago

Okay that looks like a reasonable start. As I have done quite some work on tables (cell splitting, repeating headers etc.) I suggest to use that as the basis. Could we do an API like this:

interface IBulletPointCreator {
  String getBulletPointText (int nIndex);
}

class PLBulletPointList implements IPLElement {
  setLeftWidth (WidthSpec IndentWidth);
  setBulletPointCreator (IBulletPointCreator  bpc);
  void addListItem (IPLElement<?> aElement);
}

That would:

What do you think?

RangerMak commented 4 years ago

I have some doubt about rendering as table. How this elements will copy/paste from result PDF to another editors?

phax commented 4 years ago

That is a very good question I cannot answer. Basically the table also "just" aligned text. There is no such construct in PDF. When copy pasting I think it is important that the "baseline" of the stuff is the same. And if the text is "en-block" inside the PDF it will also help I guess... But we basically need to try it

phax commented 4 years ago

A first version based on a table works. Using the Symbol font with vertical alignment was a bit tricky, but the output look okay to me. Everything should be customizable enough.

phax commented 4 years ago

Part of 5.1.0 release