2014-09-24 83 views
0

我正在使用Apache PDFBox将.pdf文档转换为使用PDFTextStripper的 WinForms C#应用程序中的文本文件。获取progressBar以显示PDFTextStripper进度

我发现转换大约需要30秒才能完成。我想要做的是在C#progressBar中向用户显示转换的进度。

我已经为程序添加了一个backgroundWorker线程以及事件处理程序。 但是,当我在backgroundWorker1_DoWork中调用PDFTextStripper时,progressBar不会报告任何 进度,直到发生转换。 (示例代码如下所示)

任何人都可以提出一个更好的方法来显示progressBar1中的进度?谢谢。

将我的.pdf文件复制到其位置后,检查文件是否已成功复制,然后调用转换方法。

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; 
} 

回答

0

看起来你已经设置好了。当你在BackgroundWorker线程中发生了一堆事情时,它仍然发生在一个线程中,一行代码在另一个线程中。你不会锁定你的UI线程,这很好,但是你仍然需要等待一行代码在另一个代码运行之前运行。

考虑到你的代码,无论发生的事情在PDFText()必须完成下一行运行,并报告进度回到UI线程(这是什么ReportProgress()一样)前:

string tempText = PDFText(myFile); 

backgroundWorker1.ReportProgress(i); 

只需翻转两行,让你马上在PDFText()做长期运行的东西之前报告进展情况:

backgroundWorker1.ReportProgress(i); 

string tempText = PDFText(myFile); 

另一个观察结果......这里没有必要实例化一个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 
+0

您好格兰特。我在转换之前翻转了工作人员的ReportProgress并看到一些轻微的视觉改善。 PDFText是Apache PDFBox的文本剥离器,它非常强大。特别是因为执行转换需要很长时间,所以我想给用户一些视觉线索,说明转换正在幕后进行。不幸的是,用户在PDF转换为文本文件之前仍然不会收到通知。我很欣赏关于使用文件路径保存文本文件的提示。 – CodeMann 2014-09-24 23:57:12

+0

是的,如果它需要很长时间才能运行,那么我想你会看到进展的变化,然后一段时间内没有其他事情会发生。您可能会考虑将ProgressBar上的“样式”更改为“选取框”,以便用户可以指示某事正在发生,然后将百分比显示在其下的标签中。 – 2014-09-25 00:24:14

+0

你好格兰特。这是一个很好的建议,虽然我修改了一下。我离开progressBar样式为Block,但添加了一个与ProgressPercentage绑定的标签,为用户提供额外的可视化查询。我将标签的文字设置为显示百分比。现在,他们可以看到百分比爬升到100%,然后清除。再次感谢。 – CodeMann 2014-09-25 20:45:54