Hello,
I absolutely love this product and have been trying to get familiar with it in the past few weeks. I have run across a problem. I want to generate a PDF file in landscape with a header which i create with the xGraphics below, then i want to list a table which
could be more than 40 rows.
In the following code the table is generated and the items are listed, when the items go over 40 rows the code creates a new page (which isn't landscape) and the entire table goes on the second page.
What I would like is for the table to be on the first page and if it goes longer (say 50 rows) the last ten rows are put on the next page and the header row is on the new page.
Thanks in advance!
!Kevin
Code:
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Data
Imports System.Configuration
Imports System.Globalization
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports PdfSharp
Imports PdfSharp.Drawing
Imports PdfSharp.Drawing.Layout
Imports PdfSharp.Pdf.PdfPage
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.DocumentObjectModel.Internals
Imports MigraDoc.DocumentObjectModel.Visitors
Imports MigraDoc.DocumentObjectModel.IO
Imports MigraDoc.DocumentObjectModel.Tables
Imports MigraDoc.DocumentObjectModel.Shapes
Imports MigraDoc.Rendering
Imports MigraDoc.Rendering.PdfDocumentRenderer
Imports MigraDoc.RtfRendering
Imports MigraDoc
Imports global_functions
Partial Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strRecords As String = ""
Dim strRecordValues As String = ""
Dim strRecordHeader As String = ""
Dim strScheme As String = Request.QueryString("scheme")
Dim strCertNum As String = Request.QueryString("certnum")
Dim oPDFDocument As PdfDocument
Dim oPDFPage As PdfPage
Dim oXGraphics As XGraphics
Dim oXTextFormatter As XTextFormatter
Dim sString As String
Dim oMemoryStream As MemoryStream
'Dim logopath As XGraphicsPath
'Dim imglogo As XImage
Dim imgOcna As XImage
Dim imgJasanz As XImage
Dim xCoord As Integer = 0
Dim yCoord As Integer = 0
Dim strServer As String = Request.ServerVariables("SERVER_NAME")
Dim strFilePath As String = Request.PhysicalPath
Dim strFolder As String = Request.MapPath("/ocna/images/")
' Create fonts
Dim oX8_ArialBold As XFont = New XFont("Arial", 8, XFontStyle.Bold)
Dim oX14_ArialBold As XFont = New XFont("Arial", 14, XFontStyle.Bold)
Dim oX6_Verdana As XFont = New XFont("Verdana", 6, XFontStyle.Regular)
Dim oX6_VerdanaBold As XFont = New XFont("Verdana", 6, XFontStyle.Bold)
Dim oX8_Verdana As XFont = New XFont("Verdana", 8, XFontStyle.Regular)
Dim oX8_VerdanaBold As XFont = New XFont("Verdana", 8, XFontStyle.Bold)
Dim oX8_VerdanaItalic As XFont = New XFont("Verdana", 8, XFontStyle.Italic)
Dim oX10_Verdana As XFont = New XFont("Verdana", 10, XFontStyle.Regular)
Dim oX10_VerdanaBold As XFont = New XFont("Verdana", 10, XFontStyle.Bold)
Dim oX10_VerdanaItalic As XFont = New XFont("Verdana", 10, XFontStyle.Italic)
Dim oX12_Verdana As XFont = New XFont("Verdana", 12, XFontStyle.Regular)
Dim oX12_VerdanaBold As XFont = New XFont("Verdana", 12, XFontStyle.Bold)
Dim oX12_VerdanaItalic As XFont = New XFont("Verdana", 12, XFontStyle.Italic)
Dim oX14_Verdana As XFont = New XFont("Verdana", 14, XFontStyle.Regular)
Dim oX14_VerdanaBold As XFont = New XFont("Verdana", 14, XFontStyle.Bold)
Dim oX14_VerdanaItalic As XFont = New XFont("Verdana", 14, XFontStyle.Italic)
Dim oX8_Times As XFont = New XFont("Times New Roman", 8, XFontStyle.Regular)
Dim oX8_TimesBold As XFont = New XFont("Times New Roman", 8, XFontStyle.Bold)
Dim oX8_TimesItalic As XFont = New XFont("Times New Roman", 8, XFontStyle.Italic)
Dim oX10_Times As XFont = New XFont("Times New Roman", 10, XFontStyle.Regular)
Dim oX10_TimesBold As XFont = New XFont("Times New Roman", 10, XFontStyle.Bold)
Dim oX10_TimesItalic As XFont = New XFont("Times New Roman", 10, XFontStyle.Italic)
Dim oX12_Times As XFont = New XFont("Times New Roman", 12, XFontStyle.Regular)
Dim oX12_TimesBold As XFont = New XFont("Times New Roman", 12, XFontStyle.Bold)
Dim oX12_TimesItalic As XFont = New XFont("Times New Roman", 12, XFontStyle.Italic)
Dim oX16_Times As XFont = New XFont("Times New Roman", 16, XFontStyle.Regular)
Dim oX16_TimesBold As XFont = New XFont("Times New Roman", 16, XFontStyle.Bold)
Dim oX16_TimesItalic As XFont = New XFont("Times New Roman", 16, XFontStyle.Italic)
Dim oX16_TimesBoldItalic As XFont = New XFont("Times New Roman", 16, XFontStyle.BoldItalic)
Dim oX20_Times As XFont = New XFont("Times New Roman", 20, XFontStyle.Regular)
Dim oX20_TimesBold As XFont = New XFont("Times New Roman", 20, XFontStyle.Bold)
Dim oX20_TimesItalic As XFont = New XFont("Times New Roman", 20, XFontStyle.Italic)
Dim oX26_Times As XFont = New XFont("Times New Roman", 26, XFontStyle.Regular)
Dim oX26_TimesBold As XFont = New XFont("Times New Roman", 26, XFontStyle.Bold)
Dim oX26_TimesItalic As XFont = New XFont("Times New Roman", 26, XFontStyle.Italic)
'XImage image = XImage.FromFile(jpegSamplePath);
'imglogo = XImage.FromFile(strFolder & "logo.jpg")
imgOcna = XImage.FromFile(strFolder & "logo_OCNA.gif")
imgJasanz = XImage.FromFile(strFolder & "logo_JASANZ.gif")
oPDFDocument = New PdfDocument() 'create a new PDF document
oPDFPage = oPDFDocument.AddPage 'create a new page in the document
oPDFPage.Orientation = PageOrientation.Landscape
oXGraphics = XGraphics.FromPdfPage(oPDFPage) 'get an XGraphics object from the page for drawing
Dim maxPageWidth As Integer = oPDFPage.Width
Dim maxPageHeight As Integer = oPDFPage.Height
Dim strProductFields As String ' variable used to concatenate all fields used in query
'Dim strSchemeFields As String
Dim strRequiredFields As String
Dim strCommonFields As String
Dim strSpecificFields As String ' fields specific to each scheme
Dim strLogoImage As XImage = Nothing
Dim strCertImage As String
Dim strSheduleImage As String
Dim strDocumentType As String = ""
Dim qryLicense As String
Dim qryProducts As String
Dim qryStandards As String
Dim iPageNum As Integer = 1
Dim strConn As String = ConfigurationManager.ConnectionStrings("OCNA_ConnectionString").ConnectionString
Dim dbConn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(strConn)
dbConn.Open()
Dim blnRecordsExist As Boolean = False
Dim drProducts As SqlDataReader
Dim cmdSelect As New SqlCommand
cmdSelect.Connection = dbConn
cmdSelect.CommandType = CommandType.Text
' make scheme type uppercase
strScheme = UCase(strScheme)
'strCommonFields = ",[Application],[Actuating Temperature],[Classification],[Energy Source],[Flow Rate],[Inlet],[Outlet],[Inlet/Outlet],[Material],[Max Operating Pressure],[Min Operating Pressure],[Nominal Flow Rate],[Nominal Size],[Rated Capacity],[Set Temperature]"
' depending on the scheme type, set all variables specific to each scheme
Select Case strScheme
Case "GMK"
strDocumentType = "Certificate"
strRequiredFields = "[Distributor],[Brand Name],[Model ID],[Product Description],[Application]"
'strCommonFields = ""
strSpecificFields = ",[Model Name],[Classification],[Energy Source],[Input],[Gas Type],[Annual Energy Cons (MJ/Year)],[Star Rating],[Star Shading Degrees],[Rated Capacity],[Nominal Flow Rate],[Flow Rate],[Pressure],[Min Operating Pressure],[Max Operating Pressure],[Temperature],[Set Temperature],[Actuating Temperature],[Burners],[Electric Elements],[Flame Tamer],[Flame Supervision],[Trivet Type],[Ignition],[Side Burner],[Oven/Hood Type],[Options],[Variations],[Features],[Refrigerator/Freezer],[Nominal Size],[Inlet/Outlet],[Inlet],[Material],[Date Endorsed]"
strLogoImage = XImage.FromFile(strFolder & "logo_GMK.jpg")
strCertImage = "/ocna/images/button_GMK_Certificate.jpg"
strSheduleImage = "/ocna/images/button_GMK_Schedule.jpg"
Case "OMK"
strDocumentType = "Certificate"
strRequiredFields = "[Brand Name],[Model Name],[Model ID],[Product Type],[Product Description],[Application]"
'strCommonFields = ""
strSpecificFields = ",[Nominal Size],[Inlet/Outlet],[Inlet],[Outlet],[End Connection],[Classification],[Nominal Pressure],[Min Operating Pressure],[Max Operating Pressure],[Set Pressure],[Pressure Classification],[Max Working Pressure],[Max Operating Temperature],[Set Temperature],[Actuating Temperature],[Nominated Max Temp Delivery],[Rated Capacity],[Nominal Flow Rate],[Flow Rate],[Energy Source],[Supplementary Energy Source],[System Performance (Annual Energy Savings %) Z1],[System Performance (Annual Energy Savings %) Z2],[System Performance (Annual Energy Savings %) Z3],[System Performance (Annual Energy Savings %) Z4],[Material],[Operating Mechanism],[Number of Collectors],[Container Designation],[Collector Designation]"
strLogoImage = XImage.FromFile(strFolder & "logo_OMK.jpg")
strCertImage = "/ocna/images/button_OMK_Certificate.jpg"
strSheduleImage = "/ocna/images/button_OMK_Schedule.jpg"
Case "WMK"
strDocumentType = "Licence"
strRequiredFields = "[Brand Name],[Model Name],[Model ID],[Product Type],[Product Description],[Application]"
'strCommonFields = ",[Application],[Actuating Temperature],[Classification],[Energy Source],[Flow Rate],[Inlet],[Outlet],[Inlet/Outlet],[Material],[Max Operating Pressure],[Min Operating Pressure],[Nominal Flow Rate],[Nominal Size],[Rated Capacity],[Set Temperature]"
strSpecificFields = ",[Nominal Size],[Inlet/Outlet],[Inlet],[Outlet],[End Connection],[Trap Designation],[Classification],[Nominal Pressure],[Min Operating Pressure],[Max Operating Pressure],[Set Pressure],[Pressure Classification],[Max Working Pressure],[Max Operating Temperature],[Set Temperature],[Actuating Temperature],[Nominated Max Temp Delivery],[Rated Capacity],[Nominal Flow Rate],[Flush Volume],[Flow Rate],[Energy Source],[Material],[Operating Mechanism],[Water Shut Off Design],[Matching Pan],[Matching Cistern]"
strLogoImage = XImage.FromFile(strFolder & "logo_WMK.gif")
strCertImage = "/ocna/images/button_WMK_Certificate.jpg"
strSheduleImage = "/ocna/images/button_WMK_Schedule.jpg"
Case Else
strDocumentType = "Licence"
strRequiredFields = "[Brand Name],[Model ID],[Product Description]"
'strCommonFields = ""
strSpecificFields = ""
End Select
strCommonFields = ""
strProductFields = strRequiredFields & strCommonFields & strSpecificFields
qryLicense = "SELECT PLD_License.* , tbl_country.country" & _
" FROM PLD_License LEFT JOIN tbl_country" & _
" ON PLD_License.country_id = tbl_country.country_id" & _
" WHERE certnum = '" & strCertNum & "'"
qryStandards = "SELECT tbl_standard.standard, tbl_standard.title" & _
" FROM PLD_Standards INNER JOIN tbl_standard" & _
" ON PLD_Standards.standard_id = tbl_standard.standard_id" & _
" WHERE certnum = '" & strCertNum & "'"
'qryProducts = "SELECT " & strProductFields & " FROM PLD_Products INNER JOIN tbl_category" & _
'" ON PLD_Products.category_id = tbl_category.category_id" & _
'" WHERE certnum = '" & strCertNum & "'"
qryProducts = "SELECT " & strProductFields & " FROM PLD_Products" & _
" WHERE certnum = '" & strCertNum & "'"
cmdSelect.CommandText = qryProducts
'Response.Write("qryProducts: " & qryProducts)
drProducts = cmdSelect.ExecuteReader
''address header
oXGraphics.DrawString(strHeader, oX14_ArialBold, XBrushes.Black, New XRect(0, 10, oPDFPage.Width.Point, 10), XStringFormats.Center)
oXGraphics.DrawString(strAddress, oX8_ArialBold, XBrushes.Black, New XRect(0, 15, oPDFPage.Width.Point, 20), XStringFormats.Center)
'********************************************************************************************
Dim tableDoc As New Document()
Dim tableSec As Section = tableDoc.AddSection()
tableSec.PageSetup.Orientation = DocumentObjectModel.Orientation.Landscape
Dim tableList As MigraDoc.DocumentObjectModel.Tables.Table = tableSec.AddTable()
Dim tableCol As Column
Dim tableCell As Cell
Dim tableRow As Row
Dim rowCounter As Integer
tableList.Borders.Width = 0
'********************************************************************************************
If drProducts.HasRows Then
Dim iFieldCount As Integer
'Dim strColor As String
Dim str_RowColor As String = "#ffffff"
Dim str_AltRowColor As String = "#eeeeee"
' put all field names where the value is not empty into an array
iFieldCount = drProducts.FieldCount
' variable used to contain recordset field names
Dim strFieldNames As String = ""
Dim iCol As Integer
Dim arrFieldNames As Array
Dim fldName As Integer
Dim fldNames As Integer
Dim fldValue As String
' get field names from 'drProducts' datareader
drProducts.Read()
For iCol = 0 To iFieldCount - 1
'response.Write fld.name
' if the field value is not empty, add the field name and a comma to the 'strFieldNames' string
If drProducts(iCol).ToString <> "" Then
strFieldNames = strFieldNames & drProducts.GetName(iCol).ToString & ","
End If
Next
' remove comma from end of 'strFieldNames' string
strFieldNames = Left(strFieldNames, Len(strFieldNames) - 1)
' put field names from 'strFieldNames' into an array
arrFieldNames = Split(strFieldNames, ",")
'Response.Write("<BR>strFieldNames: " & strFieldNames)
'Response.End()
'********************************************************************************************
rowCounter = 0
For fldNames = LBound(arrFieldNames) To UBound(arrFieldNames)
tableCol = tableList.AddColumn()
Next
tableCol.Format.Alignment = ParagraphAlignment.Center
tableRow = tableList.AddRow()
tableRow.Shading.Color = Colors.PaleGoldenrod
tableRow.HeadingFormat = True
'********************************************************************************************
For fldName = LBound(arrFieldNames) To UBound(arrFieldNames)
'strRecordHeader &= arrFieldNames(fldName) & vbTab
'********************************************************************************************
strRecordHeader = arrFieldNames(fldName)
tableCell = tableRow.Cells(rowCounter)
tableCell.Format.Font.Name = "Verdana"
tableCell.Format.Font.Size = "8"
tableCell.Format.Font.Bold = True
tableCell.AddParagraph(strRecordHeader)
rowCounter = rowCounter + 1
'********************************************************************************************
Next
' close and open the datareader again because the first row was used to check for empty values
drProducts.Close()
drProducts = cmdSelect.ExecuteReader
While drProducts.Read
rowCounter = 0
tableRow = tableList.AddRow()
For Each fldValue In arrFieldNames
strRecordValues = drProducts(fldValue)
tableCell = tableRow.Cells(rowCounter)
tableCell.Format.Font.Name = "Verdana"
tableCell.Format.Font.Size = "8"
tableCell.AddParagraph(strRecordValues)
rowCounter = rowCounter + 1
Next
End While
drProducts.Close()
cmdSelect.Dispose()
dbConn.Close()
Dim render As New MigraDoc.Rendering.DocumentRenderer(tableDoc)
render.PrepareDocument()
For i As Integer = 0 To render.FormattedDocument.PageCount - 1
oPDFPage = oPDFDocument.AddPage()
oPDFPage.Orientation = PageOrientation.Landscape
render.RenderPage(PdfSharp.Drawing.XGraphics.FromPdfPage(oPDFPage), i + 1)
Next
End If
'display the document
oMemoryStream = New MemoryStream
oPDFDocument.Save(oMemoryStream, False)
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", oMemoryStream.Length.ToString())
Response.AddHeader("content-disposition", "attachment; filename=" & strCertNum & ".pdf")
Response.BinaryWrite(oMemoryStream.ToArray())
Response.Flush()
oMemoryStream.Close()
Response.End()
oXGraphics = Nothing
oPDFPage = Nothing
oPDFDocument = Nothing
End Sub
End Class