Question on insertField method for Document Builder


#1

In the header of my document is the “real page number” Field but in the footer is a “relative” page number that we want to set by using ASPOSE to insert a complex field code that will add an integer to the page number.

That “number” that we add to the page number is going to be variable.

So how can I use ASPOSE to insert such a field?

This code gets the PAGE number in quite easily but how do I add “13” to the page number?

Dim builder As New Aspose.Words.DocumentBuilder(doc)

builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary)

builder.InsertField("PAGE", "")

When you look at the field in Word it should look something like this:

{={PAGE} + 13}

The way I got it into Word was using control-F9 to insert the field brackets

{}

and then inside of those brackets I pressed control-F9 again which gave me

{{}}

I could then type in

{={PAGE}+13}

and it works when I print the Word document or use ASPOSE PDF to turn the document into a PDF file.

I have a Word Document with the "proper" field inserted into the footer of the document but I could not find a place to attach it to this post.

Thanks


#2

Hi,

Thank you for your request. To attach the document, please click the Options tab at the top of the page and then the Add/Update button below the File attachment label.


#3

Attached is the document with the proper field code in the footer.

What I am trying to figure out is how I can get ASPOSE to insert that field.

thanks


#4

Unfortunately, constructing nested fields is not available via DocumentBuilder. However, you can do the following. Run Document Explorer (a tool to view the structure of a document supplied with Aspose.Words) and open an empty document only containing the desired field. You can then build a similar structure “manually” using document nodes shown in the explorer. Add the nodes directly to the node in your destination document where you want the field to appear.


#5

I ran the documentExplorer tool and I see the "field" codes I want but I am at a loss as to how to actually add them

I tried

builder.Write(Aspose.Words.ControlChar.FieldStartChar & "=" & Aspose.Words.ControlChar.FieldStartChar & "PAGE" & Aspose.Words.ControlChar.FieldSeparatorChar & "1" & Aspose.Words.ControlChar.FieldEndChar & " + 13" & Aspose.Words.ControlChar.FieldSeparatorChar & "14" & Aspose.Words.ControlChar.FieldEndChar)

But that failed. I think, based on what you said that the documentBuilder does not come into play here. It feels like I am supposed to get the node that is the Footer and then somehow add this stuff to it but I do not know how.

Dim this_node As Aspose.Words.Node

this_node = New Aspose.Words.HeaderFooter(doc, Aspose.Words.HeaderFooterType.HeaderPrimary)

I have attached a document which shows the snapshot of the document explorer info I need to insert.

Any help is greatly appreciated.

thanks


#6

I think I've found a neater workaround for you. Simply create the desired PAGE field in a blank document (right in the section body, not a header or footer). Then save it for example as FieldSource.doc. Now you can use the following code to take this document as a source and copy the field into the primary footer of the first section (assuming the destination document is Field.doc):

Dim srcDoc As Document = New Document("D:\Work\FieldSource.doc")

Dim dstDoc As Document = New Document("D:\Work\Field.doc")

Dim para As Node = dstDoc.ImportNode(srcDoc.FirstSection.Body.Paragraphs(0), True)

' Remove this line if the destination document already contains something in the primary footer of the first section.

dstDoc.FirstSection.AppendChild(New HeaderFooter(dstDoc, HeaderFooterType.FooterPrimary))

dstDoc.FirstSection.HeadersFooters(HeaderFooterType.FooterPrimary).AppendChild(para)

Of course, the above code should be adjusted to the exact needs (e.g. if your destination document contains multiple sections or something). You may also want to change the text of one of the runs being copied to specify a number other than 13. Anyway, try this approach and feel free to post your further requests here.


#7

That "kind of sort of" worked but it exposed a serious problem with the asposePDF converison.

If I was just trying to create the word document with this "field" in it then I would be good. But I am then using ASPOSE to turn the document into a PDF file. When I do that the field only displays "13" no matter what. If though I open the word document it appears correctly (paging from 13 onward).

So this seems to be more a ASPOSE.PDF issue.

It looks like the conversion to PDF does not account for this "dynamic" field.

Any ideas?


#8

Our apologies, but at the moment it’s actually impossible to properly convert such complex nested fields to PDF. I presume this will be possible when we implement our field evaluation module. I missed that you needed to convert your document to PDF, otherwise I would have notified you in advance.


#9

I find these forums pretty useful to get ideas on how to solve problems so I wanted to show my solution to this for someone faced with a similar issue. Thanks for the help.

The thing that I was trying to do was to put a "folio" page number on the bottom of a PDF file created from a Word document while still retaining the "real" page numbers that Word uses. The solution was found in ASPOSE.PDF.KIT. The ASPOSE.PDF.KIT provided an easy way to stick text onto the PDF file in exactly the way I needed it to go.

The following is the code I got from the aspose.pdf.kit forum that did the trick.

If start_folio_page >= 0 Then

Dim fileInfo As Aspose.Pdf.Kit.PdfFileInfo = New Aspose.Pdf.Kit.PdfFileInfo(server_path & "\staging\" & merged_letter_name & ".pdf")

Dim input_file As String = server_path & "\staging\" & merged_letter_name & ".pdf"

Dim output_file As String = server_path & "\staging\" & merged_letter_name & "1.pdf"

Dim mendor As Aspose.Pdf.Kit.PdfFileMend = New Aspose.Pdf.Kit.PdfFileMend(input_file, output_file)

For i As Int16 = 1 To fileInfo.NumberofPages

Dim width As Single = fileInfo.GetPageWidth(i)

Dim page_number As Int16 = i + start_folio_page

Dim llx As Short = 50

Dim lly As Short = 20

Dim ury As Short = 40

mendor.AddText(New Aspose.Pdf.Kit.FormattedText("Folio " & page_number, _

New Aspose.Pdf.Kit.FontColor(255, 0, 0), Aspose.Pdf.Kit.FontStyle.TimesRoman, _

Aspose.Pdf.Kit.EncodingType.Winansi, False, 12), i, llx, lly, width, ury)

Next

mendor.Close()

End If