Cell Width when Table in Header or Footer

Hi
I am using Aspose Words 15.7, Visual Studio 2013, and .NET 4.5. I am using Words to generate both MS Word documents and convert the same documents to PDF.

It is when I attempt to save as PDF, that this problem occurs.

The scenario is quite complicated, so I have attached some code, rather than attempt to post a lot of sample code inline. Note that the attached code uses several functions (not included) as a shorthand for loading the Aspose licence, generate a file name, and open the document once it has been created.

The generated documents contain header/footer information that involves inserting a table into the header or footer to allow for alignment of the text (particularly on pages where the header consists of some left-aligned and some right-aligned text).
The problem is, whenever I set up the table in the header or footer, all tables generated subsequently have their cell widths set to 100% of the client width of the page, ignoring the width explicitly set for that cell. This occurs even if the tables in question are in another Section.
I can get around this problem, to a certain extent, by generating the entire document without headers or footers, and then going back and generating the headers and footers at the end of the process. This is not ideal, though, for obvious reasons.
There is a secondary problem too. The width of tables in the header or footer is always 100% of the client width of the page, even when I explicitly set the width to something else.

NOTE: This only happens when I attempt to save the document as PDF. The save as docx version works just fine.

Thanks
Chris.

Hi Chris,

Thanks for your inquiry. I have tested the scenario and have managed to reproduce the same issues at my side. Both issues are same. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-12513. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Chris,

Thanks for your patience.

Please use InsertField(FieldType.FieldPage, false) in BuildFooter method to fix the shared issue. See the highlighted code snippet.

Moreover, please note that Document.UpdateFields() call in the end of the main method is not needed right after UpdatePageLayout() call. UpdatePageLayout() will update the fields in the footer automatically.

If you want to set the same width for all columns, it is best to use fixed table layout.

private static void BuildFooter(DocumentBuilder builder, Section section)
{
    builder.MoveToSection(builder.Document.Sections.IndexOf(section));
    builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
    Table footerTable2 = builder.StartTable();
    builder.InsertCell();
    builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(100);
    builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.InsertField(FieldType.FieldPage, false);
    builder.EndRow();
    builder.EndTable();
    // Change this value to 50% to demonstrate the secondary problem.
    footerTable2.PreferredWidth = PreferredWidth.FromPercent(50);
    builder.MoveToSection(builder.Document.Sections.IndexOf(section));
}

Hi Tahir
Thanks for your reply. While that did fix my sample code, it did not fix my larger problem. The sample code was a significantly cut-down version of a much more complex process that contains proprietary code. Unfortunately, I must have cut out at least one of the important bits, as I have implemented the changes you suggested, to no avail.

One thing that struck me as curious was, that it was the updating of fields prematurely that caused this problem. To that end, I have gone back into my code, and found every place where I use the InsertField method. There are only 3 places, and I note that two of them use a different overload. Instead of the InsertField(FieldType, bool) overload, I use InsertField(string).

I can’t dig into the source code (because it’s obfuscated), but the help file says for this overload that it “Inserts a Word field into a document and updates the field result”, which I suspect is the cause of my problem. You may be able to confirm this.

Unfortunately, I think I have to use this overload, as I need to add switch to make the page number display (the fields are all page number display fields or page reference display fields) as roman numerals. To achieve this, I pass the command string “PAGE * roman” or “PAGEREF [Name] * roman \h”. In the case of PAGEREF, the field also needs to be clickable (hence the \h suffix).

My question is, is there a way to achieve this without immediately updating the field?

Thanks
Chris.

Hi Chris,

Thanks for your inquiry. Unfortunately, there no other workaround of this issue. Please use InsertField(FieldType.FieldPage, false) in BuildFooter method to fix this issue.

We had already logged a feature as WORDSNET-832 (Make table layout as close to Word as possible) which will fix this issue issue. After the availability of this feature, you can use true value in InsertField method. Unfortunately, there is no ETA available of this feature at the moment.

We suggest you please use InsertField(FieldType.FieldPage, false) in your application.

Hi Tahir

I think I may have fixed the problem, with help from your last suggestion. It was the note that I use the specific overload of InsertField in BuildFooter that did it.

I had it in my mind that fields anywhere in the document needed to be inserted this way, but it appears just the header and footer matter.

With that in mind, the following bit of code works for me, both for regular numbers, and roman numerals (which are required at certain points in the documents I am generating):

Field field = builder.InsertField(FieldType.FieldPage, false);
// If I need to make the page number show up as roman numerals…
field.Format.GeneralFormats.Add(GeneralFormat.LowercaseRoman);

I’m going to give it some more testing, but the documents seem to be generating correctly now - both in Word and PDF formats.

Thanks very much for all your help.
Chris.

Hi Chris,

Thanks for your feedback. Yes, you can achieve your requirement using one of following code snippet. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Field field = builder.InsertField(FieldType.FieldPage, false);
// If I need to make the page number show up as roman numerals...
field.Format.GeneralFormats.Add(GeneralFormat.LowercaseRoman);
builder.StartBookmark("TestBookmark");
builder.EndBookmark("TestBookmark");
builder.InsertField(@"PAGEREF TestBookmark \* roman \h", null);