我在使用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?
感谢
答案 0 :(得分:2)
我不知道这个API,但是在尝试打印日文汉字字符时,我曾经遇到过Apache PdfBox的困难。
我只是通过选择支持这些字符的字体来解决问题。
您可能想尝试在包com.sun.pdfview.font或com.sun.pdfview.font.ttf中选择合适的字体。
编辑:Apache PdfBox的示例
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版本而我的机器无法处理它)。
我认为您需要对正确使用的字体进行一些额外的研究。
祝你好运!