我想允许用户输入自己的文件名,就像保存文件对话框和流一样(例如: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();
}
我想要的部分" ImageTest.pdf"用pdf扩展名(和.pdf文件类型)命名为用户想要的。
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("ImageTest.pdf", FileMode.Create));
任何人都可以帮助或者有没有人能更好地解决我的问题? 我想截取我的Windows窗体并将图像导出到用户输入名称
下的pdf文件修改 使用saveFileDialog(在bitmap.save之后) - 接收错误"格式错误:不是PDF或已损坏。"
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();
}
}
答案 0 :(得分:3)
我不是ITextSharp的专家,但我认为您的代码应该是这样的
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();
}
换句话说,只需将SaveFileDialog中选择的FileName字符串传递给PdfWriter.GetInstance方法