【问题标题】:Right Alignment of numerical value in PDF using Apache PDFBox library使用 Apache PDFBox 库在 PDF 中的数值右对齐
【发布时间】:2018-06-24 18:09:26
【问题描述】:

我的 Spring Boot Java 应用程序使用 apache pdf box library {version 2.0.6} 来生成 pdf。我希望十进制值右对齐。这意味着所有小数点应该在同一垂直线上对齐。我还附上了截图。

    stream.beginText();
            stream.newLineAtOffset(xCordinate, yCordinate);
            stream.showText(String.valueOf(item.getQuantity()));
            List<String> resultList = processTextData(TextUtil.isEmpty(item.getDescription()) ? "-" : item.getDescription());
            int y = 0;
            int x = 50;
            int tempYcordinate = yCordinate;
            for (String string : resultList) {
                stream.newLineAtOffset(x, y);
                stream.showText(processStringForPdf(string));
                x = 0;
                y = -8;

            }
            tempYcordinate = tempYcordinate - (8 * resultList.size());
            stream.endText();
            stream.beginText();
            stream.newLineAtOffset(285, yCordinate);
            stream.showText("$" + NumberFormat.getInstance(Locale.US).format(Util.round(item.getUnitPrice())));
            stream.newLineAtOffset(65, 0);
            stream.showText("$" + NumberFormat.getInstance(Locale.US).format(Util.round(item.getExtPrice())));
            stream.endText();
            yCordinate = tempYcordinate;

【问题讨论】:

  • 你尝试了什么,是什么阻止了你? Pdfbox 甚至不包含像屏幕截图中那样自动布局表格的选择,因此您必须已经完成了一些布局,因此知道如何做到这一点。还是您使用了未提及的插件?
  • 对齐方式取决于相邻的属性{列,这里是单价和分机价格}。所以我也想处理这个问题。如果一个数量在小数点前 4 位,另一个在小数点前 3 位。对齐被破坏了。我没有使用任何插件
  • 你听说过PDFont.getStringWidth()吗?

标签: java apache pdf spring-boot pdfbox


【解决方案1】:

要右对齐文本,您需要计算要显示的文本的宽度并将输出位置对齐

(右对齐位置)-(文本宽度)

在下面找到一个显示原理的小sn-p。您需要根据需要修改 sn-p。

import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class RightAlignDemo {

    public static void main(String[] args) throws IOException {
        File file = new File("out.pdf");
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        doc.addPage(page);
        PDPageContentStream stream = new PDPageContentStream(doc, page);

        PDType1Font font = PDType1Font.TIMES_ROMAN;
        int fontSize = 12;

        stream.setFont(font, fontSize);

        double[] values = {0, 0.1, 0.01, 12.12, 123.12, 1234.12, 123456.12};

        int columnOneLeftX = 50;
        int columnTwoRightX = 170;
        int columnThreeOffsetX = 10;

        for (int i = 0; i < values.length; i++) {
            stream.beginText();
            stream.newLineAtOffset(columnOneLeftX, 700 - (i*10));
            // show some left aligned non fixed width text
            stream.showText("value " + values[i]);

            // format the double value with thousands separator and 
            // two decimals
            String text = String.format("%,.2f", values[i]);
            // get the width of the formated value
            float textWidth = getTextWidth(font, fontSize, text);
            // align the position to (right alignment minus text width)
            stream.newLineAtOffset(columnTwoRightX - textWidth, 0);
            stream.showText(text);

            // align the positon back to columnTwoRightX plus offset for
            // column three
            stream.newLineAtOffset(textWidth + columnThreeOffsetX, 0);
            stream.showText("description " + i);
            stream.endText();
        }

        stream.close();
        doc.save(file);
        doc.close();
    }

    private static float getTextWidth(PDType1Font font, int fontSize, 
            String text) throws IOException {
        return (font.getStringWidth(text) / 1000.0f) * fontSize;
    }
}

PDF 输出

【讨论】:

    猜你喜欢
    • 2014-07-23
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 2013-03-22
    • 2021-01-05
    • 2013-11-07
    相关资源
    最近更新 更多