如何采取从按钮文件

问题描述 投票:1回答:2

我做了一个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);得到的字符串。虽然这并不做他的工作。

谢谢你的时间。

c# pdf itext
2个回答
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直接在Button1的代码的启动和清除superfluos(此时)BUTTON3

我建议你阅读变量的作用域和生存期的一些文档


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

            }
        }
    }
}

我已经把你的变量类的范围内,现在你可以从任何地方访问你的类中

© www.soinside.com 2019 - 2024. All rights reserved.