Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Fri May 04, 2018 5:40 pm

To whom it may concern.

I have a code that added a page number to the footer of the page. The document is loaded from HTML and saved as pdf. I'd like to ignore or remove when 'Page 1 of 1' happens. My code is below. Thank you in advance.

Sub ...
Dim strHTML As String = Nothing
Dim doc As New Spire.Doc.Document
Dim sec As Spire.Doc.Section
Dim para As Spire.Doc.Documents.Paragraph

sec = doc.AddSection
sec.PageSetup.Orientation = Spire.Doc.Documents.PageOrientation.Portrait
sec.PageSetup.PageSize = Spire.Doc.Documents.PageSize.Letter
sec.PageSetup.Margins.Left = 55

If Variables.Duplex = 1 Then
AddPageNumber(doc, sec)
End If

para = sec.AddParagraph()
para.AppendHTML(strHTML)

'' --->> NOT SURE WHERE THIS CODE SHOULD BE INCLUDED
'Dim searchString As New System.Text.RegularExpressions.Regex("Page 1 of 1")
'para.Replace(searchString, String.Empty)

doc.SaveToFile(PdfFileName, Spire.Doc.FileFormat.PDF)
doc.Close()
End If
End Sub

Shared Sub AddPageNumber(ByVal doc As Spire.Doc.Document, ByVal sec As Spire.Doc.Section)
''instantiate a character format object and set the font size and family as well as the color
Dim footer As Spire.Doc.HeaderFooter
Dim footerParagraph As Spire.Doc.Documents.Paragraph
Dim text As Spire.Doc.Fields.TextRange
Dim field As Spire.Doc.Fields.Field
Dim cf As New Spire.Doc.Formatting.CharacterFormat(doc)
cf.Font = New Drawing.Font(Constance.cArial, 9)
cf.TextColor = Drawing.Color.Black

''build footer
footer = sec.HeadersFooters.Footer
''Add a paragraph - blank line
'footerParagraph = footer.AddParagraph()
''Add a paragraph - page number
footerParagraph = footer.AddParagraph()
text = footerParagraph.AppendText(" Page ")
text.ApplyCharacterFormat(cf)
field = footerParagraph.AppendField("page number", Spire.Doc.FieldType.FieldPage)
field.ApplyCharacterFormat(cf)
text = footerParagraph.AppendText(" of ")
text.ApplyCharacterFormat(cf)
field = footerParagraph.AppendField("number of pages", Spire.Doc.FieldType.FieldNumPages)
field.ApplyCharacterFormat(cf)
footerParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center
End Sub

tlerner
 
Posts: 32
Joined: Mon Aug 14, 2017 5:49 pm

Mon May 07, 2018 8:22 am

Hello,

Thanks for your inquiry.
Regarding the case, you could simply use the "Document.PageCount" to make an adjustment. If the document has more than one page, add page number, otherwise, ignore the operation. Below is the code for your reference.
Code: Select all
Dim strHTML As String = Nothing
Dim doc As Spire.Doc.Document = New Spire.Doc.Document
Dim sec As Spire.Doc.Section
Dim para As Spire.Doc.Documents.Paragraph
sec = doc.AddSection
sec.PageSetup.Orientation = Spire.Doc.Documents.PageOrientation.Portrait
sec.PageSetup.PageSize = Spire.Doc.Documents.PageSize.Letter
sec.PageSetup.Margins.Left = 55
para = sec.AddParagraph
para.AppendHTML(strHTML)
If (doc.PageCount > 1) Then
    AddPageNumber(doc, sec)
End If

doc.SaveToFile(PdfFileName, Spire.Doc.FileFormat.PDF)
doc.Close


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Mon May 07, 2018 7:58 pm

Great. Works perfectly. Thanks a lot! I was doing it in the wrong spot.

tlerner
 
Posts: 32
Joined: Mon Aug 14, 2017 5:49 pm

Tue May 08, 2018 2:12 am

Hello,

Glad to hear that.
Just feel free to contact us if you need any assistance.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc