使用 Apache PDFBox 库右对齐 PDF 中的数值

Nan*_*ore 2 java apache pdf pdfbox spring-boot

我的 Spring boot Java 应用程序使用 apache pdf box 库 {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;
Run Code Online (Sandbox Code Playgroud)

PDF 的屏幕截图

Sub*_*mal 7

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

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

下面有一个小片段显示了原理。您需要根据需要修改该代码片段。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

PDF输出

片段输出