We use word documents quite often during work, merger of multiple Word documents into one might be required
We see first file browser which is the primary word file into which other file will be merged
We see second file browser which is the file that is going to be append to primary file
As a result of this merger we will have a one single file with both file content
We have several version of this representation in words ,using dropdown you can make a choice within the following
After clicking a generate button will launch a file with above choosen version of word , this file have content of both first and second file
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using OfficeComponent.Word;
namespace OfficeComponent.Samples
{
class AppendDocumentExample : WordExampleBase
#if WEB
, IUIExample
#endif
{
public string FirstFileName;
public string SecondFileName;
public AppendDocumentExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public AppendDocumentExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
#if WEB
ProcessForm();
#endif
if (string.IsNullOrWhiteSpace(SecondFileName))
{
ShowError("Please specify the source doc file.");
return null;
}
if (string.IsNullOrWhiteSpace(FirstFileName))
{
ShowError("Please specify the dest doc file.");
return null;
}
// Create a new instance of PdfDocument class.
WordDocument document = new WordDocument(FirstFileName);
// Open the source Word document.
WordDocument sourceDocument = new WordDocument(SecondFileName);
document.AppendDocument(sourceDocument, false);
string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
// Save the document.
document.Save(fileName, SaveAsFormat);
// Close the document.
document.Close();
return fileName;
}
public override string ActionTitle
{
get
{
return "Combine Files";
}
}
#if WEB
void ProcessForm()
{
FirstFileName = GetPostFile("FirstFile");
SecondFileName = GetPostFile("SecondFile");
}
#endif
}
}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text
Imports OfficeComponent.Word
Namespace OfficeComponent.Samples
#If WEB Then
Friend Class AppendDocumentExample
Inherits WordExampleBase
Implements IUIExample
#Else
Friend Class AppendDocumentExample
Inherits WordExampleBase
#End If
Public FirstFileName As String
Public SecondFileName As String
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
#If WEB Then
ProcessForm()
#End If
If String.IsNullOrWhiteSpace(SecondFileName) Then
ShowError("Please specify the source doc file.")
Return Nothing
End If
If String.IsNullOrWhiteSpace(FirstFileName) Then
ShowError("Please specify the dest doc file.")
Return Nothing
End If
' Create a new instance of PdfDocument class.
Dim document As New WordDocument(FirstFileName)
' Open the source Word document.
Dim sourceDocument As New WordDocument(SecondFileName)
document.AppendDocument(sourceDocument, False)
Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
' Save the document.
document.Save(fileName, SaveAsFormat)
' Close the document.
document.Close()
Return fileName
End Function
Public Overrides ReadOnly Property ActionTitle() As String
Get
Return "Combine Files"
End Get
End Property
#If WEB Then
Private Sub ProcessForm()
FirstFileName = GetPostFile("FirstFile")
SecondFileName = GetPostFile("SecondFile")
End Sub
#End If
End Class
End Namespace