Created
March 18, 2011 10:59
-
-
Save PaulStovell/875896 to your computer and use it in GitHub Desktop.
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 static class ComCleanupExtensions | |
{ | |
public static ComProxy<TResource> WithComCleanup<TResource>(this TResource resource) | |
where TResource : class | |
{ | |
return new ComProxy<TResource>(resource); | |
} | |
} |
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 ComProxy<TResource> : IDisposable where TResource : class | |
{ | |
private readonly TResource target; | |
public ComProxy(TResource target) | |
{ | |
this.target = target; | |
} | |
public TResource Resource | |
{ | |
get { return target; } | |
} | |
public void Dispose() | |
{ | |
if (target != null && Marshal.IsComObject(target)) | |
{ | |
Marshal.ReleaseComObject(target); | |
} | |
} | |
} |
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 void Convert(string sourcePath, string resultPath) | |
{ | |
object sourcePathRef = sourcePath; | |
using (var application = new Application().WithComCleanup()) | |
using (var documents = application.Resource.Documents.WithComCleanup()) | |
using (var document = documents.Resource.Open(ref sourcePathRef).WithComCleanup()) | |
{ | |
document.Resource.ExportAsFixedFormat( | |
resultPath, | |
WdExportFormat.wdExportFormatPDF, | |
From: 0, | |
To: 0, | |
IncludeDocProps: false, | |
CreateBookmarks: WdExportCreateBookmarks.wdExportCreateWordBookmarks); | |
application.Resource.Quit(SaveChanges:false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment