C# 获取progressBar以显示PDFTextStripper进度
我正在使用ApachePDFBox将.pdf文档转换为文本文件,在 WinC应用程序 我发现转换大约需要30秒才能完成。我想做的是在一个C进度条中向用户显示转换的进度 我在程序中添加了一个backgroundWorker线程以及事件处理程序。 但是,当我在backgroundWorker1\u DoWork中调用PDFTextStripper时,progressBar不会报告任何内容 直到发生转换之后的进度。示例代码如下所示 有人能提出一个更好的方式来展示progressBar1的进展吗?多谢各位 将.pdf文件复制到其位置后,我检查文件是否复制成功,然后调用转换方法C# 获取progressBar以显示PDFTextStripper进度,c#,winforms,C#,Winforms,我正在使用ApachePDFBox将.pdf文档转换为文本文件,在 WinC应用程序 我发现转换大约需要30秒才能完成。我想做的是在一个C进度条中向用户显示转换的进度 我在程序中添加了一个backgroundWorker线程以及事件处理程序。 但是,当我在backgroundWorker1\u DoWork中调用PDFTextStripper时,progressBar不会报告任何内容 直到发生转换之后的进度。示例代码如下所示 有人能提出一个更好的方式来展示progressBar1的进展吗?多谢各
if (File.Exists(@"C:\My PDF Folder\myFile.pdf))
{
string myFile = @"C:\My PDF Folder\myFile.pdf";
Tuple<string> tuple = Tuple.Create(myFile);
backgroundWorker1.RunWorkerAsync(tuple);//convert from .pdf to .txt file.
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i += 20)
{
Tuple<string> tupleArgs = (Tuple<string>)e.Argument;
string myFile = tupleArgs.Item1.ToString();
string tempText = PDFText(myFile);//The PDFTextStripper method
//Report the progress
backgroundWorker1.ReportProgress(i);
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = @"C:\My PDF Folder\myFile.txt";
using (Stream s = File.Open(sfd.FileName, FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(s))
sw.Write(tempText);
}
}
private static String PDFText(String PDFFilePath)
{
PDDocument doc = PDDocument.load(PDFFilePath);
PDFTextStripper stripper = new PDFTextStripper();
string text = stripper.getText(doc);
doc.close();
return text;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//Change the value of the ProgressBar to the BackgroundWorker progress.
progressBar1.Value = e.ProgressPercentage;
}
public void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Value = 0;
}
看来你安排得很好。当你在一个BackgroundWorker线程中发生了大量的事情时,它仍然发生在一个线程中,一行接一行的代码中。您不会锁定UI线程,这很好,但您仍然需要等待一行代码运行,然后再运行另一行代码 考虑到您的代码,无论PDFText中发生了什么,都必须在下一行运行并将进度报告回UI线程之前完成,ReportProgress就是这样做的: 只需翻转两行,以便在PDFText中执行长时间运行的操作之前立即报告进度: 另一个观察。。。这里不需要实例化SaveFileDialog。只需直接使用文件路径:
using (Stream s = File.Open(@"C:\My PDF Folder\myFile.txt", FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(s))
sw.Write(tempText);
更好,甚至更短:
File.WriteAllText(@"C:\My PDF Folder\myFile.txt", tempText); // overwrite the file
File.AppendAllText(@"C:\My PDF Folder\myFile.txt", tempText); // or append to the file
你好,格兰特。在转换之前,我翻阅了工人的进度报告,看到了一些轻微的视觉改善。PDFText是ApachePDFBox的文本剥离器,它非常占用cpu。特别是因为执行转换需要很长时间,我想给用户一些视觉线索,说明转换是在幕后进行的。不幸的是,在PDF转换为文本文件之前,用户仍然不会收到通知。我非常感谢关于使用文件路径保存文本文件的提示。你好,格兰特。这是一个很好的建议,尽管我做了一些修改。我将progressBar样式保留在Block中,但添加了一个与ProgressPercentage绑定的标签,以便为用户提供额外的视觉效果。我将标签的文本设置为显示百分比。现在,他们可以看到百分比上升到100%,然后清除。再次感谢。
using (Stream s = File.Open(@"C:\My PDF Folder\myFile.txt", FileMode.OpenOrCreate))
using (StreamWriter sw = new StreamWriter(s))
sw.Write(tempText);
File.WriteAllText(@"C:\My PDF Folder\myFile.txt", tempText); // overwrite the file
File.AppendAllText(@"C:\My PDF Folder\myFile.txt", tempText); // or append to the file