منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : تعديل كود تصدير الداتاغريد الى Pdf
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
السلام عليكم

هل من الممكن اضافة التعديلات التالية على الكود من فظلكم

1 - في اعلى الصفحة على اليمين اسم الجدول يؤخذ من التكست بكس1
2 - في اسفل الصفحة على اليمين اسم المؤسسة يؤخذ من التكست بكس2 و على اليسار تاريخ اليوم
3- هل من الممكن اضافة كود لتغيير مكان عمود من الجدول فقط في ملف PDF

بارك الله فيكم جميعا

كود :
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
      On Error Resume Next
      Dim res As DialogResult = MsgBox("هل أنت متأكد من عملية التصدير ", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "")
      If res = Windows.Forms.DialogResult.Yes Then
          ' تجهيز الجدول
          Dim pdfTable As New PdfPTable(DataGridView1.ColumnCount)
          pdfTable.DefaultCell.Padding = 3
          pdfTable.WidthPercentage = 100
          ' توسيط الجدول في الصفحة
          pdfTable.HorizontalAlignment = Element.ALIGN_CENTER
          ' تفعيل الاتجاه من اليمين لليسار - ضروري للغة العربية
          pdfTable.RunDirection = PdfWriter.RUN_DIRECTION_RTL
          ' تحديد سماكة الاطار
          pdfTable.DefaultCell.BorderWidth = 1
          ' تعيين ارتفاع افتراضي لخلايا الجدول
          pdfTable.DefaultCell.FixedHeight = 100.0F
          ' الخطوط يجب أن تكون من خطوط النظام لتوافقها مع اللغة العربية
          ' لكن لها طريقة خاصة من معرفة مسار ملف اسم الملف الأصلي كالتالي  
          ' تجهيز خط من النظام - آريـال عريض
          ' Arial (Bold) font
          Dim fontArialBoldPath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arialbd.ttf")
          Dim fontArialBold As BaseFont = BaseFont.CreateFont(fontArialBoldPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
          ' fontArialBold يتم استخدام هذا الاسم للخط        
          ' قراءة عناوين أعمدة جدول الداتاجريدفيو
          For Each column As DataGridViewColumn In DataGridView1.Columns
              ' تحديد الخط وحجمه
              Dim fnt As New Font(fontArialBold, 18)
              ' تجهيز محتوى الخلية مع الخط
              Dim pdfCell As New PdfPCell(New Phrase(column.HeaderText, fnt))
              ' إرتفاع الخلية
              pdfCell.FixedHeight = 40
              ' تغيير لون خلفية الخلية
              pdfCell.BackgroundColor = New iTextSharp.text.BaseColor(240, 240, 240)
              ' توسيط عمودي
              pdfCell.VerticalAlignment = Element.ALIGN_MIDDLE
              ' توسيط أفقي
              pdfCell.HorizontalAlignment = Element.ALIGN_CENTER
              ' إضافة الخلية إلى الجدول
              pdfTable.AddCell(pdfCell)
          Next
          ' قراءة أسطر جدول الداتاجريدفيو
          For Each row As DataGridViewRow In DataGridView1.Rows
              ' يتم الخروج من الحلقة إذا كان السطر الأخير الجديد
              If row.IsNewRow Then Exit For
              ' قراءة خلايا سطر جدول التاجريدفيو
              For Each cell As DataGridViewCell In row.Cells
                  ' تصحيح محتوى نص خلية جدول التاجريدفيو
                  If IsNothing(cell.Value) Then cell.Value = String.Empty
                  ' تحديد الخط وحجمه ولونه
                  Dim fnt = New iTextSharp.text.Font(fontArialBold, 16, iTextSharp.text.Font.NORMAL, New BaseColor(193, 36, 67))
                  ' تحديد الخط وحجمه بدون تحديد اللون
                  ' Dim fnt = New iTextSharp.text.Font(fontTimesNewRomanBold, 24)
                  ' تجهيز محتوى الخلية مع الخط مع مسافة قبلها
                  Dim pdfCell As New PdfPCell(New Phrase(" " & cell.Value, fnt))
                  ' إرتفاع الخلية
                  pdfCell.FixedHeight = 40
                  ' توسيط عمودي
                  pdfCell.VerticalAlignment = Element.ALIGN_MIDDLE
                  ' إضافة الخلية إلى الجدول
                  pdfTable.AddCell(pdfCell)
              Next
          Next
          ' تجهيز المجلد
          Dim SvFile As New SaveFileDialog
          SvFile.Filter = "Pdf File |*.Pdf"
          Dim folderPath As String = String.Empty
          If SvFile.ShowDialog = Windows.Forms.DialogResult.OK Then
              folderPath = SvFile.FileName
          Else
              Exit Sub
          End If
          'Dim folderPath As String = Application.StartupPath '& "\DataGridViewExport\"
          'If Not System.IO.Directory.Exists(folderPath) Then System.IO.Directory.CreateDirectory(folderPath)
          ' إسم الملف كامل مع المسار
          Dim fileName As String = folderPath '& "DataGridViewExport.pdf"
          ' تجهيز المستند
          Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
          ' كتابة المستند في الملف
          PdfWriter.GetInstance(pdfDoc, New System.IO.FileStream(fileName, System.IO.FileMode.Create))
          pdfDoc.Open()
          pdfDoc.Add(pdfTable)
          pdfDoc.Close()
          MsgBox("تمت عملية التصدير الجدول بنجاح", "تصدير", SystemIcons.Information)
      ElseIf res = Windows.Forms.DialogResult.No Then
          MsgBox("تمت عملية إلغاء الجدول", "إلغاء", SystemIcons.Error)
          Exit Sub
      End If
  End Sub


يعطيك الصحة أخي ، تتبعت الفيديو لكن لم ينجح معي نقل الكتابة من التكست بكس الى ملف الـ PDF

 هذا نفس كود الفيديو ، فقط في الفيديو يستعمل تكست بوكس 2 مسار الملف المصدر و انا في مشروعي لا استعمل تكست بوكس 2 الملف انا اختار اين احفضه ، هل من الممكن تصحيح الكود  بارك الله فيك أو من احد الاخوة جزاهم الله كل خير


كود :
Imports System.Data.Odbc ' import namespaces ODBC class
Imports iTextSharp.text ' import namespaces .net pdf library
Imports iTextSharp.text.pdf
Imports System.IO
Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       openconnection() ' open our connection
       Dim da As OdbcDataAdapter ' declaration data adapter
       Dim dt As DataTable ' declaration data table
       da = New OdbcDataAdapter("SELECT idsiswa,nama,nis,tempatlahir,alamat FROM biodata", connection)
       dt = New DataTable
       da.Fill(dt)
       DataGridView1.DataSource = dt ' bind data table into datagridview
       DataGridView1.Refresh()
       connection.Close() ' close our connection
       da.Dispose()
       'configuration fo save file dialog
       SaveFileDialog1.FileName = ""
       SaveFileDialog1.Filter = "PDF (*.pdf)|*.pdf"
       TextBox1.Text = "" ' for title
       TextBox2.Text = "" ' for file locations
   End Sub
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       SaveFileDialog1.FileName = ""
       If SaveFileDialog1.ShowDialog = DialogResult.OK Then
           ' declaration textbox2 to save file dialog name
           TextBox2.Text = SaveFileDialog1.FileName
       End If
   End Sub
   Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
       ' you must import itextsharp namespace into our form
       ' download links is available in the descriptions
       Dim Paragraph As New Paragraph ' declaration for new paragraph
       Dim PdfFile As New Document(PageSize.A4, 40, 40, 40, 20) ' set pdf page size
       PdfFile.AddTitle(TextBox1.Text) ' set our pdf title
       Dim Write As PdfWriter = PdfWriter.GetInstance(PdfFile, New FileStream(TextBox2.Text, FileMode.Create))
       PdfFile.Open()

       ' declaration font type
       Dim pTitle As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
       Dim pTable As New Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

       ' insert title into pdf file
       Paragraph = New Paragraph(New Chunk(TextBox1.Text, pTitle))
       Paragraph.Alignment = Element.ALIGN_CENTER
       Paragraph.SpacingAfter = 5.0F

       ' set and add page with current settings
       PdfFile.Add(Paragraph)

       ' create data into table
       Dim PdfTable As New PdfPTable(DataGridView1.Columns.Count)
       ' setting width of table
       PdfTable.TotalWidth = 500.0F
       PdfTable.LockedWidth = True

       Dim widths(0 To DataGridView1.Columns.Count - 1) As Single
       For i As Integer = 0 To DataGridView1.Columns.Count - 1
           widths(i) = 1.0F
       Next

       PdfTable.SetWidths(widths)
       PdfTable.HorizontalAlignment = 0
       PdfTable.SpacingBefore = 5.0F

       ' declaration pdf cells
       Dim pdfcell As PdfPCell = New PdfPCell

       ' create pdf header
       For i As Integer = 0 To DataGridView1.Columns.Count - 1

           pdfcell = New PdfPCell(New Phrase(New Chunk(DataGridView1.Columns(i).HeaderText, pTable)))
           ' alignment header table
           pdfcell.HorizontalAlignment = PdfPCell.ALIGN_LEFT
           ' add cells into pdf table
           PdfTable.AddCell(pdfcell)
       Next

       ' add data into pdf table
       For i As Integer = 0 To DataGridView1.Rows.Count - 2

           For j As Integer = 0 To DataGridView1.Columns.Count - 1
               pdfcell = New PdfPCell(New Phrase(DataGridView1(j, i).Value.ToString(), pTable))
               PdfTable.HorizontalAlignment = PdfPCell.ALIGN_LEFT
               PdfTable.AddCell(pdfcell)
           Next
       Next
       ' add pdf table into pdf document
       PdfFile.Add(PdfTable)
       PdfFile.Close() ' close all sessions

       ' show message if hasben exported
       MessageBox.Show("PDF format success exported !", "Informations", MessageBoxButtons.OK, MessageBoxIcon.Information)

   End Sub

   Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
       Me.Close()
   End Sub