Field of type FieldUserAddress empty in footer after converting word to pdf

When converting a word file to pdf, some fields in the footer are not saved correctly, but the same field type is correct in the document body.

  • Converting a word docm file to pdf using Aspose Words method Save with argument “filename”, no SaveOptions object passed to method.
    wordDocument.Save(…\targetFileName.pdf);
  • The document contains fields of type FieldUserName, FieldUserAddress
  • The field UserInformation is null, not loaded from Office
  • All the fields all have value before saving, I checked with Debugger.
  • The fields are saved correctly in the document body, but the fields are empty in the footer.
  • Fields of Type FieldDocProperty and others are saved correctly in footer.
  • We currently use version 25.5.0, updating to version 25.9.0 didn’t solve the issue
  • Using another tool like “Microsoft Print to pdf” from Word saves the fields correctly
  • Setting the field UserInformation or setting the field to IsLocked = true also saves the fields correctly.

Why is there difference between the document body and the footer.

@sme

The issue you’re experiencing with the FieldUserAddress being empty in the footer after converting a Word document to PDF using Aspose.Words may be related to how certain fields are processed during the conversion. It appears that the UserInformation for the footer fields is not being populated correctly, while it works fine in the document body. This discrepancy can occur due to how Aspose.Words handles field updates and rendering in different sections of the document.

Here are some suggestions to troubleshoot and potentially resolve the issue:

  1. Update Fields Before Saving: Ensure that all fields are updated before saving the document. You can do this by calling document.UpdateFields(); before the save operation. This will refresh all fields in the document, including those in the footer.

    wordDocument.UpdateFields();
    wordDocument.Save("targetFileName.pdf");
    
  2. Locking Fields: If applicable, consider setting the footer fields to IsLocked = true. This can help in ensuring that the fields are saved correctly.

  3. Check Field Codes: Verify that the field codes in the footer are correctly set up. Issues can arise from incorrect field codes or formatting.

  4. Render-Stage Approach: If the problem persists, consider rendering the document to a different format first (like XPS) and then converting that to PDF. This can sometimes help in preserving the field values.

If these suggestions do not resolve the issue, it may be beneficial to reach out to Aspose support with a sample document that reproduces the problem for further assistance.

@sme Could you please attach your problematic input and output documents along with simple code that will allow us to reproduce the problem? We will check the issue and provide you more information.

KB09086.docx (34,7 KB)

KB09086.PDF (76,2 KB)

Thank you. I uploade sample files (The original files use more fields, but the problem also occurs in this sample file)

The word file we use is a .dotm which i could not upload per your forum policies.

Code :

 public static void ConvertWordToPdf(string sourceFile, string targetFile)
 {
     if (!targetFile.ToLower().EndsWith(".pdf"))
     {
         throw new ArgumentException($"{nameof(targetFile)} muss in '.pdf' enden", targetFile);
     }

     var currentDirectory = KLIBLibrary.IO.Directory.AssemblyDirectory;
     if (!Hyphenation.IsDictionaryRegistered("de-CH") && !string.IsNullOrEmpty(currentDirectory))
     {
         string hyphenationDictionaryPath = Path.Combine(currentDirectory, @"dictionaries\hyph_de_CH.dic");
         if (File.Exists(hyphenationDictionaryPath))
         {
             Hyphenation.RegisterDictionary("de-CH", hyphenationDictionaryPath);
         }
     }

     var document = new Aspose.Words.Document(sourceFile);

     document.Save(targetFile);
 }

@sme Thank you for additional information. As I can see PDF generated by Aspose.Words looks the same as PDF produced by MS Word.
Aspose.Words: out.pdf (75.7 KB)
MS Word: ms.pdf (143.1 KB)

Could you please provide the expected output you would like to get?

@alexey.noskov Thank you for your fast reply.

The Value of the field UserAddress is :“Meine Adresse”
In the body of the document, this value is there in both files, but in the footer of the file KB09086.pdf, printed with the above code, the value “Meine Adresse” is not in the footer.

I have a workaround by locking the field values, but I still wonder why this problem is only in the footer (I haven’tt checked the header).
And as mentioned in my first post, it seems to work correctly with all other fields we use.

Additional information: The original word file we use is .dotm file with macros.

KB09086.PDF (78,8 KB)

KB09086_PrintedFromMSWord.pdf (564,3 KB)

@sme Fields in the documents header/footer are updated automatically upon rendering documents. That is why both MS Word and Aspose.Words update USERADDERESS field in the document footer. Since there is no values defined for this field empty string is shown. You can avoid updating USERADDERESS fields to preserve their values:

Document doc = new Document(@"C:\Temp\in.docx");

foreach (Field f in doc.Range.Fields)
{
    if (f.Type == FieldType.FieldUserAddress)
        f.IsLocked = true;
}

doc.Save(@"C:\Temp\out.pdf");

@alexey.noskov Thank you, I will this suggestion.

1 Like