Inconsistencies with header footers and page margins with doc and docx

Hello,

we noticed some general inconsistencies between documents when you insert headers / footers and apply page margins.

The original report we got from a customer is that the footer is different when the original document is doc compared to docx.

I also did some tests and noticed differences between
Same input doc file saved as doc / docx

The input doc vs the input docx file:

And executing the same code without an input document saving as doc and docx:

static void Main(string[] args)
{
    var lic = new License();
    lic.SetLicense(@"S:\Aspose.Total.NET.lic");

    var doc = new Document(@"S:\tmp\orig.docx");


    foreach (var node in doc.Sections)
    {
        var section = (Section)node;
        UpdatePageMargins(section.PageSetup);
    }

    var headerDoc = new Document(@"S:\tmp\header.docx");
    var footerDoc = new Document(@"S:\tmp\footer.docx");

    UpdateHeaderFooter(doc, HeaderFooterType.HeaderFirst, headerDoc);
    UpdateHeaderFooter(doc, HeaderFooterType.HeaderPrimary, headerDoc);
    UpdateHeaderFooter(doc, HeaderFooterType.HeaderEven, headerDoc);

    UpdateHeaderFooter(doc, HeaderFooterType.FooterFirst, footerDoc);
    UpdateHeaderFooter(doc, HeaderFooterType.FooterPrimary, footerDoc);
    UpdateHeaderFooter(doc, HeaderFooterType.FooterEven, footerDoc);


    doc.Save(@"S:\tmp\out_from_docx.docx");
}

static void UpdateHeaderFooter(Document doc, HeaderFooterType headerFooterType, Document headerFooterDocument)
{
    var builder = new DocumentBuilder(doc);

    builder.MoveToHeaderFooter(headerFooterType);
    var header = builder.CurrentSection.HeadersFooters[headerFooterType] ?? new HeaderFooter(doc, headerFooterType);

    headerFooterDocument.FirstSection.PageSetup.Orientation = doc.FirstSection.PageSetup.Orientation;
    builder.InsertDocument(headerFooterDocument, ImportFormatMode.KeepSourceFormatting);
    builder.CurrentParagraph.Remove();
}

static void UpdatePageMargins(PageSetup pageSetup)
{
    pageSetup.TopMargin = ConvertUtil.MillimeterToPoint(10);
    pageSetup.RightMargin = ConvertUtil.MillimeterToPoint(15);
    pageSetup.BottomMargin = ConvertUtil.MillimeterToPoint(10);
    pageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(25);

    pageSetup.HeaderDistance = ConvertUtil.MillimeterToPoint(11);
    pageSetup.FooterDistance = ConvertUtil.MillimeterToPoint(5);
}

documents:
orig.doc -> original doc file we received
orig.docx -> original docx file we received
out_from_doc.doc -> orig.doc processed and saved as doc
out_from_doc.docx -> orig.doc processed and saved as docx
out_from_docx.docx -> orig.docx processed and saved as docx
out_from_empty.doc -> no base document, processed and saved as doc
out_from_empty.docx -> no base document, processed and saved as docx

The outputs all look different in some aspects, either content is alligned different or the headers / footers are placed different or different in size and margins are different from what was configured.

files.zip (469.2 KB)

Target Framework is .Net 4.5.2
My dev system is

Edition Windows 10 Business
Version 22H2
Installiert am ‎21.‎07.‎2021
Betriebssystembuild 19045.2604
Leistung Windows Feature Experience Pack 120.2212.4190.0

@Serraniel this actually is the expected behavior; doc and doсx are different file types and have different structure, for that reason MS Word render both documents in different ways. Please make a quick test:

  1. run your code and save the document twice one as docx and the other as doc file,
  2. then use MS Word to save the docx file as a doc file,
  3. notice that the resultant (doc extension) documents will be render in the same way
    The properties of the documents (doc and docx) are the same, but MS Word render each in a different way.

out_docs.zip (293.0 KB)

Thanks for your explanation. I could validate the behaviour you mentioned you can close the thread.

1 Like