wordpress hit counter
Re: Merging Docx files using .Net - WordprocessingML - Formats - OpenXML Developer

Re: Merging Docx files using .Net

Formats

Discussions about working with different Open XML Formats

Merging Docx files using .Net

  • This post has 19 Replies |
  • 2 Followers
  • HI All

    I want to know about any Open Source Open XML API for .Net like the one that exists for JAVA.

    As of Now i am facing a problem where I want to copy content of one docx file to another docx file programmatically.

    -Saurav

     

  • Sysytem.IO.Packaging is an open source API to open a docx package,work with the xml files and pack it back.

    You can check out the articals in the library section of this site,and also these links

    http://msdn.microsoft.com/msdnmag/issues/06/11/BasicInstincts/default.aspx

    http://blogs.msdn.com/erikaehrli/archive/2006/08/16/word2007DataDocumentGenerationPart2.aspx

  • None of these articles actually touch the point that how to merge two docx file.

    Please advice me a way to copy content of one docx file to naother docx file programmatically.

     

     

  • Take a look at http://officeopenxml.sourceforge.net/

    I recommend getting the the daily build from SVN as the last released version is rather old.

  • I took the latest build from the link

    there is an error in the latest build when you do a 'Create Test Doc'

    Also 'show all pictures' returnd 0 images when there are images.

    Anyways, Can this project help me merging two files.

    Can you please elaborate more on this?

     

  • Yes, it could help you. But I thought of another thing that might be easier. Read up a bit on altChunks and then try to use an empty document containing two altChunks for the files you want to merge. That should work.

  • Hi,

    Those were the links for you to understand how packaging API can be used to work with the documents programatically,which was your first question.

    To just copy contents of one file to another use 

    File.Copy(@"C:\File2.docx", @"C:\File1.docx");

    To merge files

    You can use XSLT to merge two document,as source and destination file are xml files.

    or,using system.packaging.io:

    1. Open the source file(File1.docx)

    Package package1 = Package.Open(@"C:\File1.docx", FileMode.Open, FileAccess.ReadWrite);

    2. Load 'document.xml' on to an xml document,say 'doc1'

    Uri documentUri;

    foreach (System.IO.Packaging.PackageRelationship relationship in package1.GetRelationshipsByType(documentRelationshipType))

    {

       documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);

       documentPart1 = package1.GetPart(documentUri);// This id document.xml

    }

    XmlDocument doc1 = new XmlDocument();

    doc1.Load(documentPart1.GetStream());

    3.Similarly Open the second file(File2.docx),and load 'document.xml' on to an xml document ,'doc2'

    Package package2 = Package.Open(@"C:\File2.docx", FileMode.Open, FileAccess.ReadWrite);

    PackagePart documentPart2=null;

    foreach (System.IO.Packaging.PackageRelationship relationship in package2.GetRelationshipsByType(documentRelationshipType))

    {

    documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);

    documentPart2 = package2.GetPart(documentUri);

    }

    XmlDocument doc2 = new XmlDocument();

    4. To copy from 'File2.docx' into File1.docx'(Merge two documents),

    XmlNode oNodeWhereInsert = doc1.SelectSingleNode("//w:sectPr",nsmanager);

    //Copy all the paragraphs along with the child nodes of each of the paragraph

    foreach (XmlNode oNode in doc2.SelectNodes("//w:p", nsmanager))

    {

    oNodeWhereInsert.AppendChild(doc1.ImportNode(oNode, true));

    }

    doc1.Save(documentPart1.GetStream ());

    7.close the package.

    This merges the contents of 'File1.docx' at the end of  'File2.docx'.

  • arnesten:
    Yes, it could help you. But I thought of another thing that might be easier. Read up a bit on altChunks and then try to use an empty document containing two altChunks for the files you want to merge. That should work.

    this is not working and neither is the the best choice As i dont want the docs one after another but one docx in between of another

     

  • Vijayeta Tilak:

    Hi,

    Those were the links for you to understand how packaging API can be used to work with the documents programatically,which was your first question.

    To just copy contents of one file to another use 

    File.Copy(@"C:\File2.docx", @"C:\File1.docx");

    To merge files

    You can use XSLT to merge two document,as source and destination file are xml files.

    or,using system.packaging.io:

    1. Open the source file(File1.docx)

    Package package1 = Package.Open(@"C:\File1.docx", FileMode.Open, FileAccess.ReadWrite);

    2. Load 'document.xml' on to an xml document,say 'doc1'

    Uri documentUri;

    foreach (System.IO.Packaging.PackageRelationship relationship in package1.GetRelationshipsByType(documentRelationshipType))

    {

       documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);

       documentPart1 = package1.GetPart(documentUri);// This id document.xml

    }

    XmlDocument doc1 = new XmlDocument();

    doc1.Load(documentPart1.GetStream());

    3.Similarly Open the second file(File2.docx),and load 'document.xml' on to an xml document ,'doc2'

    Package package2 = Package.Open(@"C:\File2.docx", FileMode.Open, FileAccess.ReadWrite);

    PackagePart documentPart2=null;

    foreach (System.IO.Packaging.PackageRelationship relationship in package2.GetRelationshipsByType(documentRelationshipType))

    {

    documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);

    documentPart2 = package2.GetPart(documentUri);

    }

    XmlDocument doc2 = new XmlDocument();

    4. To copy from 'File2.docx' into File1.docx'(Merge two documents),

    XmlNode oNodeWhereInsert = doc1.SelectSingleNode("//w:sectPr",nsmanager);

    //Copy all the paragraphs along with the child nodes of each of the paragraph

    foreach (XmlNode oNode in doc2.SelectNodes("//w:p", nsmanager))

    {

    oNodeWhereInsert.AppendChild(doc1.ImportNode(oNode, true));

    }

    doc1.Save(documentPart1.GetStream ());

    7.close the package.

    This merges the contents of 'File1.docx' at the end of  'File2.docx'.

    This code works for small files. But if u have files with headers footers comments revisions etc. this will fail

     

  • You are absolutely sure its not working to include another docx file using altChunk? (sorry, I don't have the time to test it myself)

    Because if that works, and you want a document inside another document, then just place the altChunk at the place you want the document to be inserted.AltChunks can be placed at the same places that paragraphs can be placed.

    If not working, you could use the library I linked to. But it would mean a bit of work, since as you say you need to consider not only paragraphs but header, footer and styles.
  • arnesten:
    You are absolutely sure its not working to include another docx file using altChunk? (sorry, I don't have the time to test it myself)

    Because if that works, and you want a document inside another document, then just place the altChunk at the place you want the document to be inserted.AltChunks can be placed at the same places that paragraphs can be placed.

    If not working, you could use the library I linked to. But it would mean a bit of work, since as you say you need to consider not only paragraphs but header, footer and styles.

    Yes I am sure about that. I am planning to write some code for this purpose. The warstar API needs lots of work. So it might not be a good choice. Once i find a solution I will post it on this space.

  • Same issue here.

    Can some one guide me how to merge two DocX files into one?

  • There must be a simple way:

    I have 2 docx files , they are NOT the same. i.e. the COVER LETTER and the INVOICE.

    I need to merge them into ONE docx and print it.

    Can some one help me?

     

    Thanks a lot in advance...

    -Jenny

     

  • Hi,

    The contents/info of each document is stored in seperate files.

    For example, the main content will be in document.xml file( by default) and header/footer/comment will be seperate files.

    The code given by Vijeyatha will append content from one document to another document.

    Once you get the content from INVOICE, you can insert the same any where in COVER LETTER document.

    For header/footer/comment, you need to handle things seperately.

    Sheela

     

  • Hi,
    This is very difficult to copy selected contents from one document to another.

    1. While copying one needs to take care of IDs are not duplicated.
    2. Special care should be taken for Bullets & Images.

    Hemant
Page 1 of 2 (20 items) 12