Page Margin Problem

Why does Aspose.Words retrieve different page margins for the same document when loaded in Windows versus Linux environments?

@Melancholy

Cause

The discrepancy in page margins when loading the same document in Windows versus Linux environments can often be attributed to differences in the underlying rendering engines or system configurations. These differences may include font availability, default printer settings, or even the way each operating system handles document layouts.

Solution

To ensure consistent page margins across different environments, you can explicitly set the margins in your code. This will override any default settings that may vary between operating systems. Here’s how you can do it:

Code

Here’s a sample code snippet to set the page margins programmatically:

Document doc = new Document("YourDocument.docx");
doc.Sections[0].PageSetup.Margins.Left = 50; // Set left margin
doc.Sections[0].PageSetup.Margins.Right = 50; // Set right margin
doc.Sections[0].PageSetup.Margins.Top = 50; // Set top margin
doc.Sections[0].PageSetup.Margins.Bottom = 50; // Set bottom margin
doc.Save("UpdatedDocument.docx");

This code sets the margins to a fixed value, ensuring that they remain consistent regardless of the operating system.

Next Steps

  1. Test the document after applying the margin settings to confirm that the issue is resolved.
  2. If the problem persists, check for any additional settings or configurations specific to the environments you are using.
  3. Consider updating to the latest version of Aspose.Words, as improvements and bug fixes may address rendering issues.

Please implement these changes and let me know if you encounter any further issues!

@Melancholy The problem on your side might occur because the fonts used in your input document are not available on the machine where document is processed. If Aspose.Words cannot find the font used in the document, the font is substituted. This might lead into fonts mismatch and document layout differences due to the different fonts metrics and as a result incorrect page numbers in the TOC. You can implement IWarningCallback to get notifications when font substitution is performed.
Please see our documentation to learn where Aspose.Words looks for fonts:

I implemented the IWarningCallback interface and captured a font-related warning:
Font warning: Font 'Times New Roman' has not been found. Using 'SimHei' font instead. Reason: font info substitution.

After installing the ‘Times New Roman’ font on the Linux server and reloading the document, the font warning no longer appears. However, I am still experiencing incorrect page margin values when retrieving them. Here the wrong file
1 (2).docx (556.8 KB) in linux section.PageSetup.BottomMargin is 70.85,in windows section.PageSetup.BottomMargin is 72

@Melancholy This is a Chinese character document, and MS Word can create different layouts for different languages and locales. Please try to download this document using:

LoadOptions loadOptions = new LoadOptions();
loadOptions.LanguagePreferences.DefaultEditingLanguage = EditingLanguage.ChinesePRC;
var doc = new Document("input.docx", loadOptions);
Aspose.Words.License license = new Aspose.Words.License();

license.SetLicense("Aspose.Words.lic");
int fileIndex = 0;

var logger = new FontWarningLogger();
LoadOptions opt = new LoadOptions();
opt.LanguagePreferences.DefaultEditingLanguage = EditingLanguage.ChinesePRC;
opt.WarningCallback = logger;        

I have used this method before, but the retrieval is still incorrect

@Melancholy Could you please share the name of the Linux you are using?

CentOS Linux

@Melancholy Thank you for the provided information. I will check it out.