我使用pdfBox轻松创建一些pdf。但是当我使用特殊的时候我遇到了麻烦 字符如şıoüö等。
示例1:当我使用 PDFont font = PDType1Font.HELVETICA; 时
示例2:当我使用 PDFont font = PDTrueTypeFont.loadTTF(document,... 时)
这是我的代码:
import java.io.IOException;
import org.apache.pdfbox.exceptions.COSVisitorException;
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.PDType1Font;
public class PDFExample {
PDFExample() {} //constructor
public static void main (String [] args) throws IOException, COSVisitorException {
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA;
// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString("ş ı ç o ü ö "); // <---- non us characters
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
document.save( "test.pdf");
document.close();
}
}
我甚至试图嵌入另一种truetype字体,例如Gentium,不幸的是我的文字被渲染了一些渲染失败。
这是一个错误吗?怎么能解决我的问题?