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

时间:2015-05-15 17:01:39

标签: pdf c#

我希望用户输入自己的文件名,就像保存文件对话框和流一样(例如: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));

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您需要使用.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();
     }
 }