我在使用 com.sun.pdfview.PDFPrintPage
API 打印阿拉伯语文档时遇到问题。
阿拉伯字符为空白或 "#"。
这里是代码:
final PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setJobName(docName);
final Book book = new Book();
final PDFPrintPage pages = new PDFPrintPage(curFile);
book.append(pages, pformat, curFile.getNumPages());
pjob.setPageable(book);
if (pjob.printDialog()) {
new PrintThread(pages, pjob).start();
}
有什么方法可以通过调整当前的 api 来打印阿拉伯字符,或者我应该转移到另一个 Api 吗?
谢谢
最佳答案
我不知道这个 API,但我在尝试打印日语汉字字符时使用 Apache PdfBox 时遇到了困难。
我只需选择支持这些字符的字体就解决了这个问题。
您可能想尝试在包 com.sun.pdfview.font 中选择合适的字体或com.sun.pdfview.font.ttf 。
<小时/>编辑:使用 Apache PdfBox 的示例
- 下载 Apache PdfBox 二进制文件并在您的项目中引用它们
- 将 ttf Unicode 字体添加到您的项目资源
- 更改字体和输出文件的常量以匹配您的设置
- 您可能需要增加 IDE 允许的堆空间
- 查看 Apache PdfBox 关于 Arabic writing 的注释
package test;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
public class Main {
// TODO adapt 1st two constants
private static final String FILE_OUTPUT_PATH = "yourPath/yourFile.pdf";
private static final String FONT_PATH = "test/yourUnicodeFont.ttf";
private static final String EXAMPLE = "\u060F";
public static void main(String[] args) {
// if the following line returns garbage characters,
// make sure you set your project (and console output) to UTF-8
System.out.println(EXAMPLE);
PDDocument document = null;
try {
// generates custom document
document = new PDDocument();
// gets the Arial.ttf font you placed in your src/test folder
InputStream fontStream = Main.class.getClassLoader()
.getResourceAsStream(FONT_PATH);
// loads it in the doc
PDFont font = PDTrueTypeFont.loadTTF(document, fontStream);
// closes the stream
fontStream.close();
// initializing a new PDF page
PDPage page = new PDPage();
// initializing a page content stream
PDPageContentStream stream = new PDPageContentStream(document,
page, true, false);
// assigining font to page content stream
stream.setFont(font, 24);
// setting color
stream.setNonStrokingColor(Color.BLACK);
// starts drawing text
stream.beginText();
// draws something in Arabic
stream.drawString(EXAMPLE);
// stops drawing text
stream.endText();
// closes stream
stream.close();
// imports the page into the doc
document.importPage(page);
// creating file
File file = new File(FILE_OUTPUT_PATH);
file.createNewFile();
// creating output stream
OutputStream outputStream = new FileOutputStream(file);
// saving doc content to stream
document.save(outputStream);
// flushing stream
outputStream.flush();
// closing stream
outputStream.close();
}
// oops! something wrong happened, see stack trace
catch (Throwable t) {
t.printStackTrace();
}
finally {
// closing doc
if (document != null) {
try {
document.close();
}
// oops!
catch (Throwable t) {
t.printStackTrace();
}
}
}
}
}
最后,在解析 ttf 时,这个问题给我带来了麻烦(我使用的是 32MB Arial Unicode 版本,我的机器无法处理它)。
我认为您需要对要使用的正确字体进行一些额外的研究。
祝你好运!
关于java - 使用 pdfView api 打印阿拉伯字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16646711/