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.

@vyacheslav.deryushev this problem has been solved?
Now i have a same problem. The section.PageSetup.TopMargin of the word in Windows ,the value is 72point,but in linux is 70.85 point.

this is the error info in logfile. After i add the font simsun.ttc,the error disappears. But section.PageSetup.TopMargin is still 72point and 70.85 point.
why?
222.docx (261.5 KB)

@Melancholy This is not a defect. In your document top and bottom margins are not set explicitly in the document:

<w:sectPr>
    <w:pgSz w:w="11905" w:h="16840" />
    <w:pgMar w:right="1803" w:left="1803" w:header="708" w:footer="708" />
    <w:pgBorders>
        <w:top w:val="none" w:sz="0" w:space="0" w:color="auto" />
        <w:left w:val="none" w:sz="0" w:space="0" w:color="auto" />
        <w:bottom w:val="none" w:sz="0" w:space="0" w:color="auto" />
        <w:right w:val="none" w:sz="0" w:space="0" w:color="auto" />
    </w:pgBorders>
    <w:cols w:num="1" w:space="720" />
</w:sectPr>

In this case Aspose.Words the same as MS Word uses default margin values, which are system locale specific. Therefore if your Windows and Linux machines has different system locales, top and bottom margins values will be different.

72 point is 1 inch that corresponds the standard margin value for Canada, United States, United Kingdom, Australia and New Zealand.
70.85 point is 25mm that corresponds the standard margin for most of European locales, such as Germany, Austria, Switzerland, Poland, France and others. You can determine locale using the following code:

CultureInfo culture = Thread.CurrentThread.CurrentCulture;
return culture.LCID;

The values can be found here:
https://learn.microsoft.com/en-us/openspecs/office_standards/ms-oe376/6c085406-a698-4e12-9d4d-c3b0ee3dbc4a

@alexey.noskov I don’t understand why the default top and bottom margin values are 70.85 points when they are not manually set.When the left and right margin values aren’t manually set,the margin valuses are also 70.85 points?

@Melancholy In your attached document there are three sections. In the first section all margins are explicitly set, in the last two sections only left and right margins are explicitly set. I made the following simple test:

CultureInfo current = Thread.CurrentThread.CurrentCulture;

string[] cultureCedes = new string[] { "en-US", "da-DK", "de-DE", "uk-UA" };
foreach (string cultureCode in cultureCedes)
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureCode);
    Console.WriteLine($"---==={Thread.CurrentThread.CurrentCulture.Name}===---");
    Document doc = new Document(@"C:\Temp\in.docx");
    foreach (Section s in doc.Sections)
    {
        PageSetup ps = s.PageSetup;
        Console.WriteLine($"Left: {ps.LeftMargin}; Right: {ps.RightMargin}; Top: {ps.TopMargin}; Bottom: {ps.BottomMargin};");
    }
    Console.WriteLine("=====================================");
}

Thread.CurrentThread.CurrentCulture = current;

Here is the output:

---===en-US===---
Left: 90.15; Right: 90.15; Top: 72; Bottom: 72;
Left: 90.15; Right: 90.15; Top: 72; Bottom: 72;
Left: 90.15; Right: 90.15; Top: 72; Bottom: 72;
=====================================
---===da-DK===---
Left: 90,15; Right: 90,15; Top: 72; Bottom: 72;
Left: 90,15; Right: 90,15; Top: 85,05; Bottom: 85,05;
Left: 90,15; Right: 90,15; Top: 85,05; Bottom: 85,05;
=====================================
---===de-DE===---
Left: 90,15; Right: 90,15; Top: 72; Bottom: 72;
Left: 90,15; Right: 90,15; Top: 70,85; Bottom: 56,7;
Left: 90,15; Right: 90,15; Top: 70,85; Bottom: 56,7;
=====================================
---===uk-UA===---
Left: 90,15; Right: 90,15; Top: 72; Bottom: 72;
Left: 90,15; Right: 90,15; Top: 42,5; Bottom: 42,5;
Left: 90,15; Right: 90,15; Top: 42,5; Bottom: 42,5;
=====================================

As you can see for the first section the returned margin values are the same regardless the current thread locale. The same as left and right margins in the following two sections. But the values of top and bottom margins in the last two sections are locale dependent because they are not set explicitly.

Do you see the different picture on your side?

@alexey.noskov My computer system is win11,Now the Settings > Time & language > Language & region,the Country or region now is Setting Germany.

When i used office 2019 open the word named 222.docx, top and bottom margin values are 72 points,but used the aspose, top and bottom margin values are 70.85 points,why?

@Melancholy MS Word takes the default value from Normal.dotm template. That is why you see 72 points as default margin. MS Word default page margins depends on system locale during generation of the Normal template.