PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Trouble generating PDF file with table https://forum.pdfsharp.net/viewtopic.php?f=2&t=733 |
Page 1 of 1 |
Author: | bakardi [ Sat May 16, 2009 3:55 am ] |
Post subject: | Trouble generating PDF file with table |
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 |
Author: | Thomas Hoevel [ Mon May 18, 2009 8:27 am ] |
Post subject: | |
You don't have to call AddPage if you use MigraDoc. Change the PageSetup of your section after calling AddSection and all pages in this section will be Landscape. Or change this loop "For i As Integer = 0 To render.FormattedDocument.PageCount - 1" to set Landscape on all subsequent pages, too. |
Author: | bakardi [ Mon May 18, 2009 9:05 pm ] |
Post subject: | |
Okay I seemed to have gotten the table to work fine, it currently adds the table to page 2 and goes from there. The landscape is working fine as well. The only problem now is that the table is not loading on the first page. here is an example of how the pdf file is generating: 4 Pages: 1st Page: Document Header Document Footer 2nd Page: [Table] 3rd Page: [Table] 4th Page: [Table] but here is how I want it to generate: 3 Pages: 1st Page: Document Header [Table] 2nd Page: [Table] 3rd Page: [Table] Document Footer I'm thinking it has to do with the rendering of the table occuring before the xGraphics stuff? Thanks again! |
Author: | bakardi [ Mon May 18, 2009 10:28 pm ] |
Post subject: | |
Oh and another thing, the table is positioned like maybe 50 over and 100 from the top can I change this so its centered and the text doesn't run off the page? edit: i updated the code in the first post with the newest working code... the final document that is generated looks like this: 4 Pages: 1st Page: Document Header Document Footer 2nd Page: [Start Table] 3rd Page: [Table cont.] 4th Page: [Table End] but here is how I want it to generate: 3 Pages: 1st Page: Document Header [Start Table] 2nd Page: [Table cont.] 3rd Page: [Table End] Document Footer |
Author: | Thomas Hoevel [ Tue May 19, 2009 7:41 am ] |
Post subject: | |
With MigraDoc DOM you create an object tree that represents the document. It doesn't matter when you add something, it matters where you add it. Add one section. Add Header to this section. Add Table to this section. Add Footer to this section. All parts will appear in that order. |
Author: | bakardi [ Fri May 22, 2009 9:22 pm ] |
Post subject: | |
okay thanks i have it working right now. how do I set the footer below to show only on the last page's footer? I have been able to show the footer at the bottom of all pages or right below the table on the last page. I am unable to put it at the bottom of the last page. thanks again! Code: Dim document As New Document()
Dim section As Section = document.AddSection() section.PageSetup.TopMargin = 30 section.PageSetup.BottomMargin = 30 section.PageSetup.Orientation = DocumentObjectModel.Orientation.Landscape Dim objTable As MigraDoc.DocumentObjectModel.Tables.Table = section.AddTable() Dim objTableColumn As Column Dim objTableCell As Cell Dim objTableRow As Row Dim iColumnCount As Integer objTable.Borders.Width = 0 Dim footer As Paragraph = document.LastSection.AddParagraph() footer.AddText(strSigLine & vbCrLf & strSignature & vbCrLf) footer.Format.Alignment = ParagraphAlignment.Center footer.Format.Font.Size = 8 Dim pdfRenderer As New MigraDoc.Rendering.PdfDocumentRenderer(True) pdfRenderer.Document = document pdfRenderer.RenderDocument() oMemoryStream = New MemoryStream pdfRenderer.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() |
Author: | Thomas Hoevel [ Mon May 25, 2009 7:59 am ] |
Post subject: | |
bakardi wrote: I have been able to show the footer at the bottom of all pages or right below the table on the last page. I am unable to put it at the bottom of the last page.
You call it footer, I'd call it signature. If the size of the signature is known (e.g. up to 3 lines or max. 2 cm or whatever), then you can use a textframe to give it an absolute position. Textframe and table can overlap, so it's up to you to position the textframe below the table (e. g. by using 3 cm bottom margin and placing the signature at that bottom margin). MigraDoc has footers for first page, left and right pages - but not for last page. |
Author: | bakardi [ Wed May 27, 2009 9:27 pm ] |
Post subject: | |
Code: Dim paragraph As Paragraph
Dim formattedText As FormattedText Dim textFrame As TextFrame textFrame = document.LastSection.AddTextFrame() textFrame.RelativeVertical = RelativeVertical.Page textFrame.RelativeHorizontal = RelativeHorizontal.Page textFrame.Width = 250 textFrame.Top = 562 textFrame.Left = Int(document.DefaultPageSetup.PageWidth) / 2 'textFrame.Left = document.DefaultPageSetup.PageWidth - textFrame.Width - Unit.FromMillimeter(2) paragraph = textFrame.AddParagraph() paragraph.Format.Alignment = ParagraphAlignment.Center formattedText = paragraph.AddFormattedText(strSigLine & vbCrLf & strSignature) formattedText.Font.Name = "Verdana" formattedText.Font.Size = "10" formattedText.Font.Bold = False Edit: okay figured it out... above is the code used for anyone with same problem, thanks a lot thomas!!! |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |