如何从按钮获取文件

时间:2019-02-05 08:51:17

标签: c# pdf itext

我使用iTextSharp制作了一个应用程序,将数字放在PDF文件中。 如下面的代码所示,仅当文件位于特定目录中时,我的应用程序才能执行此操作。

因此,我做了一个“其他”按钮,用户可以在其中选择文件。 我现在要做的是,选择的文件将下载转换后的PDF。

代码:

NameError

所以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); 不应是特定目录,它应从button3获取所选文件。 您可能已经注意到,我是编程的新手,所以我想也许可以这样做:File.ReadAllBytes(@"L:\Users\user\Documents\PDFnummerieren\PDF.pdf"); 得到的字符串。虽然那没用。

感谢您的时间。

2 个答案:

答案 0 :(得分:3)

如果要在同一类的方法之间使用变量,则需要声明一个私有且非静态的类级别变量(也称为“实例字段”)。这对于该类的每个方法都是可见的。

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中的按钮1的代码开头,然后删除超级按钮(在这一点上)

我建议您阅读有关变量的作用域和生命周期的一些文档

答案 1 :(得分:0)

尝试

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
    {
        string fileToOpen = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace(fileToOpen)) return;

            byte[] bytes = File.ReadAllBytes(fileToOpen);
            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)
            {
                fileToOpen = FD.FileName;

                System.IO.FileInfo File = new System.IO.FileInfo(FD.FileName);



                System.IO.StreamReader reader = new System.IO.StreamReader(fileToOpen);

            }
        }
    }
}

我已将您的变量置于类范围内,现在您可以在类中的任何位置访问它