wordpress hit counter
Welcome to OpenXML Developer Sign in | Join | Help

Merging Docx files using .Net

Last post 04-14-2009, 11:47 AM by krishnaramuu. 19 replies.
Page 1 of 2 (20 items)   1 2 Next >
Sort Posts: Previous Next
  •  04-09-2007, 11:33 PM 1399

    Merging Docx files using .Net

    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

     

  •  04-10-2007, 1:42 AM 1402 in reply to 1399

    Re: Merging Docx files using .Net

    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

  •  04-10-2007, 6:56 PM 1411 in reply to 1402

    Re: Merging Docx files using .Net

    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.

     

     

  •  04-11-2007, 10:16 AM 1412 in reply to 1411

    Re: Merging Docx files using .Net

    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.

  •  04-11-2007, 10:00 PM 1415 in reply to 1412

    Re: Merging Docx files using .Net

    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?

     

  •  04-12-2007, 2:42 AM 1418 in reply to 1415

    Re: Merging Docx files using .Net

    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.

  •  04-12-2007, 8:56 AM 1422 in reply to 1415

    Re: Merging Docx files using .Net

    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'.

  •  04-12-2007, 9:25 PM 1424 in reply to 1418

    Re: Merging Docx files using .Net

    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

     

  •  04-12-2007, 9:27 PM 1425 in reply to 1422

    Re: Merging Docx files using .Net

    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

     

  •  04-13-2007, 7:17 AM 1427 in reply to 1424

    Re: Merging Docx files using .Net

    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.
  •  04-20-2007, 2:15 AM 1456 in reply to 1427

    Re: Merging Docx files using .Net

    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.

  •  10-09-2007, 3:53 PM 2234 in reply to 1411

    Re: Merging Docx files using .Net

    Same issue here.

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

  •  10-09-2007, 3:57 PM 2235 in reply to 1456

    Re: Merging Docx files using .Net

    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

     

  •  10-11-2007, 1:41 AM 2239 in reply to 2235

    Re: Merging Docx files using .Net

    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

     

  •  10-15-2007, 4:45 AM 2259 in reply to 2239

    Re: Merging Docx files using .Net

    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)   1 2 Next >
View as RSS news feed in XML