ASP. NET converts Word documents into pdf code, asp. netpdf

Source: Internet
Author: User

ASP. NET converts Word documents into pdf code, asp. netpdf

1. Add reference

Using Microsoft. Office. Interop. Word;

 

Ii. Conversion Method

 

1. Method

C # code Replication
/// <Summary> /// convert a Word file to a PDF file /// </summary> /// <param name = "sourcePath"> file path and file to be converted name </param> /// <param name = "targetPath"> the path and name of the converted file </param> /// <returns> returns true, false is returned for failure </returns> public static bool WordToPdf (string sourcePath, string targetPath) {bool result = false; WdExportFormat wdExportFormatPDF = WdExportFormat. wdExportFormatPDF; // conversion format 1. wdExportFormatPDF to pdf Format 2. wdExportF OrmatXPS converts it to the 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 to be converted to the format string outputFileName = targetPath; // after the conversion, the path and file name of the PDF or XPS file are WdExportFormat exportFormat = wdExportFormatPDF; // The bool o format used by the exported file PenAfterExport = false; // whether to enable WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor after the conversion is complete. wdExportOptimizeForPrint; // export method 1. wdExportOptimizeForPrint is used to export data for printing. The quality is high and the size of the generated file is large. 2. wdExportOptimizeForOnScreen is used to export the screen display. The quality is poor and the size of the generated file is small. WdExportRange wdExportAllDocument = WdExportRange. wdExportAllDocument; // export all content (enumeration) int from = 0; // start page number int to = 0; // end page number WdExportItem wdExportDocumentContent = WdExportItem. wdExportDocumentContent; // specifies whether to include only text or text tags during the export process. 1. wdExportDocumentContent: the exported file is not marked, 2. the exported file is marked with bool export dedocprops = true; // specifies whether the newly exported file is included in the file attribute bool keepsert = true; // WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExport CreateBookmarks. wdExportCreateWordBookmarks; // 1. wdExportCreateNoBookmarks: do not create bookmarks in the export file. wdExportCreateHeadingBookmarks: Create a bookmark in the file exported by the title and text box. wdExportCreateWordBookmarks bookmarks for each word, including creating a bookmark in a file that contains all the bookmarks in the header and footer. Bool docStructureTags = true; bool bitmapMissingFonts = true; bool UseISO19005_1 = false; // whether the generated document complies with ISO 19005-1 (PDF/A) document = applicationClass. documents. open (ref inputfileName, ref missing, ref missing, ref missing, ref missing); if (document! = Null) {document. export (outputFileName, exportFormat, openAfterExport, delimiter, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepsert, delimiter, docStructureTags, delimiter, delimiter, 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. Simple Method

 

C # code Replication
/// <Summary> /// convert a Word file to a PDF file /// </summary> /// <param name = "sourcePath"> file path and file to be converted name </param> /// <param name = "targetPath"> the path and name of the converted file </param> /// <returns> returns true, false is returned for failure </returns> public static bool WordToPdf (object sourcePath, string targetPath) {bool result = false; WdExportFormat wdExportFormatPDF = WdExportFormat. wdExportFormatPDF; object missing = Type. missing; Microsoft. off Ice. 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 miss Ing, 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 ;}

 

Iii. Call

OfficeToPdf. WordToPdf ("d :\\ 1234.doc"," d :\\ 1234.htm ");


Converting Word documents into PDF files in aspnet

C # programming, converting Word to PDF, this method has some drawbacks, but it is better than other methods on the network, the disadvantage is that you need to install WORD 2007 or a later version on the client. 1. Add reference: Microsoft. office. interop. word version 12.0.0.0; 2. Add the namespace reference at the beginning: using Microsoft. office. interop. word; 3. The specific implementation method is as follows: // convert Word to pdf /// <summary> // convert the Word file to a PDF file /// </summary> /// <param name = "sourcePath"> source file path </param> /// <param name = "targetPath"> target file path </param> /// <returns> true = Conversion successful </returns> privatebool wordToPDF (string sourcePath, string targetPath) {bool result = false; Microsoft. office. interop. word. wdExportFormat exportFormat = Microsoft. office. interop. word. wdExportFormat. wdExportFormatPDF; object paramMissing = Type. missing; Microsoft. office. interop. word. applicationClass wordApplication = new Microsoft. office. interop. word. applicationClass (); Microsoft. office. interop. word. document wordDocument = null; try {object paramSourceDocPath = sourcePath; string paramExportFilePath = targetPath; Microsoft. office. interop. word. wdExportFormat paramExportFormat = exportFormat; bool paramOpenAfterExport = false; Microsoft. office. interop. word. wdExportOptimizeFor paramExportOptimizeFor = Microsoft. office. interop. word. wdExportOptimizeFor. wdExportOptimizeForPrint; Microsoft. office. interop. word. wdExportRange paramExportRange = Microsoft. office. interop. wor ...... remaining full text>

ASPNET website converts Word documents to PDF format and then uploads the folder of the system

Use the Aspose. Words. dll component.
Code: Aspose. Words. Document doc = new Document ("XXXX.doc ");
Doc. SaveToPdf ("XXXXX.doc.pdf ");

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.