none
Insert Section Properties at the end of the document RRS feed

  • Question

  • Hi

    I want to merge three documents. After merging, I need to have different headers for few pages in the merged document. After going through the forum I came to know that inserting section properties would achieve this. 

    Below is the code I have written for merging.

    using (WordprocessingDocument mergedDoc = WordprocessingDocument.Open(outputFilename, true))
    {
       //Merge stores copy
       String altChunkIdForStoresCopy = "AltChunkId2";
       MainDocumentPart mergedDocMainPart = mergedDoc.MainDocumentPart;
       AlternativeFormatImportPart chunkForStoresCopy = mergedDocMainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkIdForStoresCopy);
       using (FileStream fileStream = File.Open(scFilePath, FileMode.Open))
       chunkForStoresCopy.FeedData(fileStream);
       AltChunk altChunkForStoresCopy = new AltChunk();
       altChunkForStoresCopy.Id = altChunkIdForStoresCopy;
       //Insert a page break
       Break pageBreakInStage = new Break();
       pageBreakInStage.Type = BreakValues.Page;
       mergedDocMainPart.Document.Body.Append(pageBreakInStage);
       mergedDocMainPart.Document.Save();
       mergedDocMainPart.Document.Body.InsertAfter(altChunkForStoresCopy, mergedDocMainPart.Document.Body.Elements<Break>().Last());
       mergedDocMainPart.Document.Save();
       mergedDoc.Close();
    }
    using (WordprocessingDocument mergedDoc = WordprocessingDocument.Open(outputFilename, true))
    {
       //Merge template
       String altChunkIdForTemplate = "AltChunkId3";
       MainDocumentPart mergedDocMainPart = mergedDoc.MainDocumentPart;
       AlternativeFormatImportPart chunkForTemplate = mergedDocMainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkIdForTemplate);
       using (FileStream fileStream = File.Open(templateFilePath, FileMode.Open))
       chunkForTemplate.FeedData(fileStream);
       AltChunk altChunkForTemplate = new AltChunk();
       altChunkForTemplate.Id = altChunkIdForTemplate;
       //Insert a page break
       Break pageBreakInStage = new Break();
       pageBreakInStage.Type = BreakValues.Page;
       mergedDocMainPart.Document.Body.Append(pageBreakInStage);
       mergedDocMainPart.Document.Save();
       mergedDocMainPart.Document.Body.InsertAfter(altChunkForTemplate, mergedDocMainPart.Document.Body.Elements<Break>().Last());
       mergedDocMainPart.Document.Save();
       mergedDoc.Close();
    }

    I am not able to insert a section property. I have also used the Open SDK 2.0 tool to get C# code for such requirement. But I was not able to understand it.

    Can any one help me on inserting section property and add header table to it??

    Thanks 

     

    Thursday, May 3, 2012 11:57 AM

Answers

All replies

  • HI Chaitanya,

    The sectionproperty is in the document. As usual, you can get it via MainDocumentPart.Document.SectionProperties. However I can't see your isse has relationship with section property.

    Best Regards,

    T.X.

    Friday, May 4, 2012 3:05 AM
  • Hi T.X.

    Thanks for the reply!!

    I guess, what you mean to say is, Section Property is not the solution for having different headers for few pages. So I would describe my requirment in brief here, please provide me a solution for this.

    I have three documents mydata.docx (4 pages), yourdata.docx (4 pages) and hisdata.docx (20 pages). I need to merge these three documents in the order mydata, yourdata, hisdata and create final.docx.

    mydata.docx, yourdata.docx have similar header as follows.

    hisdata.docx has similar header but the first cell is empty.

    I write below code for merging mydata.docx since its the first in the merge order. 

    File.Copy(mydata.docx, final.docx);

    Then I write the code as given above to merge yourdata.docx and hisdata.docx in final.docx. Also I insert a pagebreak to ensure that the merged content start from next page. The final document final.docx is correctly merged and header of mydata.docx appears on top of all pages.

    After merging the headers will have different contents for pages coming from yourdata.docx.

    First 4 pages (which came from mydata.docx) will have same header as above.

    Next 4 pages (which came from yourdata.docx) should have different header as below:

    Remaining 20 pages (which came from hisdata.docx) will have same header as the first 4 pages.

    Hope I have made it pretty simple to understand.

    I found a solution to similar problem in this forum. The solution was to manually create a docx, create header on the first page, apply a section break and start a second page, create different header in second section and disable link to previous. This wil allow you to have different header for second page onwards. Then open this document in open xml sdk tool to find out its C# code. I tried the same and came to know that section break is applied using SectionProperty and section property holds the HeaderReference for second Header object. I dont know how to disable LinkToPrevious for second header. Interop dll has facility for this as below:

    adoc.Sections[i].Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;

    So in all, I am doing R and D, trial and error to get a solution.

    Please help me.

    Thanks


    Friday, May 4, 2012 7:26 AM
  • Hi Chaitanya

    OpenXML Power Tools, has a very sophisticated class called DocumentBuilder, for combining documents in different variety of ways. you can find more information here: http://openxmldeveloper.org/wiki/w/wiki/documentbuilder.aspx

    in Addition, you can watch New Screen-Cast: Short and Sweet Intro to DocumentBuilder to see how this class can help you.

     


    Thursday, May 17, 2012 6:31 AM