Inserting document in existing document with different Page Layout
Home › Forums › Open-Xml-PowerTools › Inserting document in existing document with different Page Layout
This topic contains 6 replies, has 3 voices, and was last updated by Michaela 8 years, 1 month ago.
-
AuthorPosts
-
May 13, 2016 at 9:20 am #3390
Hi Eric,
I want to insert a document in an existing document with different Page Layout.
So I have two documents:
1.Flow.docx : has one section with different page header/footer set and the page orientation set to portrait
2.~mvi52qa2vp_landscape.docx : has one section with the page orientation set to landscape and no headers and footers are defined in this documentThe new created document should consist of two sections: one with the page orientation set to portrait and one with the page orientation set to landscape.
I adjusted the DocumentBuilder01 example as follows:
var n = DateTime.Now;
var tempDi = new DirectoryInfo(string.Format(“ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}”, n.Year – 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));
tempDi.Create();string source1 = “../../Flow.docx”;
string source2 = “../../~mvi52qa2vp_landscape.docx”;
List<Source> sources = null;Source sourceToInsert = new Source(new WmlDocument(source2), “InsertFile”);
sourceToInsert.KeepSections = true;
sourceToInsert.DiscardHeadersAndFootersInKeptSections = false;sources = new List<Source>()
{
new Source(new WmlDocument(source1), true),
sourceToInsert
};
DocumentBuilder.BuildDocument(sources, Path.Combine(tempDi.FullName, “Out3.docx”));The ‘KeepSections’ of both documents is set to ‘true’.
When I open the document ‘Out3.docx’ unfortunately, the page orientation of the insert document ~mvi52qa2vp_landscape.docx is discarded.
The documents Flow.docx, ~mvi52qa2vp_landscape.docx and Out3.docx are available on the following link: documents
Can you take a look on this issue together with the other issue of the Headers/Footers with with different page header/footer set?
Kind regards,
ThierryMay 13, 2016 at 10:16 am #3396Hi Thiery,
Right now I am buried in another project, and it is definitely proving to be one of the more difficult projects that I have taken on. (The project is comparing two DOCX files, and producing another document that contains tracked revisions.) Unfortunately, I underestimated this project, and must focus on this to the exclusion of all else until I complete it, due to contractual obligations.
I may come free in a couple of weeks, and then can research this.
Sorry for this response, but have no options regarding this currently.
-Eric
May 13, 2016 at 11:14 am #3398Hi Eric,
Thank you for your response. This is currently no blocker for the project. I have enough other things to do.
Success with your other project! Please, let me know when you start researching this issue.-Thierry
May 13, 2016 at 3:50 pm #3400If you get to the point where you are blocked, then remember it is pretty easy to open the document, find the section properties, and adjust them per your need.
We certainly want to get this issue resolved, but in the meantime it need not block you. 🙂
November 23, 2016 at 12:52 pm #3969Hi Eric,
I just started using Open Xml SDK and the DocumentBuilder. The answer of this question above would be very helpful for me. Do you have an answer in the meantime or are you still buried in your other project?
Thanks a lot in advance,
MichaelaNovember 23, 2016 at 2:33 pm #3971Hi Michaela,
I have solved this as follows:
– Get the SectionProperties of the document to insert
– Get the SectionProperties of the current section where the new document has to be inserted
– Copy the SectionProperties of current section where the new document has to be inserted if needed (determined by if the sections are not equal)
– Insert a Continues Section Break (via a LastRenderedPageBreak) with the copied SectionProperties of the current section
– Code for inserting the document with DocumentBuilder.BuildDocument
I have the document to insert declared as follows (syntax VB.NET):
Dim sourceToInsert As Source = New Source(New WmlDocument(fileName), “InsertFile”)
sourceToInsert.KeepSections = False
sourceToInsert.DiscardHeadersAndFootersInKeptSections = False
– Insert a Continues Section Break (via a LastRenderedPageBreak) with the copied SectionProperties of the document to insertI hope you can get something further.
Kind regards,
ThierryNovember 23, 2016 at 2:51 pm #3972Hi Thierry,
thanks a lot for describing me your solution and to be so fast.
It’s my first day with Open Xml SDK, DocumentBuilder and C#, but I think I found a solution right now that works for me. Maybe you can have a look at it? I just made a little change in the code of the DocumentBuilder. In DocumentBuilder.cs in function “AppendDocument” in the line that starts with “if (keepSection == false)” (It’s in line 1273 in my code) I replaced everything in this block (I mean between “{” and “}”) with the following lines:
newContent.Descendants(W.headerReference).Remove(); newContent.Descendants(W.footerReference).Remove();
My aim is that the header and footer should be the same as the previous, but all other information should stay (like format/size/orientation of page).
I don’t know if this is a good solution or if there will be other problems because of my change, but I will try to use this.
Kind regards,
Michaela -
AuthorPosts
You must be logged in to reply to this topic.