ASP. NET converts Word documents to pdf. asp. netpdf

Source: Internet
Author: User

ASP. NET converts Word documents to pdf. asp. netpdf

This article describes how ASP. NET converts a Word document to a pdf file. The specific implementation steps are as follows:

1. Add reference

Copy codeThe Code is as follows: using Microsoft. Office. Interop. Word;
 
Ii. Conversion Method
 
1. Method

Copy codeThe Code is as follows: // <summary>
/// Convert a Word file into a PDF file
/// </Summary>
/// <Param name = "sourcePath"> file path and name to be converted </param>
/// <Param name = "targetPath"> path and file name of the converted file </param>
/// <Returns> success returns true, failure returns false </returns>
Public static bool WordToPdf (string sourcePath, string targetPath)
{
Bool result = false;
WdExportFormat wdExportFormatPDF = WdExportFormat. wdExportFormatPDF; // conversion format 1. Convert wdExportFormatPDF 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 to be converted to format
String outputFileName = targetPath; // path and file name of the PDF or XPS file after conversion
WdExportFormat exportFormat = wdExportFormatPDF; // format used to export the file
Bool openAfterExport = false; // whether to enable the function after the conversion is completed
WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor. wdExportOptimizeForPrint; // export method 1. wdExportOptimizeForPrint is exported for printing, with high quality and large file size. 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; // The ending page number.
WdExportItem wdExportDocumentContent = WdExportItem. wdExportDocumentContent; // specify whether the exported file contains only the text or the text tag. 1. wdExportDocumentContent: the exported file is not marked; 2. the exported file is marked
Bool includeDocProps = true; // specifies whether the newly exported file is included in the document attribute.
Bool keepsert = true ;//
WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks. 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/)
Document = applicationClass. documents. open (ref inputfileName, ref missing, ref missing, ref missing, ref missing );
If (document! = Null)
{
Document. ExportAsFixedFormat (outputFileName, exportFormat, openAfterExport, plaintext, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepsert, expires, docStructureTags, expires, expires, 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

Copy codeThe Code is as follows: // <summary>
/// Convert a Word file into a PDF file
/// </Summary>
/// <Param name = "sourcePath"> file path and name to be converted </param>
/// <Param name = "targetPath"> path and file name of the converted file </param>
/// <Returns> success returns true, 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 );
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
Copy codeThe Code is as follows: OfficeToPdf. WordToPdf ("d :\\ 1234.doc"," d :\\ 1234.htm ");

I hope this article will help you design your asp.net program.


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.