[英]c# Get Path from a busy file using openFileDialog in winforms
[英]How can I get the path of a file from an OpenFileDialog and pass it to a PDFReader? (C#)
OpenFileDialog ofd = new OpenFileDialog();
private void button1_Click(object sender, EventArgs e)
{
ofd.Filter = "PDF|*.pdf";
if (ofd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Text = ofd.SafeFileName;
}
}
public static string pdfText(string path)
{
//this is the error, I cannot get the path of the File I chose from the OpenFileDialog
PdfReader reader = new PdfReader(ofd.FileName);
string text = string.Empty;
for (int page = 1; page <= reader.NumberOfPages; page++)
{
text = text += PdfTextExtractor.GetTextFromPage(reader, page);
}
reader.Close();
return text;
}
我需要從OpenFileDialog獲取用戶選擇的文件的路徑,但無法將其傳遞給PDFReader
1)您不能在static
方法中使用類變量,因此在此行中訪問ofd
:
PdfReader reader = new PdfReader(ofd.FileName);
應該導致編譯器錯誤消息,
對於非靜態字段“ ofd”,需要一個對象實例。
2)似乎您沒有在調用您的方法。 您需要調用它並將文件名作為參數傳遞給它
private void button1_Click(object sender, EventArgs e)
{
ofd.Filter = "PDF|*.pdf";
if (ofd.ShowDialog() == DialogResult.OK)
{
richTextBox1..Text = pdfText(ofd.SafeFileName);
}
}
然后,您需要在方法內部使用method參數:
public static string pdfText(string path)
{
//this is the error, I cannot get the path of the File I chose from the OpenFileDialog
PdfReader reader = new PdfReader(path); // Here use path
現在返回的字符串應該出現在您的richTextBox1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.