我正在使用以下方法逐行提取pdf文本.但问题是,它不是在文字和数字之间阅读空格.什么可以解决这个问题?
我只想创建一个字符串列表,列表对象中的每个字符串都有一个来自pdf的文本行,因为它在pdf中包含空格.
public void readtextlinebyline(string filename) {
List<string> strlist = new List<string>();
PdfReader reader = new PdfReader(filename);
string text = string.Empty;
for (int page = 1; page <= 1; page++)
{
text += PdfTextExtractor.GetTextFromPage(reader, page ,new LocationTextExtractionStrategy())+" ";
}
reader.Close();
string[] words = text.Split('\n');
foreach (string word in words)
{
strlist.Add(word);
}
foreach (string st in strlist)
{
Response.Write(st +"<br/>");
}
}
Run Code Online (Sandbox Code Playgroud)
我已经通过将策略更改为SimpleTextExtractionStrategy来尝试此方法,但它也不适用于我.
这是我的示例代码:
CodeSnippet 1:此代码在我的文件存储库服务器中执行,并使用WCF服务将文件作为编码字符串返回:
byte[] fileBytes = new byte[0];
using (FileStream stream = System.IO.File.OpenRead(@"D:\PDFFiles\Sample1.pdf"))
{
fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
}
string retVal = System.Text.Encoding.Default.GetString(fileBytes); // fileBytes size is 209050
Run Code Online (Sandbox Code Playgroud)
代码片段2:客户端框,需要PDF文件,接收编码的字符串并转换为PDF并保存到本地.
byte[] encodedBytes = System.Text.Encoding.Default.GetBytes(retVal); /// GETTING corrupted here
string pdfPath = @"C:\DemoPDF\Sample2.pdf";
using (FileStream fileStream = new FileStream(pdfPath, FileMode.Create)) //encodedBytes is 327279
{
fileStream.Write(encodedBytes, 0, encodedBytes.Length);
fileStream.Close();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作绝对精美Framework 4.5,4.6.1
当我在Asp.Net Core 2.0中使用相同的代码时,它无法正确转换为字节数组.我没有收到任何运行时错误,但是最终的PDF在创建后无法打开.pdf文件损坏时引发错误.
我也试过了Encoding.Unicode和Encoding.UTF-8.但是为最终PDF获得相同的错误.
另外,我注意到当我使用Encoding.Unicode时,至少原始字节数组和结果字节数组大小相同.但是其他编码类型也与字节大小不匹配.
那么,问题是,在.NET Core 2.0中,System.Text.Encoding.Default.GetBytes被破坏了吗?
我已经编辑了我的问题以便更好地理解. Sample1.pdf存在于不同的服务器上,并使用WCF进行通信,以将数据传输到存储文件编码流的Client,并转换为Sample2.pdf
希望我的问题现在有道理.