java pdfbox2 中文乱码,如何从PDFBox 2.0.2中的PDDocument提取字体

I have seen how to do this in previous versions like below:

But I think the getFonts() method has been removed now. I want to retrieve a map of texts to fonts (Map) in the new version of PDFBox but I have no idea how.

Thanks

Kabeer

解决方案

Do this:

PDDocument doc = PDDocument.load("C:/mydoc3.pdf");

for (int i = 0; i < doc.getNumberOfPages(); ++i)

{

PDPage page = doc.getPage(i);

PDResources res = page.getResources();

for (COSName fontName : res.getFontNames())

{

PDFont font = res.getFont(fontName);

// do stuff with the font

}

}

在使用PDFBox设置字体加粗的Java示例,您可以按照以下步骤进行操作: 1. 导入所需的类和包:首先导入必要的类,包括`File`、`FileInputStream`、`PDFParser`、`PDDocument`、`PDFTextStripper`等。 2. 创建`PdfFile`类:创建一个名为`PdfFile`的类。 3. 创建`getContent`方法:在`PdfFile`类创建一个`getContent`方法,该方法接受一个`File`对象作为参数,并返回一个`String`类型的内容。 4. 读取PDF文档:在`getContent`方法,使用`FileInputStream`和`PDFParser`来读取PDF文档。然后通过`parse()`方法解析文档,并获取`PDDocument`对象。 5. 设置字体:使用`PDFontFactory`类的`createFont()`方法来创建字体对象。您可以选择使用系统已经存在的字体,或者使用自定义字体文件。 6. 设置字体样式:将字体样式设置为加粗,可以使用`PDFont`对象的`setBold()`方法。 7. 提取文本内容:使用`PDFTextStripper`类的`getText()`方法来提取PDF文档的文本内容。 8. 关闭资源:在方法的最后,使用`close()`方法关闭`PDDocument`对象和`FileInputStream`对象。 9. 返回内容:返回提取的文本内容。 下面是基于PDFBoxJava代码示例: ```java import java.io.File; import java.io.FileInputStream; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; public class PdfFile { public String getContent(File file) throws Exception { FileInputStream fis = new FileInputStream(file); PDDocument document = PDDocument.load(fis); PDFTextStripper textStripper = new PDFTextStripper(); // 设置字体样式为加粗 textStripper.getFont().setBold(true); String content = textStripper.getText(document); document.close(); fis.close(); return content; } } ``` 请注意,这只是一个示例代码,您可能需要根据您实际的需求进行适当的修和调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [PDFbox字体.zip](https://download.csdn.net/download/qq_39936548/11300235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [java - 如何使用ITEXTRenderer将HTML转换为PDF时设置新的不同字体 - 堆栈内存溢出](https://blog.csdn.net/weixin_29171087/article/details/117724897)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [java操作office和pdf文件java读取word,excel和pdf文档内容](https://blog.csdn.net/dayou7738/article/details/102318456)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值