gpt4 book ai didi

c# - 如何从 OpenFileDialog 获取文件路径并将其传递给 PDFReader? (C#)

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:33 26 4
gpt4 key购买 nike

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

应该导致编译器错误消息

for the non-static field 'ofd' an object instance is required.

2) 看来你没有调用你的方法。您需要调用它并将文件名作为参数传递给它

private void button1_Click(object sender, EventArgs e)
{
ofd.Filter = "PDF|*.pdf";
if (ofd.ShowDialog() == DialogResult.OK)
{
richTextBox1..Text = pdfText(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(path); // Here use path

现在返回的字符串应该出现在你的 richTextBox1

关于c# - 如何从 OpenFileDialog 获取文件路径并将其传递给 PDFReader? (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51985221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com