允许用户在c# windows窗体(iTextSharp)中设置pdf文件名

本文关键字:iTextSharp pdf 文件名 窗体 设置 许用户 用户 windows | 更新日期: 2023-09-27 17:49:28

我想让用户输入自己的文件名,就像保存文件对话框和流(示例:Stream s = File.Open(sfdPdf.FileName, FileMode.CreateNew)

下面是我的代码:
private void btnSave_Click(object sender, EventArgs e)
            {
                System.Drawing.Rectangle bounds = this.Bounds;
                using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap))
                    {
                        graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                    }
                    bitmap.Save("Image.jpeg", ImageFormat.Jpeg);
                }
                Document doc = new Document(PageSize.LETTER, bounds.Left, bounds.Right, bounds.Top, bounds.Bottom);
                PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("ImageTest.pdf", FileMode.Create));
                doc.Open();
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("Image.jpeg");
                doc.Add(image);
                doc.Close();

        }

我希望部分"ImateTest.pdf"命名为用户想要的

PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("ImageTest.pdf", FileMode.Create));

有人能帮忙吗?

允许用户在c# windows窗体(iTextSharp)中设置pdf文件名

你需要使用。net保存文件对话框。

https://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog%28v=vs.110%29.aspx

 Stream myStream ;
 SaveFileDialog saveFileDialog1 = new SaveFileDialog();
 saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
 saveFileDialog1.FilterIndex = 2 ;
 saveFileDialog1.RestoreDirectory = true ;
 if(saveFileDialog1.ShowDialog() == DialogResult.OK)
 {
     if((myStream = saveFileDialog1.OpenFile()) != null)
     {
         // Code to write the stream goes here.
         myStream.Close();
     }
 }