java sring写入pdf,Java:使用PDFBox将国家字符写入PDF

I need to create PDF with Czech national characters, and I'm trying to do it with PDFBox library.

I have copied following code from some tutorials:

public void doIt(String file, String message) throws IOException, COSVisitorException

{

PDDocument doc = null;

try

{

doc = new PDDocument();

PDSimpleFont font = PDType1Font.TIMES_ROMAN;

TextToPDF textToPdf = new TextToPDF();

textToPdf.setFont(font);

textToPdf.setFontSize(12);

doc = textToPdf.createPDFFromText(new StringReader(message));

doc.save(file);

}

finally

{

if( doc != null )

{

doc.close();

}

}

}

Now, I'am calling function doIt:

app.doIt("test.pdf", "Skákal pes přes oves, přes zelenou louku.");

This completely works, but in output PDF I get: "þÿSkákal pes pYes oves, pYes zelenou louku."

I tried to find how to set UTF-8 encoding in PDFBox, but IMHO there is just no solution for this on the internet.

Do you have any ideas, how to get right text in output PDF?

Thank you.

解决方案

I think its PDType1Font.TIMES_ROMAN font which is not supporting your Czech national characters. If you can manage to get the .ttf files for the Czech national characters, then use below to get PDFont as below and use the same:

PDFont font = PDTrueTypeFont.loadTTF( doc, new File( "CheckRepFont.ttf" ) );

Here CheckRepFont.ttf is your font file name as an example. Update it with actual one.

EDIT:

PDStream pdStream = new PDStream(doc);

PDSimpleFont font = PDType1Font.TIMES_ROMAN;

font.setToUnicode(pdStream);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于您的问题,我可以为您提供一些帮助。 您可以使用以下步骤实现向PDF内追加文本: 1. 首先,您需要在项目中加入PDFBox库文件和字体文件。 2. 创建一个PDDocument对象,该对象代表正在编辑的PDF文档。 3. 使用PDPage类在文档中添加一个新的页面。 4. 创建一个PDPageContentStream对象,该对象用于编辑页面内容。 5. 使用setFont()方法设置所需的字体。 6. 使用beginText()方法开始编辑文本,并使用showText()方法添加文本。 7. 使用endText()方法结束编辑。 8. 使用close()方法关闭PDPageContentStream对象。 9. 使用save()方法保存修改。 10. 使用close()方法关闭PDDocument对象。 下面是一个示例代码,可以让您更好地了解如何使用PDFBoxPDF中追加文本: ``` import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class AddTextToPDF { public static void main(String args[]) throws IOException { // Create a document and add a page to it PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); // Initialize a stream to write to the page content PDPageContentStream contentStream = new PDPageContentStream(document, page); // Set the font to Helvetica contentStream.setFont(PDType1Font.HELVETICA, 12); // Begin editing the page content contentStream.beginText(); // Write some text to the page contentStream.showText("Hello, World!"); // End editing the page content contentStream.endText(); // Save the changes and close the content stream contentStream.close(); // Save the document to a file and close it document.save(new File("output.pdf")); document.close(); } } ``` 希望以上内容对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值