We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Orientation changed after inserting header into empty document

Hi Aspose team,

we noticed an issue in Aspose.NET with the inserting of a header file. If the document, you want to add the header to, is in horizontal format and the document is empty (no text chars; so documents with only an image also count as empty for this scenario, a space key will fix it as workaround :wink: ) the orientation will be changed to vertical.

Example code:

            var doc = new Document(@"C:\Some\Path\source.docx");

            var builder = new DocumentBuilder(doc);

            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
            var header = builder.CurrentSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
            if (header == null)
            {
                header = new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
                builder.CurrentSection.HeadersFooters.Add(header);
            }
            builder.InsertDocument(new Document(@"C:\Some\Path\header.docx"), ImportFormatMode.KeepSourceFormatting);

            doc.Save(@"C:\Some\Path\edited.docx");

(Yay, finally using markdown :heart_eyes_cat:)

If source.docx is empty, the result will look like this:
image.png (48.1 KB)

If you add content to the source document, it will stay in horizontal orientation, as expected:
image.png (47.5 KB)

I attached you example documents for content, no content and images as source and a simple header file.

Maybe this also be releated to https://forum.aspose.com/t/20615 (WORDSNET-15030)?

Kind regards,
Daniel

documents.zip (77.0 KB)

Hi Daniel,

Thanks for your inquiry. Please note both documents have different orientation, so you are getting the issue. Please change header document orientation as per empty document as following, it will resolve the issue.

var doc = new Document(@"D:\Temp\documents\no_content.docx");

var builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
var header = builder.CurrentSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
if (header == null)
{
    header = new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
    builder.CurrentSection.HeadersFooters.Add(header);
}
Document doc1=new Document(@"D:\Temp\documents\header.docx");
doc1.FirstSection.PageSetup.Orientation=doc.FirstSection.PageSetup.Orientation;
builder.InsertDocument(doc1, ImportFormatMode.KeepSourceFormatting);

doc.Save(@"E:\Data\edited.docx");

Best Regards,

Tilal Ahmad
Aspose - File Format APIs

Keep in touch! We’re on Twitter and Facebook

1 Like

Hi Tilal,

thanks for that workaround.

Just got another question while testing: I noticed a little thing in documents with mixed orientation. The header always seems to align as in first page. So having a horizontal page and a vertical afterwards ends up with this:
image.png (20.7 KB)
image.png (17.1 KB)

(other way round the same). But I guess this is nothing that easy to change, as MS Office seemed to behave exactly the same if I manually did this.

Kind regards,
Daniel

Hi Daniel,

Thanks for your feedback. It is good to know the suggested solution worked for you.

In reference to header alignment with mixed page orientation, It is not a bug. Aspose.Words mimics same behavior as MS Word does.

Best Regards,