Use Microsoft. Office. Interop. Excel to convert web pages into PDF, interop. excel. dll

Source: Internet
Author: User

Use Microsoft. Office. Interop. Excel to convert web pages into PDF, interop. excel. dll

There are many ways to convert Web pages into PDF files on the Internet, and there are also many third-party plug-ins that charge fees. In fact, you can use the built-in function of publishing EXCEL to PDF. If your requirements are not complex, you can use the author's method.

First, save the web page html as an EXCEL file.(There are multiple methods for this step, so we will not discuss it in detail. As long as the web page can be converted into an EXCEL file, the rest is easy to say .)

StringWriter html = new StringWriter (); HtmlTextWriter tw = new HtmlTextWriter (html); base. render (tw); // Excel address for Excel storage: FileStream fs = new FileStream (excelName, FileMode. create); StreamWriter sw = new StreamWriter (fs, Encoding. getEncoding ("UTF-8"); // starts writing to sw. write (html); // clear the buffer sw. flush (); // close the flow sw. close (); fs. close ();

Use Microsoft. Office. Interop. Excel to convert the EXCEL file to a PDF file.

First, reference Microsoft. Office. Interop. Excel. dll and set the dll property value to false. Otherwise, the type "Microsoft. Office. Interop. Excel. ApplicationClass" is reported. undefined constructors cannot embed the interoperability type "Microsoft. Office. Interop. Excel. ApplicationClass ". An error occurred while using the applicable interface. Then install saveasypandxps.exe (only after this plug-in is installed can excel be saved as pdf). The source code is as follows:
/// <Summary> // Save PDF in Excel /// </summary> /// <param name = "excelPath"> full EXCEL path </param> /// <param name = "pdfPath"> PDF storage path </param> // <returns> </returns> public static bool CovertExcelToPDF (string excelPath, string pdfPath) {object missing = Type. missing; // create an excel application instance ApplicationClass application = null; // create a Workbook workBook instance Workbook = null; try {application = new ApplicationClass (); /// Open the workBook = application. workbooks. open (excelPath, missing, missing ); //// open sheet Worksheet ws = (Worksheet) workBook. worksheets. item [1]; // you can specify the horizontal ws for printing. pageSetup. orientation = XlPageOrientation. xlPortrait; // set the excel content to be displayed on a page when printing. Zoom must be set to false ws. pageSetup. zoom = false; ws. pageSetup. fitToPagesTall = 1; ws. pageSetup. fitToPagesWide = 1; /// publish the workbook in PDF or XPS format. exportAsFixedFormat (XlFixedFormatType. xlTypePDF, pdfPath, XlFixedFormatQuality. xlQualityStandard, true, false /// ignore print area, missing, missing); return true ;} catch {throw ;} finally {// close the if (workBook! = Null) {workBook. Close (true, missing, missing); workBook = null;} // exit the excel application and Close if (application! = Null) {application. quit (); application = null;} GC. collect (); GC. waitForPendingFinalizers (); GC. collect (); GC. waitForPendingFinalizers ();}}


[Sample Code]

 


How to convert a web page to a PDF Format

You can use Acrobat to download a webpage from the Internet and convert it to PDF. You can define layout, set display options for fonts and other visual components, and create bookmarks for webpages converted to PDF.

Since the obtained web pages are PDF files, you can easily save, distribute, and print them for sharing and future use. Acrobat enables you to convert remote, minimal-format files into local fully formatted FPD files, which you can access at any time.

Determine your Internet or proxy server settings.

Before you intercept data over a network, you must set up your Internet or proxy server to enable your access to the global information network.

Choose File> Preferences> InternetSettings.
Select one of the following items:

In Windows, click Connection in the InernetProperties dialog box and provide necessary information for your settings. Your system administrator or ISP will provide you with the required information.

In the Mac operating system, select UseanHTTPProxyServer and enter your proxy server URL and port number in the text window.

In Windows, if you do not need InternetSettings to set your Internet, you must install InternetExplorer and set it in the InternetProperties dialog box to allow access to the global information network. In special cases, if you enter the network through the firewall from the enterprise environment, the proxy server window in the connection version must have a valid proxy server address. Once InternetExplorer is installed and configured, you can use any browser as your default browser. If your InternetExplorer version does not have an InternetProperties dialog window, you must update it to the current version of InternetExplorer (available on the Microsoft Web site ).

Set options for conversion webpage
Before downloading a webpage, you need to set options for intercepting the webpage. Here, you will set the structure and appearance options for the obtained webpage.

1. Select File> OpenWebPage.

Note: If the command OpenWebPage is not displayed in the File menu, choose File-> Preferences-> WebCapture, and discard the ConsolidateMenuItems option from the top menu. After this option is selected, all commands about web page interception are displayed in a separate web page menu.

2. Click ConversionSettings.

3. In the ConversionSettings dialog box, click General.

4. Under GeneralSettingsforGeneratedPDF, select the following options:

CreateBookmarkstoNewContent uses the HTML topic tag of the webpage as the bookmarks to create a framework bookmarks for each downloaded webpage. Frame bookmarks help you organize and browse Web pages you have intercepted.
Addmediastructure stores the structure of the PDF file that is equivalent to the HTML structure of the original webpage.
PutHeadersandFootersonNewPage (Windows) or PutHeadersandFootersonNewContent (Mac operating system) uses the subject of the webpage as the header, the URL of the webpage, the page number of the downloaded file, and the date and time as the end of the logo) or SaveUpdateCommands (Mac operating system) stores all URLs in PDF files to update webpages.

How can I convert a Microsoft Word document to a PDF file?
Enable text in Microsoft Word format

Click the CreateAdobePDF button in the toolbar, or select... the remaining full text>

How can I put a whole line on a page when converting an excel file to a PDF file?

Converting PDF is actually printing. Set the page in the printing, and print the preview on the page.
 

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.