将打印文档内容转换为pdf格式 [英] convert print document content to pdf format

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

问题描述

你好,

我正在使用vb.net2008.我想使用datagridview内容创建pdf.任何人都可以帮助我

hello,

i am using vb.net 2008. I want to create pdf with datagridview content. Any one help me

推荐答案

,如果有的话,请用ur datagridview名称替换它

nd这些是您必须使用的库
if there is any ni replace it with ur datagridview name

nd these are the libraries u have to use
Imports iTextSharp
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Imports System
Imports com.lowagie.text.pdf.PdfPTable
Imports System.Windows.Forms
Imports System.Data.Sql
Imports System.Data.SqlClient





Public Sub ExportDataToPDFTable(ByVal path As String)
       Dim count As Integer
       Dim paragraph As New Paragraph
       Dim doc As New Document(iTextSharp.text.PageSize.A4, 40, 40, 40, 10)
       Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(SaveFileDialog1.FileName + ".pdf", FileMode.Create))
       doc.Open()
       Dim font12BoldRed As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 9.0F, iTextSharp.text.Font.UNDERLINE Or iTextSharp.text.Font.BOLD, BaseColor.BLACK)
       Dim font12Bold As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 7.0F, iTextSharp.text.Font.BOLD, BaseColor.BLACK)
       Dim font12Normal As New iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.COURIER, 7.0F, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)

       Dim p1, p2 As New Phrase
       p2 = New Phrase(New Chunk(CompanyNm, font12BoldRed))
       p1 = New Phrase(New Chunk(reportname, font12BoldRed))
       ''doc.Add(p2)
       doc.Add(p1)

       ''Dim ds As New DataSet
       ''Dim qry As String = "select * from advancegridsetting where head = " + head.ToString() + " and visible = ''true'' "
       ''Dim cmd As SqlCommand = New SqlCommand(qry, mycon)
       ''Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
       ''da.Fill(ds, "advancegridsetting")
       ''If ds.Tables(0).Rows.Count = 0 Then
       ''    MessageBox.Show("Save Advance grid setting", "Desire Softech")
       ''    Exit Sub
       ''End If

       Dim PdfTable As New PdfPTable(ds.Tables(0).Rows.Count)





       Dim wid As Decimal
       Dim abs As Integer
       For l As Integer = 0 To ds.Tables(0).Rows.Count - 1
           abs = abs + 1
           wid += ds.Tables(0).Rows(l).Item("width").ToString()
       Next
       ''PdfTable.TotalWidth = 490.0F
       PdfTable.TotalWidth = wid + 50.0F

       Dim PdfPCell As New PdfPCell
       Try

           ''For Each c As DataGridViewColumn In ni.Columns
           For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
               ''If ni.Columns(i).Visible = True Then
               count = count + 1
               Dim idColumn As DataColumn = New DataColumn()
               idColumn.ColumnName = ds.Tables(0).Rows(i).Item("HeaderText")

               PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_CENTER
               PdfPCell = New PdfPCell(New Phrase(New Chunk(idColumn.ToString(), font12Bold)))
               PdfTable.AddCell(PdfPCell)

           Next

           '' PdfTable.AddCell("")

           Dim increasselen As Integer
           Dim widths(count - 1) As Single
           Dim k As Integer
           For kj As Integer = 0 To ds.Tables(0).Rows.Count - 1

               If ni.Columns(kj).HeaderText.Length >= 6 Then
                   increasselen = 40
               Else

                   increasselen = 20
               End If
               k = k + 1
               widths(k - 1) = (ds.Tables(0).Rows(kj).Item("width").ToString() + increasselen)

           Next
           PdfTable.SetTotalWidth(widths)
       Catch ex As Exception
       End Try
       '' doc.Add(PdfTable)



       Dim dt As DataTable = dtFromGrid
       If dt IsNot Nothing Then
           For rows As Integer = 0 To ni.Rows.Count - 1
               For column As Integer = 0 To ds.Tables(0).Rows.Count - 1
                   Try

                       PdfPCell = New PdfPCell(New Phrase(ni.Rows(rows).Cells(ds.Tables(0).Rows(column).Item("name").ToString()).Value.ToString(), font12Normal))

                       If column = 0 Or column = 1 Then

                           PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT
                       Else
                           PdfPCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT
                       End If

                       PdfTable.AddCell(PdfPCell)
                   Catch ex As Exception
                       PdfPCell = New PdfPCell(New Phrase("", font12Normal))
                       PdfTable.AddCell(PdfPCell)
                   End Try


               Next

           Next
           doc.Add(PdfTable)
       End If
       doc.Close()


   End Sub


这篇关于将打印文档内容转换为pdf格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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