In my application I have an option to apply a stationary to a document and save the file to PDF (or Word). I use the following code for this:
-------------
Public Shared Function MergeDocuments(WORDofPDF As String, contentDoc As Aspose.Words.Document)
Try
If File.Exists(System.Web.HttpContext.Current.Server.MapPath("templates" & WORDofPDF.ToLower & ".docx")) Then
Dim builderA As New DocumentBuilder(contentDoc)
Dim templateDoc As Document
templateDoc = New Document(System.Web.HttpContext.Current.Server.MapPath("templates" & WORDofPDF & ".docx"))
'Loop through all sections in the templateDoc document
For Each sectTemplate As Section In templateDoc.Sections
'Dim sectContent As Section = Nothing
Dim sectTemplateIndex As Integer = templateDoc.Sections.IndexOf(sectTemplate)
'Dim i As Integer
For Each sectContent As Section In contentDoc.Sections
'Remove existing headers and footers
'sectContent.ClearHeadersFooters()
'Loop throught all Headers/Footers in the templateDoc document
For Each hfB As HeaderFooter In sectTemplate.HeadersFooters
'Check whether current section from contentDoc contains
'Header/Footer with the same type as h/f from templateDoc
If sectContent.HeadersFooters(hfB.HeaderFooterType) IsNot Nothing Then
'Append content from h/f templateDoc to h/f contentDoc
For Each childB As Node In hfB.ChildNodes
'Import node
Dim childA As Node = contentDoc.ImportNode(childB, True, ImportFormatMode.KeepDifferentStyles)
'Appent node to h/f
sectContent.HeadersFooters(hfB.HeaderFooterType).AppendChild(childA)
Next
Else
'Copy whole h/f
Dim hfA As Node = contentDoc.ImportNode(hfB, True, ImportFormatMode.KeepDifferentStyles)
'Insert h/f
sectContent.HeadersFooters.Add(hfA)
End If
Next
Next
Next
contentDoc.RemoveUnusedResources()
'Save output document
Return contentDoc
Else
contentDoc.RemoveUnusedResources()
'Save output document
Return contentDoc
End If
Catch ex As Exception
'Save output document
Return contentDoc
End Try
End Function
-------------
I have one document with the stationary in it (templateDoc), this is basically a blank document with only an image in the header that spans the whole page (see attachment). Then there is one document with the actual content (contentDoc). I apply the header/footer from the templateDoc to the contentDoc. This works great, but one of my clients uses different stationary for the first page of their documents.
What I would like to do is the following: Have one templateDoc with two pages. The first page has the stationary for the first page, the second for all the other pages. If a document only has one page then it should use only the first page from the template. If the document has multiple sections, then it should still only use the stationary from the first page for the first page and the second page for the rest.
Could you tell me how I need to change the code in order to do this?