我正在使用一个名为PDFBox的Java库,试图将文本写入PDF.它适用于英文文本,但当我试图在PDF中写入俄文文本时,字母显得很奇怪.似乎问题在于使用的字体,但我不太确定,所以我希望有人能指导我完成这个.以下是重要的代码行:
PDTrueTypeFont font = PDTrueTypeFont.loadTTF( pdfFile, new File( "fonts/VREMACCI.TTF" ) ); // Windows Russian font imported to write the Russian text.
font.setEncoding( new WinAnsiEncoding() ); // Define the Encoding used in writing.
// Some code here to open the PDF & define a new page.
contentStream.drawString( "??????? ????????????" ); // Write the Russian text.
Run Code Online (Sandbox Code Playgroud)
WinAnsiEncoding源代码是:点击这里
---------------------编辑于2009年11月18日
经过一些调查,我现在确定它是一个编码问题,这可以通过使用名为DictionaryEncoding的有用的PDFBox类定义我自己的编码来解决.
我不知道如何使用它,但这是我迄今为止尝试过的:
COSDictionary cosDic = new COSDictionary();
cosDic.setString( COSName.getPDFName("Ercyrillic"), "0420 " ); // Russian letter.
font.setEncoding( new DictionaryEncoding( cosDic …Run Code Online (Sandbox Code Playgroud) 我尝试使用Apache PDFBox 1.8.6在中创建PDF Java。(请参见下面的代码)
如果我写字符串:Hello! 123 abc äöüß一切正常。
但是,如果我添加一个€符号或等效的\ u20ac,字符串就会搞砸了:þÿ H e l l o ! 1 2 3 a b c ä ö ü ß ¬ ¬ ¦
我认为这与编码有关,因为像OpenOffice这样的程序可以毫无问题地导出带有€或其他Unicode符号的pdf。
那么,我该怎么做才能将Unicode字符串写入PDF?
try {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream stream = new PDPageContentStream(doc, page);
PDFont font = PDType1Font.COURIER;
//font.setFontEncoding(new EncodingManager().getEncoding(COSName.WIN_ANSI_ENCODING));
stream.setFont(font, 14);
stream.beginText();
stream.setNonStrokingColor(Color.BLACK);
stream.moveTextPositionByAmount(20, 750);
String text = "Hello! 123 abc äöüß € \u20ac";
//JOptionPane.showMessageDialog(null, text);
stream.drawString(text); …Run Code Online (Sandbox Code Playgroud) 我正在使用PDFBox 2.0.0-SNAPSHOT在Java中构建PDF.它对于非常基本的字符(例如[a-zA-Z9-0])工作正常,但是我为稍微更高级的字符(例如’(quoteright))收到编码错误.这是我的代码:
PDDocument pdf = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
pdf.addPage(page);
PDPageContentStream contents = new PDPageContentStream(pdf, page);
PDFont font = PDType1Font.HELVETICA;
contents.beginText();
contents.setFont(font, 12);
// ...
String text = "’";
contents.showText(text);
contents.endText();
contents.close();
Run Code Online (Sandbox Code Playgroud)
我得到这个例外:
无法在字体Helvetica中编码U + 2019.Type 1字体仅支持8位代码点
我在PDF规范的 D.1节中查找了非嵌入字体支持的字符,并且应支持此字符.
的确,如果我使用这个技巧,我可以插入正确的字符:
// ...
// String text = "’";
// contents.showText(text);
byte[] commands = "(x) Tj ".getBytes();
commands[1] = (byte)145; // = 221 octal = quoteright in WinAnsi
contents.appendRawCommands(commands);
// …Run Code Online (Sandbox Code Playgroud) 我使用 Jsoup 和OpenHTMLToPDF从 HTML 创建了一个 PDF 。我必须在我的 PDF 中使用不同的字体才能覆盖非拉丁字形(请参阅此处)。如何正确嵌入我的字体?
src/main/resources/test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Font Test</title>
<style>
@font-face {
font-family: 'source-sans';
font-style: normal;
font-weight: 400;
src: url(fonts/SourceSansPro-Regular.ttf);
}
</style>
</head>
<body>
<p style="font-family: 'source-sans',serif">Latin Script</p>
<p style="font-family: 'source-sans',serif">????? ???????? ???????.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
src/main/java/main.java:
import com.openhtmltopdf.extend.FSSupplier;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.w3c.dom.Document;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
public class main {
public static void main(String[] …Run Code Online (Sandbox Code Playgroud) 我在 PDType1Font 中只能看到 4 种带有变体的字体。有什么方法可以使用其他/自定义字体吗?
PDFType1Font 字体
public static final PDType1Font TIMES_ROMAN = new PDType1Font("Times-Roman");
public static final PDType1Font TIMES_BOLD = new PDType1Font("Times-Bold");
public static final PDType1Font TIMES_ITALIC = new PDType1Font("Times-Italic");
public static final PDType1Font TIMES_BOLD_ITALIC = new PDType1Font("Times-BoldItalic");
public static final PDType1Font HELVETICA = new PDType1Font("Helvetica");
public static final PDType1Font HELVETICA_BOLD = new PDType1Font("Helvetica-Bold");
public static final PDType1Font HELVETICA_OBLIQUE = new PDType1Font("Helvetica-Oblique");
public static final PDType1Font HELVETICA_BOLD_OBLIQUE = new PDType1Font("Helvetica-BoldOblique");
public static final PDType1Font COURIER = new PDType1Font("Courier");
public …Run Code Online (Sandbox Code Playgroud) 我正在使用PDFBox和iText从各种语言创建一个简单(只是段落)的pdf文档.就像是 :
pdfBox:
private static void createPdfBoxDocument(File from, File to) {
PDDocument document = null;
try {
document = new TextToPDF().createPDFFromText(new FileReader(from));
document.save(new FileOutputStream(to));
} finally {
if (document != null)
document.close();
}
}
private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDType1Font font = PDType1Font.TIMES_ROMAN;
contentStream.setFont(font, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 400);
contentStream.drawString("š");
contentStream.endText();
contentStream.close();
document.save("test.pdf");
document.close();
}
Run Code Online (Sandbox Code Playgroud)
itext …