2014-10-28 1195 views
0

使用iTextSharp的,我有以下的代码,成功地翻出了PDF文本为广大PDF的我想读的......PdfTextExtractor.GetTextFromPage没有返回正确的文本

PdfReader reader = new PdfReader(fileName); 
for (int i = 1; i <= reader.NumberOfPages; i++) 
{ 
    text += PdfTextExtractor.GetTextFromPage(reader, i); 
} 
reader.Close(); 

然而,我的一些PDF格式的有XFA表单(已经被填写),这将导致“文本”字段来填充下面的垃圾......

"Please wait... \n \nIf this message is not eventually replaced by the proper contents of the document, your PDF \nviewer may not be able to display this type of document. \n \nYou can upgrade to the latest version of Adobe Reader for Windows®, Mac, or Linux® by \nvisiting http://www.adobe.com/products/acrobat/readstep2.html. \n \nFor more assistance with Adobe Reader visit http://www.adobe.com/support/products/\nacrreader.html. \n \nWindows is either a registered trademark or a trademark of Microsoft Corporation in the United States and/or other countries. Mac is a trademark \nof Apple Inc., registered in the United States and other countries. Linux is the registered trademark of Linus Torvalds in the U.S. and other \ncountries." 

我如何解决此问题?我尝试使用iTextSharp的PdfStamper [1]来压扁PDF,但这不起作用 - 生成的流具有相同的垃圾文本。

[1] How to flatten already filled out PDF form using iTextSharp

回答

1

您面临着充当XML流的容器的PDF。此XML流基于XML Forms Architecture(XFA)。你看到的消息是不是垃圾!这是在浏览器中打开文档时显示的PDF页面中包含的消息,该文档就像普通PDF一样读取文件。

例如:如果你在苹果预览打开文档,你会看到完全一样的消息,因为苹果预览不能渲染XFA表单。使用iText解析文件中包含的PDF时,您收到此消息时不应该感到惊讶。这正是您的文件中存在的PDF内容。在Adobe Reader中打开文档时看到的内容不是以PDF语法存储的,而是以XML流形式存储的。

你说你已经尝试在答案中描述的问题How to flatten already filled out PDF form using iTextSharp拼合PDF。 但是,这个问题是关于基于AcroForm技术的表单扁平化。它不应该与XFA表单一起使用。如果你想变平的XFA表单,你需要在iText的顶部使用XFA Worker

[JAVA]

Document document = new Document(); 
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); 
XFAFlattener xfaf = new XFAFlattener(document, writer); 
xfaf.flatten(new PdfReader(baos.toByteArray())); 
document.close(); 

[C#]

Document document = new Document(); 
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create)); 
XFAFlattener xfaf = new XFAFlattener(document, writer); 
ms.Position = 0; 
xfaf.Flatten(new PdfReader(ms)); 
document.Close(); 

的结果这个扁平化过程是一个普通的PDF,可以通过您的原始代码进行分析。