将printdocument转换为PDF [英] Convert printdocument to PDF

查看:765
本文介绍了将printdocument转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要将PrintDocument转换为Pdf。我怎么能这样做。



我尝试过:



Hi all,
I need to convert PrintDocument to Pdf . How can I do this.

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
    PrintDocument p = new PrintDocument();


    p.DefaultPageSettings.PaperSize = new PaperSize("Custom", 755, 378);
    p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
    {
        e1.Graphics.DrawString(dateTimePicker1.Text, new Font("verdana", 10), new
       SolidBrush(Color.Black), new RectangleF(603, 95,
      p.DefaultPageSettings.PrintableArea.Width,
       p.DefaultPageSettings.PrintableArea.Height));
        e1.Graphics.DrawString(label3.Text, new Font("verdana", 10), new
        SolidBrush(Color.Black), new RectangleF(159, 171,
        p.DefaultPageSettings.PrintableArea.Width,
        p.DefaultPageSettings.PrintableArea.Height));

        e1.Graphics.DrawString(label5.Text, new Font("verdana", 10), new
        SolidBrush(Color.Black), new RectangleF(618, 278,
        p.DefaultPageSettings.PrintableArea.Width,
        p.DefaultPageSettings.PrintableArea.Height));
        e1.Graphics.DrawString(label7.Text, new Font("verdana", 10), new
        SolidBrush(Color.Black), new RectangleF(111, 218,
        p.DefaultPageSettings.PrintableArea.Width,
        p.DefaultPageSettings.PrintableArea.Height));
    };


    p.Print();


}







这是我做了什么。这里不是打印到打印机。我需要将其保存为Pdf文档。我怎么能这样做。非常感谢任何帮助。




This is what I have done. Here instead of printing to a printer.I need to save it as a Pdf document.How can I do this.Any help will be really appreciated.

推荐答案

Hi


您可以实例化SaveFileDialog,然后您可以将其过滤到您想要的任何文档PDF。以下是绿灯:

Hi
You can instantiate the SaveFileDialog, then you can filter it to any document you want in your case its PDF. Here is the green light:
SaveFileDialog sfd = new SaveFileDialog();




private void btnSavePDF_Click(object sender, EventArgs e)
        {
            sfd.Title = "Save As PDF";
            sfd.Filter = "(*.pdf)|*.pdf";
            sfd.InitialDirectory = @"C:\";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                iTextSharp.text.Document doc = new iTextSharp.text.Document();
                PdfWriter.GetInstance(doc,new FileStream(sfd.FileName,FileMode.Create));
                doc.Open();
                doc.Add(new iTextSharp.text.Paragraph(rtb.Text));
                doc.Close();            
            }
        }





然后你可以修改你的代码。



Then you can twik your code.


这篇关于将printdocument转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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