I can do the page break using a DocumentBuilder.
Dim doc As Document = New Document()
Dim NewDoc As New Aspose.Words.DocumentBuilder(doc)
NewDoc.InsertBreak(BreakType.PageBreak)
I can’t figure out how to do it using the Document object. This is not actually the biggest problem though, this biggest problem is the nested repeating sections.
There are two Functions in the code. The first:
Public Function NewDoc() As Byte()
generates documents and is in production, it produces one document at a time in seperate documents. If I need 20 documents, the function gets called 20 times. These are contract documents and we store them in our database, they are tied to different contracts.
Whe doing invoices, these are not stored, if 20 invoices are to be created, I need one word document with all twenty invoices in it. I am able to create the InvoiceHeader and put a page break at the bottom of each. A single document is returned. I can get the InvoiceSupply repeating sections done. I am able to use the NewDoc() function to do this.
The function:
Public Function NewDocInvoices() As Byte()
Needs to do all of the same things NewDoc() does, but it also needs to process the nested repeating sections under the InvoiceSupply.
Below is the code from a class module that I am using to process the documents. I have commented out the section in NewDocIncoices that would deal with the nested repeating sections. Again, this works well for one repeating section, it does not for nested sections.
The problem with the code in NewDocInvoices() is that the section formatting is not copied over with text. This is currently what I am stuck on.
The byte stream for the document template comes from the database in the function GetTemplateByteStream()
SubstitutionXML is set using the contents of invoice.xml that I sent earlier.
##############################################################################
Imports System.IO
Imports System.Xml
Imports System.Data.OleDb
Imports Aspose.Words
Public Class DocumentGenerator
Public Global\_CompanyID As Integer
Public Global\_ConnectString As String
Public FileID As String
Public FileName As String
Public FileType As Integer
Public SubstitutionXML As String
Public OutputFormat As Integer = 1
Private Function GetTemplateByteStream() As MemoryStream
Dim SQL As String
Dim dt As DataTable
Dim dr As DataRow
Dim docstream As New MemoryStream
Dim conn As New OleDb.OleDbConnection(Global \ _ConnectString)
Try
SQL = "SELECT FileData, FileName, FileType, FileID FROM gmm\_file WHERE SellerID = " & Global \ _CompanyID.ToString & " "
SQL = SQL & " AND FileID = '" & FileID.ToString & "'"
SQL = SQL & " UNION ALL "
SQL = SQL & "SELECT FileDataImage as FileData, FileName, FileType, FileID FROM omm\_file WHERE SellerID = " & Global \ _CompanyID.ToString & " "
SQL = SQL & " AND FileID = '" & FileID.ToString & "'"
conn.Open()
Dim adapter As New OleDb.OleDbDataAdapter(SQL, conn)
Dim cmdBuilder As New OleDbCommandBuilder(adapter)
dt = New DataTable("documents")
adapter.Fill(dt)
dr = dt.Rows(0)
Dim srcDocBytes As Byte() = dr("FileData")
FileName = dr("FileName").ToString
FileType = dr("FileType")
docstream.Write(srcDocBytes.Clone, 0, srcDocBytes.Length)
docstream.Position = 0
GetTemplateByteStream = docstream
Catch ex As Exception
Throw ex
Finally
conn.Close()
End Try
End Function
Public Function NewDocInvoices() As Byte()
Try
Dim docstream As New MemoryStream
Dim oStream As New MemoryStream
Dim objXMLDoc As System.Xml.XmlDocument
Dim objNodes As System.Xml.XmlNodeList
Dim objNode As System.Xml.XmlNode
Dim objChildNodes As System.Xml.XmlNodeList
Dim objChildNode As System.Xml.XmlNode
Dim numChildNodes As Integer
Dim oAttributes As System.Xml.XmlAttributeCollection
Dim oAttribute As System.Xml.XmlAttribute
Dim numNodes As Integer
Dim nX As Integer
Dim numAttributes As Integer
Dim aX As Integer
Dim cX As Integer
Dim sNodeName As String
Dim sNodeValue As String
Dim iPos1 As Integer
Dim iPos2 As Integer
Dim strBlank As String
'get the template
docstream = GetTemplateByteStream()
docstream.Position = 0
Dim sourcedoc As New Aspose.Words.Document(docstream)
Dim doc As Document = New Document()
Dim NewDoc As New Aspose.Words.DocumentBuilder(doc)
'substitutions
objXMLDoc = New System.Xml.XmlDocument
Try
objXMLDoc.LoadXml(SubstitutionXML)
Catch ex As Exception
Throw ex
End Try
objNodes = objXMLDoc.FirstChild.ChildNodes
numNodes = objNodes.Count
For nX = 0 To numNodes - 1
Dim InvoiceHeaderName As String
Dim HeaderSection As Section
HeaderSection = sourcedoc.Sections(0).Clone
objNode = objNodes(nX)
oAttributes = objNode.Attributes
numAttributes = oAttributes.Count
InvoiceHeaderName = objNode.Name
For aX = 0 To numAttributes - 1
oAttribute = oAttributes(aX)
sNodeName = oAttribute.Name
sNodeValue = oAttribute.Value
HeaderSection.Range.Replace("<<" & sNodeName.ToString.Trim & ">>", sNodeValue.ToString.Trim, False, False)
Next
NewDoc.Write(HeaderSection.Range.Text)
'objChildNodes = objNode.ChildNodes
'numChildNodes = objChildNodes.Count
'If numChildNodes > 0 Then
' Dim section As Section
' Dim clonesection As Section
' Dim sSectionName As String
' Dim numSections As Integer
' Dim sX As Integer
' Dim oSection As Section
' Dim sTemp As String
' For cX = 0 To numChildNodes - 1
' objChildNode = objChildNodes(cX)
' sSectionName = objChildNode.Name
' numSections = sourcedoc.Sections.Count
' For sX = 0 To numSections - 1
' oSection = sourcedoc.Sections(sX)
' sTemp = oSection.Range.Text
' If sTemp.Contains("<<" & sSectionName.ToString & "\_") Then
' section = sourcedoc.Sections(sX)
' clonesection = section.Clone
' oAttributes = objChildNode.Attributes
' numAttributes = oAttributes.Count
' For aX = 0 To numAttributes - 1
' oAttribute = oAttributes(aX)
' sNodeName = oAttribute.Name
' sNodeValue = oAttribute.Value
' clonesection.Range.Replace("<>", sNodeValue.ToString.Trim, False, False)
' Next
' sTemp = clonesection.Range.Text
' Do While sTemp.Contains("<>") And sTemp.Contains("\_")
' iPos1 = sTemp.IndexOf("<<")
' iPos2 = sTemp.IndexOf(">>")
' strBlank = sTemp.Substring(iPos1, (iPos2 + 2) - iPos1)
' clonesection.Range.Replace(strBlank.ToString.Trim, "", False, False)
' sTemp = sTemp.Substring(0, iPos1) & Space((iPos2 + 2) - iPos1) & sTemp.Substring(iPos2 + 2)
' Loop
' sourcedoc.Sections.Insert(sX, clonesection)
' clonesection = Nothing
' section = Nothing
' Exit For
' End If
' Next
' Next
' numSections = sourcedoc.Sections.Count
' For sX = numSections - 1 To 0 Step -1
' oSection = sourcedoc.Sections(sX)
' sTemp = oSection.Range.Text
' If sTemp.Contains("<>") And sTemp.Contains("\_") Then
' Do While sTemp.Contains("<>") And sTemp.Contains("\_")
' iPos1 = sTemp.IndexOf("<<")
' iPos2 = sTemp.IndexOf(">>")
' strBlank = sTemp.Substring(iPos1, (iPos2 + 2) - iPos1)
' sourcedoc.Sections(sX).Range.Replace(strBlank.ToString.Trim, "", False, False)
' sTemp = sTemp.Substring(0, iPos1) & Space((iPos2 + 2) - iPos1) & sTemp.Substring(iPos2 + 2)
' Loop
' 'section = sourcedoc.Sections(sX)
' 'sourcedoc.Sections.Remove(section)
' End If
' Next
'End If
If numNodes > 1 And nX < numNodes - 1 Then
'add a page break
NewDoc.InsertBreak(BreakType.PageBreak)
End If
Next
Select Case OutputFormat
Case 1
Call doc.Save(oStream, SaveFormat.Doc)
Case 2
Call doc.Save(oStream, SaveFormat.AsposePdf)
End Select
NewDocInvoices = oStream.ToArray
Catch ex As Exception
Throw ex
End Try
End Function
Public Function NewDoc() As Byte()
Try
Dim docstream As New MemoryStream
Dim oStream As New MemoryStream
Dim objXMLDoc As System.Xml.XmlDocument
Dim objNodes As System.Xml.XmlNodeList
Dim objNode As System.Xml.XmlNode
Dim objChildNodes As System.Xml.XmlNodeList
Dim objChildNode As System.Xml.XmlNode
Dim numChildNodes As Integer
Dim oAttributes As System.Xml.XmlAttributeCollection
Dim oAttribute As System.Xml.XmlAttribute
Dim numNodes As Integer
Dim nX As Integer
Dim numAttributes As Integer
Dim aX As Integer
Dim cX As Integer
Dim sNodeName As String
Dim sNodeValue As String
Dim iPos1 As Integer
Dim iPos2 As Integer
Dim strBlank As String
'get the template
docstream = GetTemplateByteStream()
docstream.Position = 0
Dim doc As New Aspose.Words.Document(docstream)
'substitutions
objXMLDoc = New System.Xml.XmlDocument
Try
objXMLDoc.LoadXml(SubstitutionXML)
Catch ex As Exception
Throw ex
End Try
objNodes = objXMLDoc.FirstChild.ChildNodes
numNodes = objNodes.Count
For nX = 0 To numNodes - 1
objNode = objNodes(nX)
oAttributes = objNode.Attributes
numAttributes = oAttributes.Count
For aX = 0 To numAttributes - 1
oAttribute = oAttributes(aX)
sNodeName = oAttribute.Name
sNodeValue = oAttribute.Value
doc.Range.Replace("<<" & sNodeName.ToString.Trim & ">>", sNodeValue.ToString.Trim, False, False)
Next
objChildNodes = objNode.ChildNodes
numChildNodes = objChildNodes.Count
If numChildNodes > 0 Then
Dim section As Section
Dim clonesection As Section
Dim sSectionName As String
Dim numSections As Integer
Dim sX As Integer
Dim oSection As Section
Dim sTemp As String
For cX = 0 To numChildNodes - 1
objChildNode = objChildNodes(cX)
sSectionName = objChildNode.Name
numSections = doc.Sections.Count
For sX = 0 To numSections - 1
oSection = doc.Sections(sX)
sTemp = oSection.Range.Text
If sTemp.Contains("<<" & sSectionName.ToString & "\_") Then
section = doc.Sections(sX)
clonesection = section.Clone
oAttributes = objChildNode.Attributes
numAttributes = oAttributes.Count
For aX = 0 To numAttributes - 1
oAttribute = oAttributes(aX)
sNodeName = oAttribute.Name
sNodeValue = oAttribute.Value
clonesection.Range.Replace("<<" & sNodeName.ToString.Trim & ">>", sNodeValue.ToString.Trim, False, False)
Next
sTemp = clonesection.Range.Text
Do While sTemp.Contains("<<") And sTemp.Contains(">>") And sTemp.Contains("\_")
iPos1 = sTemp.IndexOf("<<")
iPos2 = sTemp.IndexOf(">>")
strBlank = sTemp.Substring(iPos1, (iPos2 + 2) - iPos1)
clonesection.Range.Replace(strBlank.ToString.Trim, "", False, False)
sTemp = sTemp.Substring(0, iPos1) & Space((iPos2 + 2) - iPos1) & sTemp.Substring(iPos2 + 2)
Loop
doc.Sections.Insert(sX, clonesection)
clonesection = Nothing
section = Nothing
Exit For
End If
Next
Next
numSections = doc.Sections.Count
For sX = numSections - 1 To 0 Step -1
oSection = doc.Sections(sX)
sTemp = oSection.Range.Text
If sTemp.Contains("<<") And sTemp.Contains(">>") And sTemp.Contains("\_") Then
Do While sTemp.Contains("<<") And sTemp.Contains(">>") And sTemp.Contains("\_")
iPos1 = sTemp.IndexOf("<<")
iPos2 = sTemp.IndexOf(">>")
strBlank = sTemp.Substring(iPos1, (iPos2 + 2) - iPos1)
doc.Sections(sX).Range.Replace(strBlank.ToString.Trim, "", False, False)
sTemp = sTemp.Substring(0, iPos1) & Space((iPos2 + 2) - iPos1) & sTemp.Substring(iPos2 + 2)
Loop
'section = doc.Sections(sX)
'doc.Sections.Remove(section)
End If
Next
End If
Next
Select Case OutputFormat
Case 1
Call doc.Save(oStream, SaveFormat.Doc)
Case 2
Call doc.Save(oStream, SaveFormat.AsposePdf)
End Select
NewDoc = oStream.ToArray
Catch ex As Exception
Throw ex
End Try
End Function
Public Sub New()
Dim license As Aspose.Words.License = New Aspose.Words.License
license.SetLicense("Aspose.EMK3.lic")
End Sub
End Class