ExcelTechnology

Convert Word to PDF with C# and Avoid Error Happening during Process

Because Microsoft Word doesn’t provide us with a function to convert Word to PDF directly, so we often need to use software to realize conversion. We can find several good converters online. These converters are mainly offered for common users. While, developers may write code to complete the requirement. Of course, we can find many materials online about how to convert Word to PDF with C#. To certain degree, these materials can help developers. But developers should test practically to make sure if the methods are useful for them.
During process to use C# to convert Word to PDF, we may come across this problem that error happen when using ACRODISTLib.PdfDistillerClass. After converting one document, we can’t release it and the error happens when invoking ACRODISTLib.PdfDistillerClass again. How to solve this problem?
Firstly, we install the needed environment, for example, Microsoft Word. Please not that the printer installed is “MS Publisher Color Printer”, the default printer provided by MS. Then add references, Acrobat Distiller and Word 2003.
The following code is about the detailed method to convert Word to PDF with C#.
using oWord = Microsoft.Office.Interop.Word;
private void WordConvert()
{
oWord.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType= word.GetType();

var word = new ActiveXObject("Word.Application");
var doc = word.Documents.Open(docfile);
oWord.Documents docs = word.Documents;
Type docsType = docs.GetType();
object objDocName = @"c:test.doc";
oWord.Document doc = (oWord.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] {objDocName, true, true});
//Print it to the specified file by using Type.InvokeMember to invoke
Type docType = doc.GetType();
object printFileName = @"c:test.ps";
docType.InvokeMember("PrintOut",System.Reflection.BindingFlags.InvokeMethod,null,doc,new object[]{false,false,oWord.WdPrintOutRange.wdPrintAllDocument,printFileName});
//new object[]{false,false,oWord.WdPrintOutRange.wdPrintAllDocument,printFileName}
//Quit WORD
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,null, word, null);

object o1= "c:\test.ps";
object o2= "c:\test.pdf";
object o3= "";
//Covert PS to PDF object
try
{
ACRODISTXLib.PdfDistillerClass pdf = new ACRODISTXLib.PdfDistillerClass();
Type pdfType = pdfConvert.GetType();
pdfType.InvokeMember("FileToPDF",System.Reflection.BindingFlags.InvokeMethod,null,pdf,new object[]{o1,o2,o3});
pdf = null;
}
Finally, in order to avoid the errors happening when invoking this method, we need to end acrodist.exe process.

12.01.2011. u 08:49 | 0 Komentara | Print | # | ^

<< Arhiva >>

Creative Commons License
Ovaj blog je ustupljen pod Creative Commons licencom Imenovanje-Dijeli pod istim uvjetima.

< siječanj, 2011 >
P U S Č P S N
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            

Kolovoz 2011 (2)
Srpanj 2011 (2)
Lipanj 2011 (2)
Svibanj 2011 (2)
Travanj 2011 (3)
Ožujak 2011 (2)
Veljača 2011 (2)
Siječanj 2011 (5)
Prosinac 2010 (5)

Dnevnik.hr
Gol.hr
Zadovoljna.hr
Novaplus.hr
NovaTV.hr
DomaTV.hr
Mojamini.tv

Opis bloga

something about Excel Technology in VB.Net and C#

Linkovi

Dnevnik.hr
Video news portal Nove TV

Blog.hr
Blog servis

Igre.hr
Najbolje igre i igrice

Forum.hr
Monitor.hr