- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试运行 Stefano Chizzolini(很棒的人:PDFClown 的创建者)编写的 Java 代码,以使用 PDF Clown 库解析 PDF。我收到此错误,但我不知道该如何解决。
Exception in thread "main" org.pdfclown.util.parsers.ParseException: 'name' table does NOT exist.
at org.pdfclown.documents.contents.fonts.OpenFontParser.getName(OpenFontParser.java:570)
at org.pdfclown.documents.contents.fonts.OpenFontParser.load(OpenFontParser.java:221)
at org.pdfclown.documents.contents.fonts.OpenFontParser.<init>(OpenFontParser.java:205)
at org.pdfclown.documents.contents.fonts.TrueTypeFont.loadEncoding(TrueTypeFont.java:91)
at org.pdfclown.documents.contents.fonts.SimpleFont.onLoad(SimpleFont.java:118)
at org.pdfclown.documents.contents.fonts.Font.load(Font.java:738)
at org.pdfclown.documents.contents.fonts.Font.<init>(Font.java:351)
at org.pdfclown.documents.contents.fonts.SimpleFont.<init>(SimpleFont.java:62)
at org.pdfclown.documents.contents.fonts.TrueTypeFont.<init>(TrueTypeFont.java:68)
at org.pdfclown.documents.contents.fonts.Font.wrap(Font.java:253)
at org.pdfclown.documents.contents.FontResources.wrap(FontResources.java:72)
at org.pdfclown.documents.contents.FontResources.wrap(FontResources.java:1)
at org.pdfclown.documents.contents.ResourceItems.get(ResourceItems.java:119)
at org.pdfclown.documents.contents.objects.SetFont.getResource(SetFont.java:119)
at org.pdfclown.documents.contents.objects.SetFont.getFont(SetFont.java:83)
at org.pdfclown.documents.contents.objects.SetFont.scan(SetFont.java:97)
at org.pdfclown.documents.contents.ContentScanner.moveNext(ContentScanner.java:1330)
at org.pdfclown.tools.TextExtractor.extract(TextExtractor.java:626)
at org.pdfclown.tools.TextExtractor.extract(TextExtractor.java:296)
at PDFReader.FullExtract.run(FullExtract.java:71)
at PDFReader.FullExtract.main(FullExtract.java:142)
我知道库包中的类 OpenFontParser 会引发此错误。我能做些什么来解决这个问题吗?
此代码适用于大多数 PDF。我有一个无法解析的 PDF。我猜这是因为 this symbol在 pdf 下方。
public class PDFReader extends Sample {
@Override
public void run()
{
String filePath = new String("C:\\Users\\XYZ\\Desktop\\SomeSamplePDF.pdf");
// 1. Open the PDF file!
File file;
try
{file = new File(filePath);}
catch(Exception e)
{throw new RuntimeException(filePath + " file access error.",e);}
// 2. Get the PDF document!
Document document = file.getDocument();
// 3. Extracting text from the document pages...
for(Page page : document.getPages())
{
extract(new ContentScanner(page)); // Wraps the page contents into a scanner.
}
close(file);
}
private void close(File file) {
// TODO Auto-generated method stub
}
/**
Scans a content level looking for text.
*/
/*
NOTE: Page contents are represented by a sequence of content objects,
possibly nested into multiple levels.
*/
private void extract(
ContentScanner level
)
{
if(level == null)
return;
while(level.moveNext())
{
ContentObject content = level.getCurrent();
if(content instanceof ShowText)
{
Font font = level.getState().getFont();
// Extract the current text chunk, decoding it!
System.out.println(font.decode(((ShowText)content).getText()));
}
else if(content instanceof Text
|| content instanceof ContainerObject)
{
// Scan the inner level!
extract(level.getChildLevel());
}
}
}
private boolean prompt(Page page)
{
int pageIndex = page.getIndex();
if(pageIndex > 0)
{
Map<String,String> options = new HashMap<String,String>();
options.put("", "Scan next page");
options.put("Q", "End scanning");
if(!promptChoice(options).equals(""))
return false;
}
System.out.println("\nScanning page " + (pageIndex+1) + "...\n");
return true;
}
public static void main(String args[])
{
new PDFReader().run();
}
}
最佳答案
如堆栈跟踪所示,问题在于 PDF 中嵌入的某些 TrueType 字体不包含 name
表,即使它是必需的表:
org.pdfclown.util.parsers.ParseException: 'name' table does NOT exist.
...
at org.pdfclown.documents.contents.fonts.TrueTypeFont.loadEncoding(TrueTypeFont.java:91)
因此,严格来说,嵌入的字体是无效的,因此嵌入的 PDF 也是如此。由于这个有效性问题,PDFClown 遇到了异常。
A TrueType font file consists of a sequence of concatenated tables. ...
The first of the tables is the font directory, a special table that facilitates access to the other tables in the font. The directory is followed by a sequence of tables containing the font data. These tables can appear in any order. Certain tables are required for all fonts. Others are optional depending upon the functionality expected of a particular font.
Tables that are required must appear in any valid TrueType font file. The required tables and their tag names are shown in Table 2.
Table 2: The required tables
Tag Table
'cmap' character to glyph mapping
'glyf' glyph data
'head' font header
'hhea' horizontal header
'hmtx' horizontal metrics
'loca' index to location
'maxp' maximum profile
'name' naming
'post' PostScript(Section TrueType Font files: an overview in chapter 6 The TrueType Font File in the TrueType Reference Manual)
另一方面,虽然有许多 PDF 生成器将嵌入式 TrueType 字体减少到 PDF 查看器(最重要的 Adobe Reader)所需的基本要素,并且 name
表似乎没有严格要求。
此外,表 name
在 PDFClown 中仅用于一个目的,即确定相关字体的名称,即使字体名称可以从 BaseFont 相关字体词典的条目也是如此。实际上后者是 PDF 规范必需的,而 name
表中的 PostScript 字体名称 是可选的 根据 TTF 手册。
因此,使用 PDF 字体字典中的 BaseFont 条目将是此 name
表访问的更好替代方案。
Is there anything I can do to fix this?
您可以通过向有问题的嵌入式 TTF 添加 name
表来修复不完全有效的 PDF,或者您可以修补 PDFClown 以忽略丢失的缺失表:在类 org.pdfclown.documents.contents.fonts.OpenFontParser
中编辑方法 getName
:
private String getName(
int id
) throws EOFException, UnsupportedEncodingException
{
// Naming Table ('name' table).
Integer tableOffset = tableOffsets.get("name");
if(tableOffset == null)
throw new ParseException("'name' table does NOT exist.");
将 throw new ParseException("'name' table does NOT exist.")
替换为 return null
。
虽然可以仅使用 OP 提供的信息来分析问题,但 sample file由 @akarshad 提供在他现已删除的答案中,他更有动力开始分析。
关于java - 线程 "main"org.pdfclown.util.parsers.ParseException : 'name' table does NOT exist 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23544547/
我想解析一些文本,其中某些字段在大多数情况下都具有结构,但偶尔(由于特殊大小写、拼写错误等)该结构会丢失。 例如常规情况是 Cost: 5,但偶尔会显示 Cost: 5m 或 Cost: 3 + 1
以下有什么区别: parser.setFeature("http://xml.org/sax/features/validation",true); and parser.setFeature("ht
我尝试在 Windows 8.1 上的 Git Bash 客户端中执行 npm install,但收到以下错误: npm WARN optional SKIPPING OPTIONAL DEPENDE
试图理解 evancz/url-parser 模块时,我偶然发现了这种我难以理解的类型声明:( source ) type Parser a b = Parser (State a -> List
我长期使用下面的 TypeScript 和 Vue 预设。它有效,但我还没有理解每个选项,现在要理解它。第一:parser之间有什么区别?和 @typescript-eslint/parser ? p
我正在尝试使用node-sql-parser在nodejs中解析sql查询。 const {Parser} = require('node-sql-parser'); const parser = n
自定义指令中的 ngModelCtrl.$parsers.unshift 和 ngModelCtrl.$parsers.push 之间的确切区别是什么。 当发生对模型生效但对表单本身无效的事情时,我想
我正在寻找 SAX 和 Pull Parser 之间的主要区别。我知道 SAX 解析器非常适合处理大型 XML 文件,因为它不存储 XML 并且只在一个方向上遍历。与 DOM 相比。但我无法找到 SA
我已经按照存档中的说明成功(?)安装了 QJson 库。但是编译器给我这个错误: Undefined reference to QJSon::Parser::Parser(). 我找到了安装库文件的位
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
尝试实现 Cucumber 来进行一些自动化测试。当我使用 junit 运行测试时,出现以下错误 项目路径: src/test/java/ cucumberJava -cucumberjava,ja
我已经阅读了我能找到的所有文档,但是我找不到关于这两个中间件的作用的简单解释。 body-parser 中的body 指的是什么?为什么需要解析正文? Cookie 也类似。我是否更正了 cookie
我在gradle项目中使用Xerces jar,然后在gradle插件中使用该项目的jar:实际上,我正在开发一个gradle插件,其中包含使用gradle项目jar的某些功能的自定义任务;当我尝试运
我正在尝试连接到 Linux 上的 FTP 服务器,当我执行 ftp.listFiles(remote); 时遇到异常 SYST 215 Linux Exception in thread "AW
我有react-app的示例安装,我得到了以下内容 Error: Failed to load parser '@typescript-eslint/parser' declared in '.esl
我在 there 中提到过类似的问题.我正在使用 mvn clean compile site 命令,我的版本是: cobertura.version: 2.5.1 findbugs.version:
我正在 Eclipse 中开发 GWT 应用程序并使用 jdom2 读取一些自定义 xml 属性文件。 在最近的更新之后,我的应用程序现在在尝试读取 xml 文件时失败并出现上述错误。相关堆栈跟踪是:
我正在使用 spring+maven。我正在 tomcat 服务器中部署我的应用程序。当我尝试运行我的应用程序时,突然出现以下错误。 INFO: Starting Servlet Engine: Ap
我在玩dateutil module在 Python 2.7.3 中。我只是想使用: import dateutil dateutil.parser.parse("01-02-2013") 但我得到了
一.入参解析库 argparse 有时候写Python脚本,需要处理入参[-h][-v][-F]...等情况,如果自己来解析的话,会花费很多时间,而且也容易出问题,好在Python有现成的li
我是一名优秀的程序员,十分优秀!