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