保存用户输入文件名PDF文件(iTextSharp的) [英] Save pdf file with user input filename (iTextSharp)

查看:146
本文介绍了保存用户输入文件名PDF文件(iTextSharp的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户输入自己的文件名,就像保存文件对话框,并流(例如:流S = File.Open(sfdPdf.FileName,FileMode.CreateNew)



下面是我的代码:

 私人无效btnSave_Click (对象发件人,EventArgs五)
{

System.Drawing.Rectangle边界= this.Bounds;
使用(位图位图=新位图(bounds.Width,bounds.Height ))
{使用(图形图像
= Graphics.FromImage(位图))
{
graphics.CopyFromScreen(新点(bounds.Left,bounds.Top)点。空,bounds.Size);
}
bitmap.Save(Image.jpeg,ImageFormat.Jpeg);
}

文档的文档=新的文件( PageSize.LETTER,bounds.Left,bounds.Right,bounds.Top,bounds.Bottom);
PdfWriter WRI = PdfWriter.GetInstance(文件,新的FileStream(ImageTest.pdf,FileMode.Create));
doc.Open();
iTextSharp.text.Image图像= iTextSharp.text.Image.GetInstance(Image.jpeg);
doc.Add(图片);
doc.Close();
}



我想要的部分ImageTest.pdf被命名为用户希望用PDF格式扩展名(和.pdf文件类型)。



PdfWriter作家= PdfWriter.GetInstance(文件,新的FileStream(ImageTest.pdf,FileMode.Create));



谁能帮助或有没有人对我的问题更好的解决方案?
我想利用我的窗户形式和出口形象的截图pdf文件根据用户输入的名称



编辑:
将saveFileDialog(bitmap.save后) - 接收错误。格式错误:不是一个PDF或损坏

  SaveFileDialog SFD =新SaveFileDialog(); 
sfd.Filter =PDF文件| * .PDF
如果(sfd.ShowDialog()== DialogResult.OK)
{$ B $使用B(流S = File.Open(sfd.FileName,FileMode.CreateNew))
$ { b $ b稿件DOC =新的文件(PageSize.LETTER,bounds.Left,bounds.Right,bounds.Top,bounds.Bottom);
PdfWriter WRI = PdfWriter.GetInstance(文件,新的FileStream(ImageTest.pdf,FileMode.Create));
doc.Open();
iTextSharp.text.Image图像= iTextSharp.text.Image.GetInstance(Image.jpeg);
doc.Add(图片);
doc.Close();
S.CLOSE();
s.Dispose();
}
}


解决方案

我我不是iTextSharp的的专家,但我认为你的代码应该是这样的。

  SaveFileDialog SFD =新SaveFileDialog(); 
sfd.Filter =PDF文件| * .PDF
如果(sfd.ShowDialog()== DialogResult.OK)
{
文档的文档=新的文件(PageSize.LETTER,bounds.Left,bounds.Right,bounds.Top,界限。底部);
PdfWriter WRI = PdfWriter.GetInstance(文件,新的FileStream(sfd.FileName,FileMode.Create));
doc.Open();
iTextSharp.text.Image图像= iTextSharp.text.Image.GetInstance(Image.jpeg);
doc.Add(图片);
doc.Close();
}

在换句话说,只通过在SaveFileDialog选择去的PdfWriter的文件名的字符串。 getInstance方法


I want to allow user to enter his own file name, just like save file dialog and stream (Example: Stream s = File.Open(sfdPdf.FileName, FileMode.CreateNew)

Here is my code:

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

I want the part "ImageTest.pdf" to be named as the user want with pdf extension (and .pdf filetype).

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

Can anyone help or does anyone has better solution for my problem? I want to take screenshot of my windows form and export image to pdf file under user input name

EDIT: With saveFileDialog (after bitmap.save) - Receiving error "Format Error: Not a PDF or corrupted."

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pdf File |*.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
    using (Stream s = File.Open(sfd.FileName, FileMode.CreateNew))
    {
        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();
        s.Close();
        s.Dispose();
    }               
}

解决方案

I am not an expert of ITextSharp, but I think that your code should be something like this

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pdf File |*.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
    Document doc = new Document(PageSize.LETTER, bounds.Left, bounds.Right, bounds.Top, bounds.Bottom);
    PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(sfd.FileName, FileMode.Create));
    doc.Open();
    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("Image.jpeg");
    doc.Add(image);
    doc.Close();
}

In other words, just pass the FileName string choosen in SaveFileDialog to the PdfWriter.GetInstance method

这篇关于保存用户输入文件名PDF文件(iTextSharp的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆