Orientation of Document is not Changed when DocumentBuilder.InsertDocument is used using .NET

Dear all
Is there an update on this issue?
I’m having the same problem when merging a Word document which is using the landscape layout into a Word document which is using the portrait layout. The final document is only using the portrait layout and not keeping the landscape layout.

@tpalmie

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi Tahir
In the attached solution I’m inserting a word document which is using the landscape layout into the summary document. The content is inserted. However the landscape layout is not respected.
best regards,
thomasSetContentControlTest.zip (6.0 MB)

@tpalmie

In your case, we suggest you please use Document.AppendDocument method instead of DocumentBuilder.InsertDocument. Please use the following modified code to get the desired output.

string strSummary = MyDir + @"Summary.docx";
string strFileToInsert = MyDir + @"FileToInsert.docx";
string strTemplate = MyDir + @"Template.docx";

File.Copy(strTemplate, strSummary, true);
Document summaryDoc = new Document(strSummary);
if (summaryDoc != null)
{
    //DocumentBuilder documentBuilder = new DocumentBuilder(summaryDoc);
    //if (documentBuilder != null)
    {
        Document insertDoc = new Document(strFileToInsert);
        //documentBuilder.MoveToDocumentEnd();
        //documentBuilder.InsertDocument(insertDoc, ImportFormatMode.KeepSourceFormatting);
        summaryDoc.AppendDocument(insertDoc, ImportFormatMode.KeepSourceFormatting);
        summaryDoc.Save(MyDir + "20.11.docx");
    }
}

That’s working fine. Thanks for your prompt response!

Hi Tahir.
Using the AppendDocument method worked fine for a simple test where I only inserted a single landscape layout document into a portrait layout document.
Now I’m having this issue:

I’m using:
documentBuilder.Writeln();
to insert text into a word document and:
documentBuilder.Write()
to insert additional paragraphs into the word document.
I’m also using:
Document.AppendDocument();
to append content from another word document.
The final result is that all text/paragraphs which have been inserted using
documentBuilder.Writeln(); and documentBuilder.Write()
are sequentially in the document (all of them) and only then the content which was inserted using Document.AppendDocument();
shows up.

What I need is:
Text/paragraphs
Content of document 1 (could be portrait or landscape)
Text paragraphs
Conent of document 2 (could be portrait or landscape)

Any ideas how to implement that?

@tpalmie

Please insert your desired content using DocumentBuilder.Write and Writeln methods, move the cursor to the end of document and insert the document using Document.AppendDocument method.

If you still face problem, please ZIP and attach your input, problematic and expected output documents along with code example that you are using. We will investigate the issue and provide you code example according to your requirement.

Hi Tahir
In the attached example I’m:
-using documentBuilder write/writeln to insert a text/hyperlink at the end of the document
-using document.AppendDocument to append the content of a (landscape) word document
-using documentBuilder write/writeln to insert a text/hyperlink at the end of the document
-using document.AppendDocument to append the content of an e-mail

I’m having those issues:
-The 2nd heading (“2.Document 2”) looks different than the 1st heading (“1. Document 1”)
-the page width of the page with the 2nd heading (“2.Document 2”) is smaller than the page width of the page with the inserted e-mail (the last page)
-on the page with the 2nd heading (“2.Document 2”) there is a gap between “Content of file” and the real content of the e-mail

Thanks,
thomasSetContentControlTest.zip (217.0 KB)

@tpalmie

Please clear the font formatting after inserting the document to get the same font formatting.

The paper size is different in both documents. Please set the same paper size for both documents to avoid this issue.

The Document.AppendDocument inserts section break according to section start property of document’s section. Please set PageSetup.SectionStart property as SectionStart.Continuous to remove the gap between two pages.

We have modified your code example. Please check the modified code between /**modified code */.

documentBuilder.MoveToDocumentEnd();
                        
/**modified code */
PaperSize paperSize =  documentBuilder.CurrentSection.PageSetup.PaperSize;
/**modified code */


// insert heading
documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
documentBuilder.Writeln("Document 1");
documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;

//insert hyperlink
//documentBuilder.Font.Color = Color.Blue;
documentBuilder.Font.Underline = Underline.Single;
documentBuilder.InsertHyperlink("Link to document 1", "http://...", false);
documentBuilder.Font.ClearFormatting();
documentBuilder.Writeln(ControlChar.LineBreak);
documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
documentBuilder.Writeln("Content of file: ");
//documentBuilder.Writeln(ControlChar.LineBreak);

// append file content
Document insertDoc = new Document(strFileToInsert);
documentBuilder.MoveToDocumentEnd();
//documentBuilder.MoveTo(summaryDoc.LastSection.Body.LastParagraph);
//documentBuilder.InsertDocument(insertDoc, ImportFormatMode.KeepSourceFormatting);
summaryDoc.AppendDocument(insertDoc, ImportFormatMode.KeepSourceFormatting);

// insert new section and reset page layout
documentBuilder.MoveToDocumentEnd();
documentBuilder.InsertBreak(BreakType.SectionBreakNewPage);

/**modified code */
documentBuilder.PageSetup.PaperSize = paperSize;
/**modified code */
documentBuilder.PageSetup.Orientation = Orientation.Portrait;

// insert second document
//documentBuilder.ParagraphFormat.Style.Font.ClearFormatting();
/**modified code */
documentBuilder.Font.ClearFormatting(); 
documentBuilder.ParagraphFormat.ClearFormatting();
/**modified code */

// insert heading
documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
documentBuilder.Writeln("Document 2");
documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;

//insert hyperlink
//documentBuilder.Font.Color = Color.Blue;
documentBuilder.Font.Underline = Underline.Single;
documentBuilder.InsertHyperlink("Link to document 2", "http://...", false);
documentBuilder.Font.ClearFormatting();
documentBuilder.Writeln(ControlChar.LineBreak);
documentBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
documentBuilder.Writeln("Content of file: ");
//documentBuilder.Writeln(ControlChar.LineBreak);

// append file content
MailMessage message = Aspose.Email.MailMessage.Load(strEmailFile);

// convert the .EML or .MSG file into HTML - option 1: AppendDocument
int posOfLastBackslash = strEmailFile.LastIndexOf(@"\");
string strHtmlFileName = strEmailFile.Substring(0, posOfLastBackslash) + @"\_" + strEmailFile.Substring(posOfLastBackslash + 1);
strHtmlFileName = strHtmlFileName.Replace(".msg", ".htm");
message.Save(strHtmlFileName, Aspose.Email.SaveOptions.DefaultHtml);

Document emailDoc = new Document(strHtmlFileName);
/**modified code */
emailDoc.FirstSection.PageSetup.PaperSize = paperSize;
emailDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
/**modified code */
documentBuilder.MoveToDocumentEnd();
summaryDoc.AppendDocument(emailDoc, ImportFormatMode.KeepSourceFormatting);

Thanks again Tahir.
Now, I’m also inserting a PDF document. I converted the PDF into DOCX and inserted it into a Word document. But I have identified those issues:
-the page width of the inserted PDF content is smaller than the rest of the document
-the section “3. Document 3” which is inserted after the PDF content is misaligned (there’s no left- and top-border on the page)
Please see Summary.docx page 31 in the “Docs” folderSetContentControlTest.zip (9.2 MB)
.
best regards,
thomas

@tpalmie

Please get the paper size of last section of document and set it for inserted document.

Please use the same page properties of main document for new inserted document. Please set the page setup properties according to your requirement.

We have modified your code example and attached it with this post. Program.zip (2.3 KB)

Thank you Tahir
After identifying the PageSetup of the template file and setting the 4 border values:
documentBuilder.CurrentSection.PageSetup.LeftMargin = _pageSetup.LeftMargin;
documentBuilder.CurrentSection.PageSetup.RightMargin = _pageSetup.RightMargin;
documentBuilder.CurrentSection.PageSetup.TopMargin = _pageSetup.TopMargin;
documentBuilder.CurrentSection.PageSetup.BottomMargin = _pageSetup.BottomMargin;
This is working fine.
Thanks!