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

<summary>

Convert a Word file to a PDF file

</summary>

<param name= "SourcePath" > File path and file name to convert </param>

<param name= "TargetPath" > The path and filename name of the file after the conversion is complete </param>

<returns> successful return true, failed to return false</returns>

public static bool Wordtopdf (string SourcePath, String TargetPath)

{

BOOL result = FALSE;

Wdexportformat wdexportformatpdf = wdexportformat.wdexportformatpdf;//conversion format

1.wdExportFormatPDF Convert to PDF format 2.wdExportFormatXPS 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;//file path that needs to be reformatted

String outputfilename = path and file name of the PDF or XPS file after the targetpath;//conversion is complete

Wdexportformat ExportFormat = wdexportformatpdf;//The format used by the exported file

BOOL Openafterexport = false;//Open after conversion completed

Wdexportoptimizefor Wdexportoptimizeforprint =

wdexportoptimizefor.wdexportoptimizeforprint;//Export mode 1.wdExportOptimizeForPrint for printing into

Line export, high quality, the resulting file size is large. 2.wdExportOptimizeForOnScreen is exported for screen display,

Poor quality, resulting in smaller file sizes.

Wdexportrange wdexportalldocument = wdexportrange.wdexportalldocument;//Export Full

Part 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: The export file is not tagged, 2.

Export file has tags

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 guide the

Create bookmarks in the file, 2.wdExportCreateHeadingBookmarks: Title and text box to create a bookmark in the exported file,

3.wdExportCreateWordBookmarks bookmarks for each word, including all bookmarks in the header and footer in the exported file

Create a bookmark.

bool Docstructuretags = true;

bool bitmapmissingfonts = true;

BOOL Useiso19005_1 = false;//generated document conforms to ISO 19005-1 (pdf/a)

Document = ApplicationClass.Documents.Open (ref inputfilename, 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 (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 = null;

}

}

return result;

}

2. Concise method

C # code

<summary>

Convert a Word file to a PDF file

</summary>

<param name= "SourcePath" > File path and file name to convert </param>

<param name= "TargetPath" > The path and filename name of the file after the conversion is complete </param>

<returns> successful return true, failed to return false</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 = null;

}

}

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.