private string SaveAsMhtForPdh(string docxFilePath, Core.InternalBookmarkDomain firstDomain) { object missing = System.Type.Missing; Microsoft.Office.Interop.Word.Application wApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wDoc = null; string xmlFilePath = Path.ChangeExtension(docxFilePath, FileExtension.Xml); try { // open word document wApp.Visible = false; wDoc = wApp.Documents.Open(docxFilePath, AddToRecentFiles: false); // remove custom xml part RemoveCustomXmlParts(wDoc); // put watermark tag MakePdwWatermark(wApp, TemplateType.Pdh, firstDomain); // pre-process ProcessHeaderFooter(wApp, wDoc); ProcessWaterMark(wDoc); ProcessBookmarkForMhtFile(wDoc); // save as word xml document Microsoft.Office.Interop.Word.WdSaveFormat fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFlatXML; xmlFilePath = Path.ChangeExtension(xmlFilePath, FileExtension.Mht); fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatWebArchive; wDoc.WebOptions.Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; wDoc.SaveAs(FileName: xmlFilePath, FileFormat: fileFormat, Encoding: Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8, AddToRecentFiles: false); // close document ((Microsoft.Office.Interop.Word._Document)wDoc).Close(); } catch { } finally { ((Microsoft.Office.Interop.Word._Application)wApp).Quit(missing, missing, missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc); System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp); } return(xmlFilePath); }
private string SaveAsWordXmlForPdw(string docxFilePath, Core.InternalBookmarkDomain firstDomain) { object missing = System.Type.Missing; Microsoft.Office.Interop.Word.Application wApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document wDoc = null; string xmlFilePath = Path.ChangeExtension(docxFilePath, FileExtension.Xml); try { // open word document wApp.Visible = false; wDoc = wApp.Documents.Open(docxFilePath, AddToRecentFiles: false); // remove custom xml part RemoveCustomXmlParts(wDoc); // save as word xml document Microsoft.Office.Interop.Word.WdSaveFormat fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFlatXML; MakePdwWatermark(wApp, TemplateType.Pdw, firstDomain); wDoc.SaveAs(FileName: xmlFilePath, FileFormat: fileFormat, AddToRecentFiles: false); // close document ((Microsoft.Office.Interop.Word._Document)wDoc).Close(); } catch { } finally { ((Microsoft.Office.Interop.Word._Application)wApp).Quit(missing, missing, missing); System.Runtime.InteropServices.Marshal.ReleaseComObject(wDoc); System.Runtime.InteropServices.Marshal.ReleaseComObject(wApp); } return(xmlFilePath); }
/// <summary> /// Copies a document to a specified path. /// </summary> /// <param name="document">The Document instance.</param> /// <param name="path">The path where the new file will be saved.</param> /// <param name="saveFormat">The save format.</param> /// <exception cref="IOException">When the file cannot be saved.</exception> /// <remarks>The document is saved in Unicode little endian encoding.</remarks> /// <returns>True if the operation succedes. False otherwise.</returns> public bool ShadowCopyDocument(Microsoft.Office.Interop.Word.Document document, string path, Microsoft.Office.Interop.Word.WdSaveFormat saveFormat) { DocumentCopier documentCopier = new DocumentCopier(document, path, saveFormat); documentCopier.Perform(); return(documentCopier.GetResults()); }