Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();


}



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.
Posted
Updated 26-Aug-22 6:41am

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900