Iterating through a collection to create a multu-page PDF

Iterating through a collection to create a multu-page PDF

Forum member · Feb 26, 2008
Hi there I'm trying to create a multi-page merged PDF by iterating through the rows collection of a DataTable. However I cant seem to get the code to create a new page based on the values for that row. It just makes a document with the correct number of pages but each page containing the values from ALL rows super-imposed over each other : Here's my code: ' Create a document and set it's properties Dim MyDocument As MergeDocument MyDocument = New MergeDocument(applicationPath & "\admin\templates\08-02-letterhead.pdf", MyOptions) Dim counter As Integer = 0 For Each row As DataRow In postalRecipients.Rows Dim MyPage As ceTe.DynamicPDF.Page = MyDocument.Pages(0) MyPage.Dimensions.SetMargins(30, 30, 30, 30) Dim contactName As String = row("ContactName").ToString() Dim address1 As String = row("CustomerID").ToString() Dim address2 As String = row("CustomerID").ToString() Dim city As String = row("CustomerID").ToString() Dim region As String = row("CustomerID").ToString() Dim postalCode As String = row("CustomerID").ToString() Dim country As String = row("CustomerID").ToString() Dim certificateReference As String = row("PolicyGUID").ToString() ' Create a style Dim style As FormattedTextAreaStyle = New FormattedTextAreaStyle(FontFamily.Helvetica, 12, False) ' Add address elements to page MyPage.Elements.Add(New Label(contactName, 8, 86, 490, 12, Font.Helvetica, 10)) Dim formattedText As String = "<p>Dear " & contactName & "</p>" ' Create the text and the formatted text area formattedText &= "<p>Formatted text area provide rich formatting support for " + _ "text that appears in the document. You have complete control over 8 paragraph " + _ "properties: spacing before, spacing after, first line indentation, left indentation," + _ " right indentation, alignment, allowing orphan lines, and white space preservation; " + _ "6 font properties: <font face='Times'>font face, </font><font " + _ "pointSize='6'>font size, </font><font color='FF0000'>color, " + _ "</font><b>bold, </b><i<italic and </i><u>" + _ "underline</u>; and 2 line properties: leading, and leading type. Text can " + _ "also be rotated.</p>" Dim MyFormattedTextArea As FormattedTextArea = New FormattedTextArea(formattedText, 8, 250, 400, 400, style) ' Add the formatted text area to the page MyPage.Elements.Add(MyFormattedTextArea) MyDocument.Pages.Add(MyPage) Next Return MyDocument How do I fix this?

Re: Iterating through a collection to create a multu-page PDF

Support · Feb 26, 2008
Hello, Do you want to create a new page for every row of your datatable. If that is the case then you will have to create a new Page object in the for loop, currently in your code you are not creating a new page but you are getting the existing page from the merged PDF and you are adding the data to it. You are using the same page in the for loop and this is the reason for seeing the supre imposed data. The code should look something like this. For Each row As DataRow In postalRecipients.Rows 'create a new page object dim page as new Page(); 'add data to the page MyDocument.Pages.Add(MyPage) Next Or if you want to use the same template page but with different data then you can do this by using the PdfDocument object. Following would be the sanmple code for this. Dim pdf As New PdfDocument("C:\Temp\test.pdf")'Single page PDF Dim document As New MergeDocument() For i As Integer = 0 To 10 document.Append(pdf) document.Pages(i).Elements.Add(New Label("label" & i.ToString(), 10, 90, 100, 20)) Next document.DrawToWeb() This is an efficient way to do this. Thanks, ceTe Software Support Team.

Re: Iterating through a collection to create a multu-page PDF

Forum member · Feb 26, 2008
Hi there Even creating a new page each time the loop passes creates a document of pages superimposed with all the row's details. This doesnt seem to work: Dim MyPage As ceTe.DynamicPDF.Page For Each row As DataRow In postalRecipients.Rows MyPage = New ceTe.DynamicPDF.Page() MyPage = MyDocument.Pages(0) MyDocument.Pages.Add(MyPage) Next Return MyDocument However your other suggesstion works fine. Thank you. Stephen