C # convert Word to PDF

Source: Internet
Author: User

/// <Summary>
/// This function copy from Microsoft msdn
/// </Summary>
/// <Param name = "wordinputpath"> </param>
/// <Param name = "pdfoutputpath"> </param>
/// <Param name = "CommonName"> </param>
Public static void convertword2pdf (string wordinputpath, string pdfoutputpath, string CommonName)
{
Try
{
String extension = ". pdf ";

// Validate patameter
If (! Directory. exists (pdfoutputpath) {directory. createdirectory (pdfoutputpath );}
If (partition name. Trim (). Length = 0) {partition name = path. getfilenamewithoutextension (wordinputpath );}
If (! (Path. getextension (pipeline name). toupper () = pipeline extension. toupper () {pipeline name = pipeline name + pipeline extension ;}

Object paramsourcedocpath = wordinputpath;
Object parammissing = type. missing;

String paramexportfilepath = path. Combine (pdfoutputpath, parameter name );

// Create a word Application Object
Applicationclass wordapplication = new applicationclass ();
Microsoft. Office. InterOP. Word. Document worddocument = NULL;

// Set exportformat to PDF
Wdexportformat paramexportformat = wdexportformat. wdexportformatpdf;
Bool paramopenafterexport = false;
Wdexportoptimizefor paramexportoptimizefor = wdexportoptimizefor. wdexportoptimizeforprint;
Wdexportrange paramexportrange = wdexportrange. wdexportalldocument;
Int paramstartpage = 0;
Int paramendpage = 0;
Wdexportitem paramexportitem = wdexportitem. wdexportdocumentcontent;
Bool paramdid dedocprops = true;
Bool paramkeepsert = true;
Wdexportcreatebookmarks paramcreatebookmarks = wdexportcreatebookmarks. wdexportcreatewordbookmarks;
Bool paramdocstructuretags = true;
Bool parambitmapmissingfonts = true;
Bool paramuseiso19005_1 = false;

Try
{
// Open the source document.
Worddocument = wordapplication. Documents. Open (
Ref paramsourcedocpath, ref parammissing, ref parammissing,
Ref parammissing, ref parammissing, ref parammissing,
Ref parammissing, ref parammissing, ref parammissing,
Ref parammissing, ref parammissing, ref parammissing,
Ref parammissing, ref parammissing, ref parammissing,
Ref parammissing );

// Export it in the specified format.
If (worddocument! = NULL)
Worddocument. exportasfixedformat (paramexportfilepath,
Paramexportformat, paramopenafterexport,
Paramexportoptimizefor, paramexportrange, paramstartpage,
Paramendpage, paramexportitem, paramincludedocprops,
Paramkeepsert, paramcreatebookmarks, paramdocstructuretags,
Parambitmapmissingfonts, paramuseiso19005_1,
Ref parammissing );
}
Catch (exception ex)
{
Throw ex;
}
Finally
{
// Close and release the Document Object.
If (worddocument! = NULL)
{
Worddocument. Close (ref parammissing, ref parammissing, ref parammissing );
Worddocument = NULL;
}

// Quit word and release the applicationclass object.
If (wordapplication! = NULL)
{
Wordapplication. Quit (ref parammissing, ref parammissing, ref parammissing );
Wordapplication = NULL;
}

GC. Collect ();
GC. waitforpendingfinalizers ();
GC. Collect ();
GC. waitforpendingfinalizers ();
}

}
Catch (exception ex)
{
Throw ex;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.