Check this out http://devpinoy.org/blogs/keithrull/archive/2007/05/23/how-to-merge-multiple-microsoft-word-documents-in-c.aspx
Change this line:
object pageBreak = Word.WdBreakType.wdPageBreak;
to this
object pageBreak = Word.WdBreakType.wdSectionBreakNextPage;
To get the heading and footer from the second document.
Adding in the entire method and how to call it in case the source changes.
The Class:
using System;
using Word = Microsoft.Office.Interop.Word;
using System.Configuration;
namespace KeithRull.Utilities.OfficeInterop
{
public class MsWord
{
/// <summary>
/// This is the default Word Document Template file. I suggest that you point this to the location
/// of your Ms Office Normal.dot file which is usually located in your Ms Office Templates folder.
/// If it does not exist, what you could do is create an empty word document and save it as Normal.dot.
/// </summary>
private static string defaultWordDocumentTemplate = @"Normal.dot";
/// <summary>
/// A function that merges Microsoft Word Documents that uses the default template
/// </summary>
/// <param name="filesToMerge">An array of files that we want to merge</param>
/// <param name="outputFilename">The filename of the merged document</param>
/// <param name="insertPageBreaks">Set to true if you want to have page breaks inserted after each document</param>
public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks)
{
Merge(filesToMerge, outputFilename, insertPageBreaks, defaultWordDocumentTemplate);
}
/// <summary>
/// A function that merges Microsoft Word Documents that uses a template specified by the user
/// </summary>
/// <param name="filesToMerge">An array of files that we want to merge</param>
/// <param name="outputFilename">The filename of the merged document</param>
/// <param name="insertPageBreaks">Set to true if you want to have page breaks inserted after each document</param>
/// <param name="documentTemplate">The word document you want to use to serve as the template</param>
public static void Merge(string[] filesToMerge, string outputFilename, bool insertPageBreaks, string documentTemplate)
{
object defaultTemplate = documentTemplate;
object missing = System.Type.Missing;
object pageBreak = Word.WdBreakType.wdSectionBreakNextPage;
object outputFile = outputFilename;
// Create a new Word application
Word._Application wordApplication = new Word.Application( );
try
{
// Create a new file based on our template
Word.Document wordDocument = wordApplication.Documents.Add(
ref missing
, ref missing
, ref missing
, ref missing);
// Make a Word selection object.
Word.Selection selection = wordApplication.Selection;
//Count the number of documents to insert;
int documentCount = filesToMerge.Length;
//A counter that signals that we shoudn't insert a page break at the end of document.
int breakStop = 0;
// Loop thru each of the Word documents
foreach (string file in filesToMerge)
{
breakStop++;
// Insert the files to our template
selection.InsertFile(
file
, ref missing
, ref missing
, ref missing
, ref missing);
//Do we want page breaks added after each documents?
if (insertPageBreaks && breakStop != documentCount)
{
selection.InsertBreak(ref pageBreak);
}
}
// Save the document to it's output file.
wordDocument.SaveAs(
ref outputFile
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing
, ref missing);
// Clean up!
wordDocument = null;
}
catch (Exception ex)
{
//I didn't include a default error handler so i'm just throwing the error
throw ex;
}
finally
{
// Finally, Close our Word application
wordApplication.Quit(ref missing, ref missing, ref missing);
}
}
}
}
And the Call:
using System;
using KeithRull.Utilities.OfficeInterop;
namespace WordDocMerge2
{
class Program
{
static void Main(string[] args)
{
try
{
string document1 = @"D:\Visual Studio Projects\31.docx";
string document2 = @"D:\Visual Studio Projects\33.docx";
string document3 = @"D:\Visual Studio Projects\32.docx";
string[] documentsToMerge = { document1, document2, document3 };
string outputFileName = String.Format("D:\\Visual Studio Projects\\{0}.docx", Guid.NewGuid( ));
MsWord.Merge(documentsToMerge, outputFileName, true);
}
catch (Exception ex)
{
//messageLabel.Text = ex.Message;
}
}
}
}
You can use docx4j.NET to convert a docx to XSL FO, and from there, to PDF. Or, indeed, to any of the other output formats supported by Apache FOP.
See this sample.
docx4j.NET is an IKVM'd DLL of docx4j, an ASL v2 licensed open source project.
Best Answer
I do not have code for converting DocX to PDF, but it appears your requirement for DocX is not firm. Your last bullet says:
I read this to mean you want to be able to create a template document, fill it with data and covert it to PDF, yet allow the template to be maintained, right?
Solution: XSL-FO
XSL-FO is a W3C standard like HTML and can be transformed by a number of open source and commercial products into PDF, WordML, XPS, PS, PCL, SVG, TIFF, etc. I have used this to deliver hundreds of thousands of documents per month, both online as PDFs and offline (things like bulk check printing).
To get you started, here is the W3C page for XML-FO. There is a lot of good information there, including a list of software (both open source and commecial) down the left side. I have personally used two commercial products called IBEX PDF Creator and XEP by RenderX. Both are excellent products, and there is a 100% managed C# implementation to get to PDF called FO.NET up on CodePlex. I have not tried this, but it should satisfy your "free" criterion.
There are a number of ways you can edit the template for documents to be created in XSL-FO. Typically this template is XSLT that you apply to your XML data, but this is not a requirement. I have built these by hand, but it is a bit of a learning curve. You can start with a document in XSL-FO and fill in sections of it with code, just as you could HTML. The good news is that there are a number of XSL-FO editors out there. The bad news is that none I know of are free, but several of them are cheap and you may find something that meets the free criterion with a bit of Googling. However, one option is that you can convert from Word using a stylesheet (commercial & free).