Created
August 8, 2013 05:11
-
-
Save danielmackay/6181650 to your computer and use it in GitHub Desktop.
PDF generator from master Word file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DocumentGenerator | |
{ | |
public void CreatePdf(string originalPath, string outputPath, IEnumerable<TextInfo> replacementText) | |
{ | |
// Create a new Microsoft Word application object | |
var word = new Application(); | |
// C# doesn't have optional arguments so we'll need a dummy value | |
object oMissing = System.Reflection.Missing.Value; | |
word.Visible = false; | |
word.ScreenUpdating = false; | |
var wordFile = new FileInfo(originalPath); | |
// Cast as Object for word Open method | |
Object filename = (Object)wordFile.FullName; | |
// Use the dummy value as a placeholder for optional arguments | |
Document doc = word.Documents.Open(ref filename, ref oMissing, | |
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, | |
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, | |
ref oMissing, ref oMissing, ref oMissing, ref oMissing); | |
doc.Activate(); | |
foreach (var text in replacementText) | |
{ | |
FindAndReplace(word, text.FindText, text.ReplaceText); | |
} | |
// TODO: Could let the user specify the save format. | |
// TODO: Should check if we are dealing with doc or docx | |
//object outputFileName = wordFile.FullName.Replace(".docx", ".pdf"); | |
string originalFileName = Path.GetFileName(originalPath); | |
string newFileName = Path.GetFileName(outputPath); | |
object outputFileName = wordFile.FullName.Replace(originalFileName, newFileName); | |
object fileFormat = WdSaveFormat.wdFormatPDF; | |
// Save document into PDF Format | |
doc.SaveAs(ref outputFileName, | |
ref fileFormat, ref oMissing, ref oMissing, | |
ref oMissing, ref oMissing, ref oMissing, ref oMissing, | |
ref oMissing, ref oMissing, ref oMissing, ref oMissing, | |
ref oMissing, ref oMissing, ref oMissing, ref oMissing); | |
// Close the Word document, but leave the Word application open. | |
// doc has to be cast to type _Document so that it will find the | |
// correct Close method. | |
object saveChanges = WdSaveOptions.wdDoNotSaveChanges; | |
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); | |
doc = null; | |
// word has to be cast to type _Application so that it will find | |
// the correct Quit method. | |
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); | |
word = null; | |
} | |
private void FindAndReplace(Application wordApp, object findText, | |
object replaceText) | |
{ | |
object matchCase = true; | |
object matchWholeWord = true; | |
object matchWildCards = false; | |
object matchSoundsLike = false; | |
object nmatchAllWordForms = false; | |
object forward = true; | |
object format = false; | |
object matchKashida = false; | |
object matchDiacritics = false; | |
object matchAlefHamza = false; | |
object matchControl = false; | |
object readOnly = false; | |
object visible = true; | |
object replace = 2; | |
object wrap = 1; | |
wordApp.Selection.Find.Execute(ref findText, | |
ref matchCase, ref matchWholeWord, | |
ref matchWildCards, ref matchSoundsLike, | |
ref nmatchAllWordForms, ref forward, | |
ref wrap, ref format, ref replaceText, | |
ref replace, ref matchKashida, | |
ref matchDiacritics, ref matchAlefHamza, | |
ref matchControl); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TextInfo | |
{ | |
public string FindText { get; private set; } | |
public string ReplaceText { get; private set; } | |
public TextInfo(string findText, string replaceText) | |
{ | |
FindText = findText; | |
ReplaceText = replaceText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment