在我的Windows 8应用程序中,我想逐行读取PDF,然后我想分配一个String数组.我该怎么做?
public StringBuilder addd= new StringBuilder();
string[] array;
private async void btndosyasec_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".pdf");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
PdfReader reader = new PdfReader((await file.OpenReadAsync()).AsStream());
for (int page = 1; page <= reader.NumberOfPages; page++)
{
addd.Append(PdfTextExtractor.GetTextFromPage(reader, page));
string tmp= PdfTextExtractor.GetTextFromPage(reader, page);
array[page] = tmp.ToString();
reader.Close();
}
}
}
解决方法:
嗨,我也有这个问题,我用这个代码,它工作.
您将需要对iTextSharp库的引用.
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
PdfReader reader = new PdfReader(@"D:\test pdf\Blood Journal.pdf");
int intPageNum = reader.NumberOfPages;
string[] words;
string line;
for (int i = 1; i <= intPageNum; i++)
{
text = PdfTextExtractor.GetTextFromPage(reader, i, new LocationTextExtractionStrategy());
words = text.Split('\n');
for (int j = 0, len = words.Length; j < len; j++)
{
line = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(words[j]));
}
}
words数组包含pdf文件的行