wordpress hit counter
Re: Word 2007 cfChunk/altChunk - WordprocessingML - Formats - OpenXML Developer

Re: Word 2007 cfChunk/altChunk

Formats

Discussions about working with different Open XML Formats

Word 2007 cfChunk/altChunk

  • This post has 8 Replies |
  • 0 Followers
  • I guess I have 2 questions:

    1. In thedocumentation on msdn referring to Openxml and Word 2007, I noticed that there was a schema for the relationship cfChunk. Does that mean that I can use the 2003 cfChunk in Office 2007?

    2. I have tried to use altChunk to add an rtf file into my .docx by manipulating the xml file I added the necessary relationship, the content type, and the code in document.xml, but the file opens as corrupt. Does altChunk not work in Microsoft Office Enterprise 2007?

    I really need a way to add a .docx into another .docx file and I have tried just about everything I have found in the forums.

  • It is pretty easy to insert one docx inside another using Open Xml SDK v2 .  Here is roughly what I do to insert a docx file "indocument" right infront content control sdtBlock:

    AlternativeFormatImportPart inDocPart = doc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);

    //Open file to be inserted and stream its contents into the part
    using (FileStream stream = new FileStream(indocument, FileMode.Open))
    {
          inDocPart.FeedData(stream);
    }
    //Create a AltChunk - <w:altChunk> and give the reltionship if linking it to the
    //alternative format part we added above
    AltChunk altChunk = new AltChunk();
    altChunk.Id = doc.MainDocumentPart.GetIdOfPart(inDocPart);
    //Insert the altChunk tag just before content control
    sdtBlock.InsertBeforeSelf(altChunk);
  • Thank you so much I will try it. Do you know if there is any good documentation for this?
  • Hi thanks for the code snippet for the OpenXml sdk v2.0

    I have an unanswered question I was wondering at the bottom of the code

    where you have :

    //Insert the altChunk tag just before content control
    sdtBlock.InsertBeforeSelf(altChunk);

    How can I just insert the altChunk at the bottom of the .docx or what is sdtBlock?

  • vmoss:

    Hi thanks for the code snippet for the OpenXml sdk v2.0

    I have an unanswered question I was wondering at the bottom of the code

    where you have :

    //Insert the altChunk tag just before content control
    sdtBlock.InsertBeforeSelf(altChunk);

    How can I just insert the altChunk at the bottom of the .docx or what is sdtBlock?



    Here is how you can insert a file at the end of the document (after the last paragraph):

                string document = @"D:\TestDoc.docx";
                string indocument = @"D:\InDoc.docx";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(document, true))
                {
                    //Get the last paragraph in the document
                    Paragraph paragraph = doc.MainDocumentPart.Document.Descendants<Paragraph>().Last();
                    //Create a new Alternative Format part
                    AlternativeFormatImportPart inDocPart = doc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);

                    //Open file to be inserted and stream its contents into the part
                    using (FileStream stream = new FileStream(indocument, FileMode.Open))
                    {
                        inDocPart.FeedData(stream);
                    }
                    //Create a AltChunk - <w:altChunk> and give the reltionship if linking it to the
                    //alternative format part we added above
                    AltChunk altChunk = new AltChunk();
                    altChunk.Id = doc.MainDocumentPart.GetIdOfPart(inDocPart);
                    //Insert the altChunk tag after the paragraph
                    paragraph.InsertAfterSelf(altChunk);

                    //Save the document
                    doc.MainDocumentPart.Document.Save();
                }

  • IT WORKED!!!!!!!!!!! That's one headache out of the window!

    Thank you very much vj9999.

  • Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} Hi,


    Thanks for the code snippet. It really helped out a lot. There is one other issue involved here.

    Some parts of the merged(second) document are reformatted using the style of the first document, particularly the bulleting & numbering. I tried to fix this using quite a few options. Couple of these are mentioned below,

    Snippet 1 -
    DocumentFormat.OpenXml.Wordprocessing.Locked locked1 = doc.MainDocumentPart.StyleDefinitionsPart.Styles.Descendants<Locked>().Last();
    locked1.Val = BooleanValues.False;
    doc.MainDocumentPart.Document.Save();


    Snippet 2 -
    AltChunkProperties properties = new AltChunkProperties();
    altChunk.AltChunkProperties = properties;

    MatchSource matchSource = new MatchSource();
    matchSource.Val = BooleanValues.True;
    altChunk.AltChunkProperties.MatchSource = matchSource;


    None of these options work.

    Are there any other properties which need to be set apart from the ones mentioned above?

    Any help on this is highly appreciated.

    Thanks,
    Anand.

  • is there any way that we can apply formatting on altchunk?????
    like we apply formating on paragraph

    i am placing altchunk in sdtBlock's SdtContentBlock which accepts childs of type Paragraph so i can insert altchunk there as a child element
    but the formatting which was applied on sdtBlock vanished
    i want to keep the formatting which was applied on sdtBlock

    any suggestion?


  • Was this matter ever resolved?  I am having a hard time merging docx files and keeping the styles and more importantly the title page contents disappear.
Page 1 of 1 (9 items)