Skip to content

Instantly share code, notes, and snippets.

@jonghwanhyeon
Created March 8, 2015 15:47
Show Gist options
  • Save jonghwanhyeon/3f9def11368d4c52893c to your computer and use it in GitHub Desktop.
Save jonghwanhyeon/3f9def11368d4c52893c to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
namespace Converter
{
class WordConverter : IConverter
{
private Application application;
public WordConverter()
{
this.application = new Application();
this.application.ScreenUpdating = false;
this.application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
}
~WordConverter()
{
// casting to _Application to prevent type conflicts
((_Application)this.application).Quit(WdSaveOptions.wdDoNotSaveChanges);
}
public void ToPDF(string sourceFilename, string destinationFilename)
{
Document document = this.application.Documents.Open(
FileName: sourceFilename,
ConfirmConversions: false,
ReadOnly: true,
AddToRecentFiles: false,
Revert: true
);
document.ExportAsFixedFormat(
OutputFileName: destinationFilename,
ExportFormat: WdExportFormat.wdExportFormatPDF,
OpenAfterExport: false,
OptimizeFor: WdExportOptimizeFor.wdExportOptimizeForPrint,
BitmapMissingFonts: true
);
((_Document)document).Close(WdSaveOptions.wdDoNotSaveChanges);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment