How can I save a PrintDocument as PDF file?

852 views Asked by At

I have tried couple examples on stackoverflow using with iText library but I didn't get success. I want to save printDocument2 as a pdf file and with OpenFileDialog Is there anyway to do without using itext library.

I want to create a method about it. I am very beginner to c# I would feel appreciate If you help me out thank you all

Print document method:

private void printDocument2_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            Brush kalın = new SolidBrush(Color.Black);
            Font yazi = new Font("Arial", 16, FontStyle.Regular);
            Font yazince = new Font("Arial", 10, FontStyle.Regular);
            Font yazince1 = new Font("Arial", 8, FontStyle.Regular);
            Font yazince2 = new Font("Arial", 7, FontStyle.Regular);
            Font yazince3 = new Font("Arial", 6, FontStyle.Regular);
            Font baslik = new Font("Arial", 14, FontStyle.Bold);
            Font diyagrambaslik = new Font("Arial", 12, FontStyle.Bold);
            Font baslikalt = new Font("Arial", 10, FontStyle.Bold);
            Font rapor = new Font("Arial", 10, FontStyle.Regular);
            Font rapor2 = new Font("Arial", 9, FontStyle.Regular);
            Font koyuitalik = new Font("Arial", 10, FontStyle.Italic | FontStyle.Bold);
            Pen kesikli = new Pen(Color.Black, 2);
            kesikli.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

            Pen ince = new Pen(Color.DarkGray, 2);
            Pen kalin = new Pen(Color.Red, 4);
            Pen orta = new Pen(Color.Black, 1);
            Pen kalem = new Pen(Color.Black, 2);
            Brush siyah = new SolidBrush(Color.Black);
            Form1 frm1 = new Form1();
            Class1 obj = new Class1();
            frm1.malzeme_hesap();

            if (frm1.sev.Value == 2)
            {

                //Çerçeve
                g.DrawLine(kalem, 15, 15, 790, 15);
                g.DrawLine(kalem, 790, 15, 790, 1130);
                g.DrawLine(kalem, 15, 1130, 790, 1130);
                g.DrawLine(kalem, 15, 1130, 15, 15);
                //Çerçeve Sonu
                //üst baslık ölçüleri

                g.DrawLine(kalem, 15, 110, 790, 110);//yatay cizgi
                g.DrawString("İşin Adı : ", yazi, siyah, 200, 30);
                g.DrawString("Projeyi Yapan : ", yazi, siyah, 200, 60);
                g.DrawLine(kalem, 165, 15, 165, 110);// sol resim
                /* rect = new Rectangle(15, 15, 150, 95);
                 g.DrawImage(image, rect);*/

                //üst baslık ölçüleri
                g.DrawLine(kalem, 15, 110, 790, 110);//yatay cizgi
                //g.DrawString("İşin Adı : " + isinAdi, yazi, siyah, 200, 30);
                //g.DrawString("Projeyi Yapan : " + adsoyad, yazi, siyah, 200, 60);
                g.DrawLine(kalem, 165, 15, 165, 110);// sol resim
                Image image3 = Properties.Resources.logo;

                Rectangle rect3 = new Rectangle(40, 15, 100, 95);
                g.DrawImage(image3, rect3);
                g.DrawLine(kalem, 15, 360, 790, 360);//orta çizgi 
                                                     //rect = new Rectangle(40, 115, 715, 240);
                                                     //g.DrawImage(image3, rect);

                Image image2 = Properties.Resources.DURUM_2;
                Rectangle rect = new Rectangle(280, 115, 215, 240);
                g.DrawImage(image2, rect);
                if (fotografsecili == true)
                {
                    
                    g.DrawLine(kalem, 640, 15, 640, 110);//sag resim baslangıc
                    rect4 = new Rectangle(655, 17, 120, 93);
                    g.DrawImage(image5, rect4);
                }
                else
                {
                    g.DrawLine(kalem, 640, 15, 640, 110);//sag resim baslangıc
                }

                //alt baslık
                image4 = Properties.Resources.ista;
                g.DrawLine(kalem, 15, 1110, 790, 1110);
                g.DrawLine(kalem, 760, 1110, 760, 1130);
                rect = new Rectangle(20, 1110, 20, 20);
                g.DrawImage(image4, rect);
                g.DrawString("Bu program İSTA Yazılım tarafından geliştirilmiştir.", yazince, siyah, 50, 1112);
                g.DrawString("1", yazince, siyah, 770, 1112);

                //Rapor baslangıc
                g.DrawString("KUYU TEMEL HESAP RAPORU", baslik, siyah, 270, 365);
                double deger1, deger2;
                double cbolunbet;
                double sboluncel;

                g.DrawString("KUYU TEMEL GEOMETRİK BİLGİLERİ", baslikalt, siyah, 20, 400);
                g.DrawString("Kuyu temel seviye sayısı", rapor, siyah, 20, 420);
                g.DrawString(": " + frm1.sev.Value.ToString(), rapor, siyah, 350, 420);
                g.DrawString("1. seviye genişlik", rapor, siyah, 20, 440);
                g.DrawString(": " + frm1.gen1.Value + " cm ", rapor, siyah, 350, 440);
                g.DrawString("2. seviye genişlik", rapor, siyah, 20, 460);
                g.DrawString(": " + frm1.gen2.Value + " cm ", rapor, siyah, 350, 460);
                g.DrawString("Toplam genişik", rapor, siyah, 20, 480);
                g.DrawString(": " + frm1.gen1.Value + frm1.gen2.Value + " cm ", rapor, siyah, 350, 480);
                g.DrawString("1. seviye yükseklik", rapor, siyah, 20, 500);
                g.DrawString(": " + frm1.yuk1.Value + " cm ", rapor, siyah, 350, 500);
                g.DrawString("2. seviye yükseklik", rapor, siyah, 20, 520);
                g.DrawString(": " + frm1.yuk2.Value + " cm ", rapor, siyah, 350, 520);
                e.HasMorePages = false;
            }
        }
0

There are 0 answers