c# - 如何从按钮获取文件
问题描述
我使用 iTextSharp 制作了一个应用程序,将数字放在 PDF 文件中。正如您将在以下代码中看到的,我的应用程序只能执行此操作,前提是文件位于特定目录中。
所以我做了一个“其他”按钮,用户可以在其中选择一个文件。我现在要做的是,所选文件将下载转换后的 PDF。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace NummerierePDF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] bytes = File.ReadAllBytes(@"L:\Users\user\Documents\PDFnummerieren\PDF.pdf");
iTextSharp.text.Font blackFont = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
using (MemoryStream stream = new MemoryStream())
{
PdfReader reader = new PdfReader(bytes);
using (PdfStamper stamper = new PdfStamper(reader, stream))
{
int pages = reader.NumberOfPages;
for (int i = 1; i <= pages; i++)
{
ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
}
}
bytes = stream.ToArray();
}
File.WriteAllBytes(@"L:\Users\user\Documents\PDFnummerieren\PDF1.pdf", bytes);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button3_Click(object sender, EventArgs e)
{
var FD = new System.Windows.Forms.OpenFileDialog();
if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fileToOpen = FD.FileName;
System.IO.FileInfo File = new System.IO.FileInfo(FD.FileName);
System.IO.StreamReader reader = new System.IO.StreamReader(fileToOpen);
}
}
}
}
所以File.WriteAllBytes(@"L:\Users\user\Documents\PDFnummerieren\PDF1.pdf", bytes);
可以留下来,因为文件在转换后下载到哪里都没有关系。
但File.ReadAllBytes(@"L:\Users\user\Documents\PDFnummerieren\PDF.pdf");
不应该是一个特定的目录,它应该从 button3 中获取选择的文件。正如您可能注意到的那样,我是编程新手,所以我想也许我可以这样做: File.ReadAllBytes(fileToOpen);
获取字符串。虽然这不符合他的工作。
谢谢你的时间。
解决方案
如果要在同一类的方法之间使用变量,则需要声明一个私有且非静态的类级别变量(也称为实例字段)。这对类的每个方法都是可见的。
public partial class Form1 : Form
{
// Here declare a variable visible to all methods inside the class but
// not outside the class. Init it with an empty string
private string theFile = "";
private void button1_Click(object sender, EventArgs e)
{
// When you click the button1 check if the variable
// has been set to something in the button3 click event handler
if(string.IsNullOrEmpty(theFile) || !File.Exists(theFile))
return;
// Now you can use it to load the file and continue with the code
// you have already written
byte[] bytes = File.ReadAllBytes(theFile);
......
......
}
private void button3_Click(object sender, EventArgs e)
{
// The only job of button3 is to select the file to work on
// as you can see we can use the variable also here
var FD = new System.Windows.Forms.OpenFileDialog();
if (FD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
theFile = FD.FileName;
}
}
但是你真的需要一个单独的按钮吗?我的意思是您可以将 button3 中的三行代码直接放在 button1 中代码的开头并删除多余的(此时)button3
我建议你阅读一些关于变量 Scope 和 Lifetime 的文档
推荐阅读
- amazon-web-services - 使用 AWS Glue Crawler 时出现 RecrawlPolicy 问题
- c# - 拆分大文本字段c#
- python - 使用python运行具有不同参数的函数
- r - 仅使用前 5 个创建条形图
- python - Jupyter Notebook - 内核似乎已经死在 . 运行 Neural Prophet 时会自动重启
- php - 如何通过引导表编辑/删除 MySQL 数据库中的信息
- mediawiki - 在共享主机中运行作曲家的问题
- typescript - 带有导入语句的打字稿编译错误
- azure - 您如何使用 rest 将 .csv 文件放入存储帐户?
- python - wxPython - 创建帮助窗口/框架