PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Print Document to PDFSharp, Code Help, VB Express https://forum.pdfsharp.net/viewtopic.php?f=2&t=2018 |
Page 1 of 1 |
Author: | PelcPete [ Fri May 11, 2012 3:37 pm ] |
Post subject: | Print Document to PDFSharp, Code Help, VB Express |
I have a large VB programme that prints many documents mainly from DataGridViews.. I would also like the option to create PDF's. Looking around the web I always get pointed to here. I was hoping that someone may be able to help me convert my Print Document code to a PDFSharp document. The main problem is the number of pages and amount of data may vary. Here is a form that will Generate a Table from a DataGridView and send it to PrintPreview. Simply add a Datagridview, Button, PrintPriviewDialog and PrintDocument to a new form and paste the code. Code: Imports System.IO
Imports System.Diagnostics Imports System.Drawing Public Class Form1 Dim mRow As Integer = 0 Dim newpage As Boolean = True Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage With DataGridView1 Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit) fmt.LineAlignment = StringAlignment.Center fmt.Trimming = StringTrimming.EllipsisCharacter 'define header room Dim y As Single = 250 Do While mRow < .RowCount Dim row As DataGridViewRow = .Rows(mRow) Dim x As Single = e.MarginBounds.Left Dim h As Single = 0 For Each cell As DataGridViewCell In row.Cells Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height) e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height) If (newpage) Then ' Header and footer go in here e.Graphics.DrawString(DataGridView1.Columns(cell.ColumnIndex).HeaderText, .Font, Brushes.Black, rc, fmt) Else ' DataGridView Loop e.Graphics.DrawString(DataGridView1.Rows(cell.RowIndex).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt) End If x += rc.Width h = Math.Max(h, rc.Height) Next 'define footer room and if new page is required newpage = False y += h mRow += 1 If y + h > 600 Then e.HasMorePages = True mRow -= 1 newpage = True Exit Sub End If Loop mRow = 0 End With End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Populate DataGridView Dim dt As New DataTable dt.Columns.Add("Number", GetType(Integer)) dt.Columns.Add("name", GetType(String)) dt.Columns.Add("address", GetType(String)) For i As Integer = 0 To 100 dt.Rows.Add(i, "Name " & i.ToString, "Address " + i.ToString) Next DataGridView1.DataSource = dt End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() End Sub End Class |
Author: | () => true [ Fri May 11, 2012 4:31 pm ] |
Post subject: | Re: Print Document to PDFSharp, Code Help, VB Express |
PelcPete wrote: I was hoping that someone may be able to help me convert my Print Document code to a PDFSharp document. Better convert your code to a MigraDoc document (MigraDoc will handle varying page numbers automatically).Look at the MigraDoc Hello MigraDoc sample to get started. http://www.pdfsharp.net/wiki/HelloMigraDoc-sample.ashx |
Author: | PelcPete [ Sat Jun 02, 2012 6:47 pm ] |
Post subject: | Re: Print Document to PDFSharp, Code Help, VB Express |
If anyone like me does struggle with the C language examples and is also new to PDFsharp I have written a small example. Add PDFSharp to refrences. Add a Datagridview. Add a Button (Named btnpdf) Paste the code below (My notes are not great, sorry ![]() Code: Imports PdfSharp Imports PdfSharp.Drawing Imports PdfSharp.Pdf Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 'Create a Database for fast testing Dim db As New DataTable db.Columns.Add("Column1", GetType(Integer)) db.Columns.Add("Column2", GetType(Integer)) db.Columns.Add("Column3", GetType(Integer)) Dim a As Integer = 50 Dim b As Integer = 100 Dim z As Integer = 0 Dim i As String = 1 For v As Integer = 1 To 100 z = z + 1 a = a + 1 b = b + 1 Dim value2 As Integer = z Dim value3 As Integer = a Dim value4 As Integer = b db.Rows.Add(value2, value3, value4) Next 'Add the auto Generated Database to our Datagridview DataGridView1.DataSource = db End Sub Private Sub btnpdf_Click(sender As System.Object, e As System.EventArgs) Handles btnpdf.Click ' Create a new PDF document Dim document As PdfDocument = New PdfDocument document.Info.Title = "Created with PDFsharp" ' Create an empty page Dim page As PdfPage = document.AddPage ' Get an XGraphics object for drawing Dim gfx As XGraphics = XGraphics.FromPdfPage(page) 'set up some fonts Dim font As XFont = New XFont("Verdana", 10, XFontStyle.Bold) Dim font2 As XFont = New XFont("Verdana", 4, XFontStyle.Bold) 'this will be used to set the text x position Dim x As Integer = 0 'this will be used to set the text y position Dim y As Integer = 33 'this will count the current row Dim row As Integer = 0 'this will count the current column Dim col As Integer = 0 'this is our maximum row setting. Dim pmax As Integer = 25 'This will count the current row on the page Dim count As Integer = 0 'starting page number Dim pagen As String = 1 'Fow every row in our datagrid For Each DataGridViewRow In DataGridView1.Rows If count = 0 Then 'header (every time we start a new page, the count is reset, becuase of this we can insert our header and any other repeated items here) 'draw the header text gfx.DrawString("Header", font, XBrushes.Black, 400, 20) 'Draw our page number gfx.DrawString("Page" & pagen, font2, XBrushes.Black, 400, 500) 'add one to page number so the page number is correct the next time we use this statement pagen = pagen + 1 End If 'Fow every row in colum in our current row datagrid. For Each DataGridViewCell In DataGridView1.Columns 'select the text using our row and colum counters Dim text As String = DataGridView1.Item(col, row).Value.ToString 'draw the text gfx.DrawString(text, font, XBrushes.Black, x, y) 'move to colum selector along one col = col + 1 'move the text along x = x + 50 Next 'Reset the text position x = 0 'Reset text colum selection col = 0 'reset our text row selection row = row + 1 'move the text down ready for the next row y = y + 20 'Add one to our Page row count count = count + 1 'if our page row count hits the maxium allowed (stated above) and there are more rows left to export. If count = pmax And row < DataGridView1.RowCount Then 'add a new page page = document.AddPage 'reset the graphics to our new page gfx = XGraphics.FromPdfPage(page) 'reset the count count = 0 'reset the text position y = 33 End If Next 'save the document Dim filename As String = "DGV Export.pdf" document.Save(filename) 'View the document Process.Start(filename) End Sub End Class What I was struggling with is not knowing that I after declaring a new page we also need to reset gfx. Was quite simple when I thought about it ![]() The rest is pretty simple VB. Code: 'add a new page
page = document.AddPage 'reset the graphics to our new page gfx = XGraphics.FromPdfPage(page) |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |