我试图使用java创建一个pdf文件
new_canvas.drawText("To",20,90,pdf_paint);
在两者之间,我想像表格一样将几行在另一行下面对齐。
为此,我在作为输入收到的每个数字之前添加了一些空格,以将每个数字与表中的金额对齐。
但是 java 不会将空格保持与字符相同的大小,这会扭曲对齐。
有没有办法通过在每个金额前面添加空格来将每个金额对齐到列的右侧
谢谢
这只是一个简单的概念验证程序:
以下是一个简单的Java应用Maven项目结构。您可以在您的机器上验证它。确认可行后,转换为Gradle项目结构,应用到你的Android项目中。
(您可以选择自己的等宽字体。)
Get font
,下载Noto_Sans_Mono.zip
Noto_Sans_Mono.zip
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
<?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>
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
到PDFBox_Invoice/src/main/resources
mvn clean package
mvn dependency:copy-dependencies -DoutputDirectory=target/libs
java -cp "target/libs/*:target/pdfbox-invoice-0.0.1-SNAPSHOT.jar" com.example.GenerateInvoice
在项目根目录中,Invoice001.pdf