Asp. NET implementation method to convert Word documents to PDF-Practical tips

Source: Internet
Author: User

This article describes the ASP.net implementation of the Word document conversion to PDF method, shared for everyone to reference. The following steps are implemented:

One, add a reference

Copy Code code as follows:
Using Microsoft.Office.Interop.Word;


Second, the conversion method

1. Methods

Copy Code code as follows:
<summary>
Convert Word files to PDF files
</summary>
<param name= "SourcePath" > File path and file name required for conversion </param>
<param name= "TargetPath" > Conversion completed file path and filename name </param>
<returns> returns true successfully, Failure returns false</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. Convert 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 requires a format transfer
String outputfilename = path and filename name of the PDF or XPS file after the conversion is complete targetpath;//
Wdexportformat ExportFormat = wdexportformatpdf;//The format used by the export file
BOOL Openafterexport = false;//after conversion is complete
Wdexportoptimizefor Wdexportoptimizeforprint = wdexportoptimizefor.wdexportoptimizeforprint;// Export mode 1.wdExportOptimizeForPrint for printing export, high quality, resulting in large file size. 2.wdExportOptimizeForOnScreen exports for on-screen display, poor quality, resulting in a smaller file size.
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 marked, 2. export file marked
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 a bookmark in the export file, 2.wdExportCreateHeadingBookmarks: Create a bookmark in the title and text box export file, 3. Wdexportcreatewordbookmarks a bookmark for each word, including creating a bookmark in a file that contains all the bookmarks exported in the header and footer.
bool Docstructuretags = true;
bool bitmapmissingfonts = true;
BOOL Useiso19005_1 = false;//The resulting document conforms to ISO 19005-1 (pdf/a)
Document = ApplicationClass.Documents.Open (ref inputfilename, ref missing, ref missing, ref missing, ref missing, ref miss ING, 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

Copy Code code as follows:
<summary>
Convert Word files to PDF files
</summary>
<param name= "SourcePath" > File path and file name required for conversion </param>
<param name= "TargetPath" > Conversion completed file path and filename name </param>
<returns> returns true successfully, Failure returns 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
Copy Code code as follows:
Officetopdf.wordtopdf ("D:\\1234.doc", "d:\\1234.pdf");

I hope this article will help you with the ASP.net program design.

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.