Asp. NET converting Word documents into PDF code

Source: Internet
Author: User

One, add a reference

Using Microsoft.Office.Interop.Word;

Second, the conversion method

1. Methods

 C # code replication
<summary>///Convert Word file to PDF file///</summary>//<param name= "SourcePath" > File path and file name to convert &lt ;/param>//<param name= "TargetPath" > Path and filename name of the file after conversion completed </param>//<returns> Successful return True, failed to return Fals e</returns> public static bool Wordtopdf (string SourcePath, String TargetPath){bool result = false; Wdexportformat wdexportformatpdf = wdexportformat.wdexportformatpdf;//conversion format 1.wdExportFormatPDF converted to PDF format 2.        Wdexportformatxps converted to XPS format object missing = Type.Missing;        Microsoft.Office.Interop.Word.ApplicationClass applicationclass = null;        Document document = NULL; Try{ApplicationClass = new Microsoft.Office.Interop.Word.ApplicationClass ();            Object inputfilename = sourcepath;//The file path that needs to be reformatted string outputfilename = targetpath;//the path and file name of the PDF or XPS file after conversion is complete            Wdexportformat ExportFormat = wdexportformatpdf;//The format used by the exported file bool Openafterexport = false;//After the conversion is complete open Wdexportoptimizefor wdexportoptimizeforprint = wdexportoptimizefor.wdexportoptimizeforprint;//Export Mode 1.wdExportOpti Mizeforprint export for printing, high quality, the resulting file size is large.            2.wdExportOptimizeForOnScreen Export for screen display, poor quality, the resulting file size is small.            Wdexportrange wdexportalldocument = wdexportrange.wdexportalldocument;//Export All content (enum) int from = 0;//Start Page int to = 0;//End page number Wdexportitem Wdexportdocumentcontent = wdexportitem.wdexportdocumentcontent;//Specifies whether the export process contains only            Text or markup that contains text. 1.wdExportDocumentContent: Export file is not tagged, 2. The export file has the tag bool Includedocprops = true;//Specifies whether to include the newly exported file in the document properties BOOL keepirm = true;//Wdexportcreatebookmarks wdexpOrtcreatewordbookmarks = Wdexportcreatebookmarks.wdexportcreatewordbookmarks;//1.wdexportcreatenobookmarks: Do not create bookmarks in the export file, 2.wdExportCreateHeadingBookmarks: Title and text box to create a bookmark in the exported file, 3.wdExportCreateWordBookmarks bookmarks for each word,            This includes creating a bookmark in the exported file except for all bookmarks that contain headers and footers.            bool Docstructuretags = true;            bool bitmapmissingfonts = true; BOOL Useiso19005_1 = false;//The generated document conforms to ISO 19005-1 (pdf/a) documents = ApplicationClass.Documents.Open (ref input FileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref miss            ING, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);             if (document! = NULL){document. ExportAsFixedFormat (OutputFileName, ExportFormat, Openafterexport, Wdexportoptimizeforprint, WdExportAllDocument, From, to, Wdexportdocumentcontent, Includedocprops, Keepirm, Wdexportcreatewordbookmarks, Docstructuretags,            Bitmapmissingfonts, Useiso19005_1, ref missing);        } result = true;        } catch {result = false; } finally {if (document! = NULL) {document. Close (ref missing, ref missing, ref missing); Document = NULL; if (ApplicationClass! = null) {applicationclass.quit (ref missing, ref missing, ref missing); ApplicationClass = nul L }} return result; }

2. Concise method

C # codeCopy
<summary>///Convert Word file to PDF file///</summary>//<param name= "SourcePath" > File path and file name to convert &lt ;/param>//<param name= "TargetPath" > Path and filename name of the file after conversion completed </param>//<returns> Successful return True, failed to return Fals e</returns> public static bool Wordtopdf (object SourcePath, String TargetPath){bool result = false;        Wdexportformat wdexportformatpdf = wdexportformat.wdexportformatpdf;        Object missing = Type.Missing;        Microsoft.Office.Interop.Word.ApplicationClass applicationclass = null;        Document document = NULL; Try{ApplicationClass = new Microsoft.Office.Interop.Word.ApplicationClass (); Document = ApplicationClass.Documents.Open (ref sourcepath, ref missing, ref missing, ref missing, ref missing, ref missing  , ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref            missing);             if (document! = NULL){document. ExportAsFixedFormat (TargetPath, Wdexportformatpdf, False, Wdexportoptimizefor.wdexportoptimizeforprint, Wdexportrange.wdexportalldocument, 0, 0, Wdexportitem.wdexportdocumentcontent, true, true,            Wdexportcreatebookmarks.wdexportcreatewordbookmarks, True, True, false, ref missing);        } result = true;        } catch {result = false; } finally {if (document! = NULL) {document. Close (ref missing, ref missing, ref missing); Document = NULL; if (ApplicationClass! = null) {applicationclass.quit (ref missing, ref missing, ref missing); ApplicationClass = nul L }} return result; }

Third, call

Officetopdf.wordtopdf ("D:\\1234.doc", "d:\\1234.pdf");

Asp. NET converting Word documents into PDF code

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.