"Source" Word to PDF V1.0.1 small software for novice reference

Source: Internet
Author: User

Yesterday, a friend asked me to help find a word to PDF software, today I make a point of the results of packaging a helper for everyone to use ~

Open Source Address: Https://github.com/dunitian/WordConvertPDF

Software Download: https://github.com/dunitian/WordConvertPDF/tree/master/Bin

Encapsulates a helper class for everyone to call:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks;        Using microsoft.office.interop.word;using system.io;namespace wordconvertpdf{public static class Wordtopdfhelper { <summary>///Word conversion to PDF (single file conversion recommended)///</summary>//<param name= "INPUTP Ath "> Load full path </param>//<param name=" OutputPath "> Save full path </param>//<param name=" STA Rtpage "> Initial page number (default = First [0]) </param>//<param name=" endpage "> End page number (default = Last page) </param> public static bool Wordtopdf (string InputPath, string outputPath, int startpage = 0, int endpage = 0) {bool B            = true;            #region Initialization//initialization of a application application Wordapplication = new application ();            Initializes a document document worddocument = NULL; #endregion #region Parameter Settings ~ ~ I'm going to kill the baby ~ ~ (the so-called parameters are based on this method: ExportasfixedformAT)//word Path object wordpath = Path.GetFullPath (InputPath);            Output path String pdfpath = Path.GetFullPath (OutputPath);            The export format is PDF Wdexportformat Wdexportformat = wdexportformat.wdexportformatpdf;            Export Large file Wdexportoptimizefor wdexportoptimizefor = Wdexportoptimizefor.wdexportoptimizeforprint;            Export the entire document Wdexportrange Wdexportrange = wdexportrange.wdexportalldocument;            Start Page number int startIndex = StartPage;            End page number int endIndex = EndPage;            Export documents without tags (this can be changed) wdexportitem Wdexportitem = wdexportitem.wdexportdocumentcontent;            Contains the word attribute bool Includedocprops = true;            Export bookmarks Wdexportcreatebookmarks paramcreatebookmarks = Wdexportcreatebookmarks.wdexportcreatewordbookmarks;            Default value of Object parammissing = Type.Missing; #endregion #region Conversion TRY {//open Word worddocument = WordApplication.Documents.Open (ref wordpath, ref para Mmissing, 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); Converts to the specified format if (worddocument! = null) {Worddocument.exportasfixedformat (PD Fpath, Wdexportformat, False, Wdexportoptimizefor, Wdexportrange, StartIndex, EndIndex, Wdexportitem, Includedocprops,                True, Paramcreatebookmarks, True, True, False, ref parammissing);            }} catch (Exception ex) {b = false;                    } finally {//closes if (worddocument! = null) { Worddocument.close (ref parammissing, ref ParammissING, ref parammissing);                Worddocument = null; }//Exit if (wordapplication! = null) {Wordapplication.quit                    (ref parammissing, ref parammissing, ref parammissing);                Wordapplication = null;            }} return B; #endregion}///<summary>//Convert Word to PDF (bulk file conversion recommended)///</summary>//&L T;param name= "InputPath" > File full path </param>//<param name= "OutputPath" > Save path </param> publi            c static int Wordstopdfs (string[] inputpaths, string outputPath) {int count = 0;            #region Initialization//initialization of a application application Wordapplication = new application ();            Initializes a document document worddocument = NULL;            #endregion//Default Value object parammissing = Type.Missing; for (int i = 0; i < inputpaths.length;                i++) {#region parameter settings ~ ~ I'm going to kill the baby ~ ~ (so-called parameters are based on this method: ExportAsFixedFormat)//word Path                Object Wordpath = Path.GetFullPath (Inputpaths[i]);                Gets the file name string outputname = Path.getfilenamewithoutextension (Inputpaths[i]);                Output path String pdfpath = Path.GetFullPath (OutputPath + @ "\" + Outputname + ". pdf");                The export format is PDF Wdexportformat Wdexportformat = wdexportformat.wdexportformatpdf;                Export Large file Wdexportoptimizefor wdexportoptimizefor = Wdexportoptimizefor.wdexportoptimizeforprint;                Export the entire document Wdexportrange Wdexportrange = wdexportrange.wdexportalldocument;                Start Page number int startIndex = 0;                End page number int endIndex = 0;      Export documents without tags (this can be changed) wdexportitem Wdexportitem = wdexportitem.wdexportdocumentcontent;          Contains the word attribute bool Includedocprops = true;                                            Export bookmarks Wdexportcreatebookmarks paramcreatebookmarks = Wdexportcreatebookmarks.wdexportcreatewordbookmarks;                #endregion #region Convert Try {//Open Word worddocument = WordApplication.Documents.Open (ref wordpath, ref P Arammissing, ref parammissing, ref parammissing, ref parammissing, ref parammissing, ref parammissing, ref parammissing, R EF parammissing, ref parammissing, ref parammissing, ref parammissing, ref parammissing, ref parammissing, ref Parammissin                    g, ref parammissing); Convert to specified format if (worddocument! = null) {WORDDOCUMENT.EXPORTASFI Xedformat (Pdfpath, Wdexportformat, False, Wdexportoptimizefor, Wdexportrange, StartIndex, EndIndex, Wdexportitem, Includedocprops, True, paramcreatEbookmarks, True, True, False, ref parammissing);                } count++;                    } catch (Exception ex) {} finally { Close if (worddocument! = null) {Worddocument.close (                        Ref parammissing, ref parammissing, ref parammissing);                    Worddocument = null; }}}//Exit if (wordapplication! = null) {WORDAP Plication.                Quit (ref parammissing, ref parammissing, ref parammissing);            Wordapplication = null;                } return count;        #endregion} #region Other///<summary>//Word converted to PDF (with diary)///</summary> <param name= "InputPath" > Load full path </param>///<param Name= "OutputPath" > Save full path </param>//<param name= "Log" > Conversion diary </param>//<param name= "startpage" > Initial page number (default = First [0]) </pa ram>//<param name= "endpage" > End page number (default = Last page) </param> public static void Wordtopdfcreatelog (ST Ring InputPath, String outputPath, out string log, int startpage = 0, int endpage = 0) {log = "Success"            ;            #region Initialization//initialization of a application application Wordapplication = new application ();            Initializes a document document worddocument = NULL; #endregion #region Parameter Settings ~ ~ I'm going to kill the baby ~ ~//word Path Object wordpath = Path.GetFullPath (InputPath)            ;            Output path String pdfpath = Path.GetFullPath (OutputPath);            The export format is PDF Wdexportformat Wdexportformat = wdexportformat.wdexportformatpdf;            Export Large file Wdexportoptimizefor wdexportoptimizefor = Wdexportoptimizefor.wdexportoptimizeforprint;    Export the entire document        Wdexportrange wdexportrange = wdexportrange.wdexportalldocument;            Start Page number int startIndex = StartPage;            End page number int endIndex = EndPage;            Export documents without tags (this can be changed) wdexportitem Wdexportitem = wdexportitem.wdexportdocumentcontent;            Contains the word attribute bool Includedocprops = true;            Export bookmarks Wdexportcreatebookmarks paramcreatebookmarks = Wdexportcreatebookmarks.wdexportcreatewordbookmarks;            Default value of Object parammissing = Type.Missing; #endregion #region Convert try {//open Word worddocument = WordApp Lication.  Documents.Open (ref Wordpath, 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);  Converts to the specified format if (worddocument! = null) {Worddocument.exportasfixedformat ( Pdfpath, Wdexportformat, False, Wdexportoptimizefor, Wdexportrange, StartIndex, EndIndex, Wdexportitem,                Includedocprops, True, Paramcreatebookmarks, True, True, False, ref parammissing); }} catch (Exception ex) {if (ex! = null) {log = ex. ToString ();                }} finally {//close if (worddocument! = null)                    {Worddocument.close (ref parammissing, ref parammissing, ref parammissing);                Worddocument = null; }//Exit if (wordapplication! = null) {Wordapplication.quit                    (ref parammissing, ref parammissing, ref parammissing);                Wordapplication = null; } GC.                Collect (); Gc. WaitForpendingfinalizers (); Gc.                Collect (); Gc.            WaitForPendingFinalizers (); } #endregion} #endregion}}

  

"Source" Word to PDF V1.0.1 small software for novice reference

Related Article

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.