2020年6月10日 星期三

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 把 .docx 轉成 .pdf

[研究][C#][ASP.NET] 使用 FreeSpire.Doc 7.11.0 把 .docx 轉成 .pdf

2020-06-10

Visual Studio 2019 v16.6.1 + WebForm + WebApplication
Nuget 安裝 FreeSpire.Doc 7.11.0 元件 (不是 Spire.Doc )

官方網站

免費版有篇幅限制。在加載或操作 Word 文檔時,要求 Word 文檔不超過 500 個段落,25 個表格。同時將 Word 文檔轉換為 PDF 和 XPS 格式時,僅支持轉換前三頁。

Free Spire.Doc for .NET is a Community Edition of the Spire.Doc for .NET, which is a totally free word API for commercial and personal use. As a free C#/VB.NET API, it also offers a lot of powerful functions. Developers can use it to generate, read, write, save and convert documents on any .NET applications. The featured function, conversion allows converting Word documents (Word 97-2003, Word 2007, Word 2010, Word 2013 and Word 2016) to commonly used file format, such as XML, RTF, TXT, PDF, XPS, EPUB, HTML and Image etc.

Friendly Reminder:
Free version is limited to 500 paragraphs and 25 tables. This limitation is enforced during reading or writing files. When converting word documents to PDF and XPS files, you can only get the first 3 page of PDF file. Upgrade to Commercial Edition of Spire.Doc

參考
https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SpireDocDemo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using Spire.Doc;
using System;

namespace SpireDocDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"C:\temp\test.docx");

            //Convert Word to PDF
            document.SaveToFile(@"C:\temp\testt.pdf", FileFormat.PDF);

            //Launch Document
            System.Diagnostics.Process.Start(@"C:\temp\testt.pdf");
        }
    }
}


(完)

沒有留言:

張貼留言