vandeseer / easytable

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

Question: why cant numbers be formatted correctly? #139

Closed todayifeeltired closed 2 years ago

todayifeeltired commented 2 years ago

The result of: DecimalFormat decimalFormat = new DecimalFormat("#,###.00"); String.format("%15s", decimalFormat.format(bigDecimal)) can´t really be displayed. The whitespace-formatting of different numbers like 3,00 / 12,00 / 100,00 get a weird formatting.

Have i overlooked the correct attempt for this ?

vandeseer commented 2 years ago

Hi @dennisgodec,

Can you share a little bit more of your code? And maybe also a screenshot illustrating the issue? If so, I will try to look into the issue (as soon as I find some time for it).

Best, Stefan

PS: Thanks for buying me a beer! 🍻 👍 😄

todayifeeltired commented 2 years ago

Hey @vandeseer,

i setup a quick & dirty main method where my issue should be explained more precise.

public static void main (String[] args) {

    String PATH = "SETUP/YOUR/PATH.pdf";
    DecimalFormat decimalFormat = new DecimalFormat("#,###.00");

    try {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage( page );
        PDFont font = PDType1Font.HELVETICA_BOLD;

        PDPageContentStream contentStream = new PDPageContentStream(document, page);

        contentStream.beginText();
        contentStream.setFont( font, 12 );
        contentStream.endText();

        Table.TableBuilder tableBuilder = Table.builder()
                .addColumnsOfWidth(400);

        BigDecimal three = new BigDecimal(3).setScale(2, BigDecimal.ROUND_UNNECESSARY);
        BigDecimal twelve = new BigDecimal(12).setScale(2, BigDecimal.ROUND_UNNECESSARY);
        BigDecimal hundred = new BigDecimal(100).setScale(2, BigDecimal.ROUND_UNNECESSARY);

        System.out.println(String.format("%15s", decimalFormat.format(three)));
        System.out.println(String.format("%15s", decimalFormat.format(twelve)));
        System.out.println(String.format("%15s", decimalFormat.format(hundred)));

        tableBuilder.addRow(Row.builder()
                .add(TextCell.builder().text(String.format("%15s", decimalFormat.format(three))).build())
                .font(PDType1Font.HELVETICA)
                .height(15f)
                .build());
        tableBuilder.addRow(Row.builder()
                .add(TextCell.builder().text(String.format("%15s", decimalFormat.format(twelve))).build())
                .font(PDType1Font.HELVETICA)
                .height(15f)
                .build());
        tableBuilder.addRow(Row.builder()
                .add(TextCell.builder().text(String.format("%15s", decimalFormat.format(hundred))).build())
                .font(PDType1Font.HELVETICA)
                .height(15f)
                .build());

        TableDrawer.builder()
                .contentStream(contentStream)
                .table(tableBuilder.build())
                .startX(75f)
                .startY(560f)
                .endY(100f)
                .build()
                .draw();

        contentStream.close();

        document.save(PATH);
        document.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The system.out.println produces: image

but on the created PDF it looks like this: image

If it still isn´t precise enough, feel free to ask again :)

PS: Glad you enjoyed it!

vandeseer commented 2 years ago

I had a quick look: I think the issue is that you don't specify .horizontalAlignment(HorizontalAlignment.RIGHT) on the respective cells or rows. If you do so it will right align.

Hope this helps!

todayifeeltired commented 2 years ago

Hey @vandeseer Thank you so much! It did the trick :) I was quite desperate /w trying to get this done.