为什么java不将空格的大小保留为pdf文件中的字符

问题描述 投票:0回答:1

我试图使用java创建一个pdf文件

new_canvas.drawText("To",20,90,pdf_paint);
在两者之间,我想像表格一样将几行在另一行下面对齐。 为此,我在作为输入收到的每个数字之前添加了一些空格,以将每个数字与表中的金额对齐。 但是 java 不会将空格保持与字符相同的大小,这会扭曲对齐。

有没有办法通过在每个金额前面添加空格来将每个金额对齐到列的右侧

谢谢

java android pdf alignment
1个回答
0
投票

这只是一个简单的概念验证程序:

  • (1)生成PDF
  • (2) 对齐原始数据中的空格
  • (3) 使用等宽字体。

以下是一个简单的Java应用Maven项目结构。您可以在您的机器上验证它。确认可行后,转换为Gradle项目结构,应用到你的Android项目中。

下载指定字体。

(您可以选择自己的等宽字体。)

  • (1) https://fonts.google.com/noto/specimen/Noto+Sans+Mono
  • (2) 点击按钮
    Get font
    ,下载
    Noto_Sans_Mono.zip
  • (3) 解压
    Noto_Sans_Mono.zip
  • (4)打开文件夹
    Noto_Sans_Mono/static
    ,请选择您认为合适的字体文件。这里我选择了
    NotoSansMono_Condensed-Light.ttf

准备测试数据

这是我之前用过的测试内容。 (热敏打印机打印发票,javaFX 应用程序中的字符串格式问题

在你的程序中,你可以根据你的需要,逻辑生成类似下面的对齐的文本内容。

             Company Name            
-------------------------------------------
Item                        Qty       Price
----                      -----  ----------
Android                       1       12500
Mac OS                        1       12500
Windows 10 Operating System
                              1       12500
Linux Operating System        1       12500
Windows Server OS             1       12500
___________________________________________
Net Amount                             5000
___________________________________________
Thank you so much!

项目树

PDFBox_Invoice
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── GenerateInvoice.java
        └── resources
            └── NotoSansMono_Condensed-Light.ttf

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>pdfbox-invoice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>pdfbox-invoice</name>
    <description>pdfbox-invoice Project</description>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.24</version>
        </dependency>
    </dependencies>
</project>

生成发票.java

package com.example;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;

import java.io.IOException;
import java.io.InputStream;

public class GenerateInvoice {
    public static void main(String[] args) {
        try (PDDocument document = new PDDocument()) {
            PDPage page = new PDPage();
            document.addPage(page);

            InputStream fontStream = GenerateInvoice.class.getClassLoader().getResourceAsStream("NotoSansMono_Condensed-Light.ttf");
            if (fontStream == null) {
                throw new IOException("Font file not found");
            }
            PDType0Font font = PDType0Font.load(document, fontStream);

            try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
                contentStream.beginText();
                contentStream.setFont(font, 12);
                contentStream.newLineAtOffset(50, 750);

                //Put Your data here 
                contentStream.showText("             Company Name             ");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("-------------------------------------------");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("Item                        Qty       Price");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("----                      -----  ----------");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("Android                       1       12500");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("Mac OS                        1       12500");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("Windows 10 Operating System");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("                              1       12500");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("Linux Operating System        1       12500");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("Windows Server OS             1       12500");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("___________________________________________");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("Net Amount                             5000");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("___________________________________________");
                contentStream.newLineAtOffset(0, -15);
                contentStream.showText("Thank you so much!");
                contentStream.endText();
            }

            document.save("Invoice001.pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

NotoSansMono_Condensed-Light.ttf

复制

NotoSansMono_Condensed-Light.ttf
PDFBox_Invoice/src/main/resources

编译打包

mvn clean package

下载依赖jar

mvn dependency:copy-dependencies -DoutputDirectory=target/libs

运行应用程序

java -cp "target/libs/*:target/pdfbox-invoice-0.0.1-SNAPSHOT.jar" com.example.GenerateInvoice

生成的PDF

在项目根目录中,Invoice001.pdf

Invoice001.png

© www.soinside.com 2019 - 2024. All rights reserved.