C# Itextsharp文本提取
我正在使用vb.net上的itextsharp从pdf文件中获取文本内容。该解决方案适用于某些文件,但不适用于其他文件,即使是非常简单的文件。问题是令牌stringvalue设置为null(一组空的方形框) 我可以测量内容的长度,但无法获得实际的字符串内容 我意识到这取决于pdf的字体。如果我使用Acrobat或PdfCreator with Courier创建pdf(顺便说一下,这是我的visual studio编辑器中的默认字体),我可以获取所有文本内容。如果相同的pdf使用不同的字体生成,我会得到空的方形框 现在的问题是,如何在不考虑字体设置的情况下提取文本 谢谢退房 或 两者都需要iText[Sharp]的最新版本。实际上,自己解析内容流只是在这一点上重新发明轮子。别让自己痛苦,让我来帮你吧C# Itextsharp文本提取,c#,itextsharp,C#,Itextsharp,我正在使用vb.net上的itextsharp从pdf文件中获取文本内容。该解决方案适用于某些文件,但不适用于其他文件,即使是非常简单的文件。问题是令牌stringvalue设置为null(一组空的方形框) 我可以测量内容的长度,但无法获得实际的字符串内容 我意识到这取决于pdf的字体。如果我使用Acrobat或PdfCreator with Courier创建pdf(顺便说一下,这是我的visual studio编辑器中的默认字体),我可以获取所有文本内容。如果相同的pdf使用不同的字体生成,
PDFTextractor将为您处理所有不同的字体/编码问题。。。不管怎么说,都是可以处理的。如果您不能准确地从Reader复制/粘贴,则PDF中没有足够的信息从内容流中获取字符信息。补充Mark的回答,这对我有很大帮助。iTextSharp实现名称空间和类与java版本有点不同
public static string GetTextFromAllPages(String pdfPath)
{
PdfReader reader = new PdfReader(pdfPath);
StringWriter output = new StringWriter();
for (int i = 1; i <= reader.NumberOfPages; i++)
output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()));
return output.ToString();
}
public静态字符串GetTextFromAllPages(字符串pdfPath)
{
PdfReader reader=新的PdfReader(pdfPath);
StringWriter输出=新建StringWriter();
对于(int i=1;i,如果有人需要,这里有一个带有iTextSharp.text.pdf.PdfName.ANNOTS和iTextSharp.text.pdf.PdfName.CONTENT的变体
string strFile = @"C:\my\path\tothefile.pdf";
iTextSharp.text.pdf.PdfReader pdfRida = new iTextSharp.text.pdf.PdfReader(strFile);
iTextSharp.text.pdf.PRTokeniser prtTokeneiser;
int pageFrom = 1;
int pageTo = pdfRida.NumberOfPages;
iTextSharp.text.pdf.PRTokeniser.TokType tkntype ;
string tknValue;
for (int i = pageFrom; i <= pageTo; i++)
{
iTextSharp.text.pdf.PdfDictionary cpage = pdfRida.GetPageN(i);
iTextSharp.text.pdf.PdfArray cannots = cpage.GetAsArray(iTextSharp.text.pdf.PdfName.ANNOTS);
if(cannots!=null)
foreach (iTextSharp.text.pdf.PdfObject oAnnot in cannots.ArrayList)
{
iTextSharp.text.pdf.PdfDictionary cAnnotationDictironary = (iTextSharp.text.pdf.PdfDictionary)pdfRida.GetPdfObject(((iTextSharp.text.pdf.PRIndirectReference)oAnnot).Number);
iTextSharp.text.pdf.PdfObject moreshit = cAnnotationDictironary.Get(iTextSharp.text.pdf.PdfName.CONTENTS);
if (moreshit != null && moreshit.GetType() == typeof(iTextSharp.text.pdf.PdfString))
{
string cStringVal = ((iTextSharp.text.pdf.PdfString)moreshit).ToString();
if (cStringVal.ToUpper().Contains("LOS 8"))
{ // DO SOMETHING FUN
}
}
}
}
pdfRida.Close();
string strFile=@“C:\my\path\tothefile.pdf”;
iTextSharp.text.pdf.PdfReader pdfRida=新的iTextSharp.text.pdf.PdfReader(strFile);
iTextSharp.text.pdf.PRTokeniser-prtTokeneiser;
int pageFrom=1;
int pageTo=pdfRida.NumberOfPages;
iTextSharp.text.pdf.PRTokeniser.TokType tkntype;
字符串tknValue;
对于(int i=pageFrom;i+1)给出完整的代码示例,而不仅仅是一行代码。是否可以使用itextsharp在“ftp服务器”上提取文件?@Munavvar,不幸的是,我没有这样的经验。但通常您可以读取文件流或二进制文件的frs中的ftp,并向itextsharp馈送返回一个空的(只有空格)有时可能的原因是什么?@Nerdintraining,请更确切地说,有时,可能是当异常的pdf格式通过时,或者可能是当页面实际上是一个必须首先进行OCR的图像时。
String pageText =
PdfTextExtractor.getTextFromPage(myReader, pageNum, new LocationTextExtractionStrategy());
public static string GetTextFromAllPages(String pdfPath)
{
PdfReader reader = new PdfReader(pdfPath);
StringWriter output = new StringWriter();
for (int i = 1; i <= reader.NumberOfPages; i++)
output.WriteLine(PdfTextExtractor.GetTextFromPage(reader, i, new SimpleTextExtractionStrategy()));
return output.ToString();
}
string strFile = @"C:\my\path\tothefile.pdf";
iTextSharp.text.pdf.PdfReader pdfRida = new iTextSharp.text.pdf.PdfReader(strFile);
iTextSharp.text.pdf.PRTokeniser prtTokeneiser;
int pageFrom = 1;
int pageTo = pdfRida.NumberOfPages;
iTextSharp.text.pdf.PRTokeniser.TokType tkntype ;
string tknValue;
for (int i = pageFrom; i <= pageTo; i++)
{
iTextSharp.text.pdf.PdfDictionary cpage = pdfRida.GetPageN(i);
iTextSharp.text.pdf.PdfArray cannots = cpage.GetAsArray(iTextSharp.text.pdf.PdfName.ANNOTS);
if(cannots!=null)
foreach (iTextSharp.text.pdf.PdfObject oAnnot in cannots.ArrayList)
{
iTextSharp.text.pdf.PdfDictionary cAnnotationDictironary = (iTextSharp.text.pdf.PdfDictionary)pdfRida.GetPdfObject(((iTextSharp.text.pdf.PRIndirectReference)oAnnot).Number);
iTextSharp.text.pdf.PdfObject moreshit = cAnnotationDictironary.Get(iTextSharp.text.pdf.PdfName.CONTENTS);
if (moreshit != null && moreshit.GetType() == typeof(iTextSharp.text.pdf.PdfString))
{
string cStringVal = ((iTextSharp.text.pdf.PdfString)moreshit).ToString();
if (cStringVal.ToUpper().Contains("LOS 8"))
{ // DO SOMETHING FUN
}
}
}
}
pdfRida.Close();