Aspose.Words_檔案創建讀寫_資訊獲取

Aspose.Words for .NET是一個類庫,使你的應用程序能夠執行各種文檔處理任務。
Aspose.Words支持DOC、DOCX、RTF、HTML、OpenDocument、PDF、XPS、EPUB和其他格式。詳細表如下


支援讀取格式:




支援保存格式






使用Aspose.Words,不需安裝Microsoft Word軟體,就可運用內建的物件模型來讀取、編輯及產生Word文件。內建的物件模型支援大部份Word功能,例如文字格式操作、表格、圖片、合併列印...等。

試用版載點
https://downloads.aspose.com/words/net
試用版缺點是會有公司版權浮水印、紅色告警文字



需要引入的Dll參考
Aspose.Words.dll

API線上文檔
Font Properties
https://apireference.aspose.com/words/net/aspose.words/font/properties/index
Document Class
https://apireference.aspose.com/words/net/aspose.words/document


一個使用範例
aspx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AsposeWordTest.WebForm1" %>

<!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="btnCreateWordDoc" runat="server" Text="建立Word" OnClick="btnCreateWordDoc_Click" />
            <asp:Button ID="btnReadWordDoc" runat="server" Text="讀取word" OnClick="btnReadWordDoc_Click" />
            <asp:Button ID="btnGetWordDocInfo" runat="server" Text="讀取檔案資訊" OnClick="btnGetWordDocInfo_Click" />
            <br />
            <textarea id="TextArea_Output" runat="server" cols="200" rows="10"></textarea>
            <br />
        </div>
    </form>
</body>
</html>






aspx.cs

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Words;


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

        }

        protected void btnCreateWordDoc_Click(object sender, EventArgs e)
        {
            Document doc = new Document();//一份Microsoft Word文件(可以是一個Word檔案、一個Stream物件,或是加密的文件)
            DocumentBuilder builder = new DocumentBuilder(doc);//處理文件內容
            //預設不指定會跑到C disk IIS Express目錄下
            //doc.Save("myAsposeWord1.doc");//C:\Program Files (x86)\IIS Express\myAsposeWord1.doc'
            //建立空檔
            //doc.Save(@"D:\AsposeTmpDoc\myAsposeWord1.doc");
            //doc.Save(@"D:\AsposeTmpDoc\myAsposeWord2.docx");


            //如果沒有加上這段會視為沒取得授權的Evaluation版本,會有Aspose公司的icon浮水印跟紅色警示文字(於開始跟頁尾)。
            //Aspose.Words.License words_license = new Aspose.Words.License();
            //words_license.SetLicense(HttpContext.Current.Server.MapPath("./RefDll/Aspose.Total.lic"));
            //builder.Write("這是文字第1列");
            //builder.Write("這是文字第2列");
            //builder.Writeln("這是文字第3列(會換行)");
            //doc.Save(@"D:\AsposeTmpDoc\myAsposeWord4_with_text.docx");

            //設定授權
            Aspose.Words.License words_license = new Aspose.Words.License();
            words_license.SetLicense(HttpContext.Current.Server.MapPath("./RefDll/Aspose.Total.lic"));


            //寫入文字後存檔
            //builder.Write("這是文字第1列");
            //builder.Write("這是文字第2列");
            //builder.Writeln("這是文字第3列_尾端串換行字元(還不會換行)");
            //builder.Writeln("這是文字第4列_尾端串換行字元(會換行)");
            //builder.Writeln("這是文字第5列_尾端串換行字元(會換行)");
            //doc.Save(@"D:\AsposeTmpDoc\myAsposeWord4_with_text.docx");


            //寫入文字後存檔(顏色、字體)
            //Default字體、大小、顏色黑色
            builder.Writeln("這是文字第1列_尾端串換行字元(預設中文新細明體12pt)");
            builder.Writeln("second line default font style: Times New Roman, font size:12pt");

            Aspose.Words.Font font = builder.Font;
            font.Size = 16;
            font.Bold = true;
            font.Color = System.Drawing.Color.Blue;
            font.Name = "Elephant";
            builder.Writeln("這是文字第3列_尾端串換行字元16pt");
            builder.Writeln("forth line default font style: Elephant, font size:16pt");

            builder.Font.Size = 20;
            builder.Font.Bold = false;
            builder.Font.Color = System.Drawing.Color.Orange;
            builder.Font.Name = "Courier New";
            builder.Writeln("這是文字第3列_尾端串換行字元20pt");
            builder.Writeln("forth line default font style: Courier New, font size:20pt");

            builder.Font.Border.Color = System.Drawing.Color.Green;
            builder.Font.Border.LineWidth = 2.5d;
            builder.Font.Border.LineStyle = LineStyle.DashDotStroker;
            builder.Writeln("Text surrounded by green border.");

            builder.Font.Size = 14;
            builder.Font.Underline = Underline.Dash;
            builder.Writeln("Text with dash underline font size: 14pt");

            builder.Font.ClearFormatting();
            builder.Font.Size = 14;
            builder.Font.Underline = Underline.Dash;
            builder.Font.UnderlineColor = System.Drawing.Color.Red;
            builder.Writeln("Text with red dash underline font size: 14pt");

            builder.Font.ClearFormatting();
            builder.Font.Size = 12;
            builder.Font.HighlightColor = System.Drawing.Color.Yellow;
            builder.Writeln("Text with yellow higlight");

            builder.Font.ClearFormatting();
            builder.Writeln("back to default font setting");

            builder.InsertHyperlink("連結到yahoo", "https://tw.yahoo.com/", false);
            doc.Save(@"D:\AsposeTmpDoc\myAsposeWord6_with_text.docx");
            //builder.Font.Color = System.Drawing.Color.Blue;


        }

        protected void btnReadWordDoc_Click(object sender, EventArgs e)
        {
            //設定授權
            Aspose.Words.License words_license = new Aspose.Words.License();
            words_license.SetLicense(HttpContext.Current.Server.MapPath("./RefDll/Aspose.Total.lic"));
            //Document doc = new Document(Server.MapPath("~/App_Data/myAsposeWord4_with_text.docx"));
            //Document doc = new Document(Server.MapPath("~/App_Data/myAsposeWord4_with_text_image.docx"));//只會抓文字部分的內容
            Document doc = new Document(Server.MapPath("~/App_Data/test.txt"));//.txt也可讀取
            TextArea_Output.InnerText = doc.GetText();
        }

        protected void btnGetWordDocInfo_Click(object sender, EventArgs e)
        {
            //設定授權
            Aspose.Words.License words_license = new Aspose.Words.License();
            words_license.SetLicense(HttpContext.Current.Server.MapPath("./RefDll/Aspose.Total.lic"));
            Document doc = new Document(Server.MapPath("~/App_Data/myAsposeWord5_with_text_image_3page.docx"));
            StringBuilder sbDocInfo = new StringBuilder();
            sbDocInfo.AppendLine("Page Count:" + doc.PageCount);//Gets the number of pages in the document as calculated by the most recent page layout operation.
            sbDocInfo.AppendLine("Page Count:" + doc.Count);//Gets the number of immediate children of this node.(Inherited from CompositeNode.)
            sbDocInfo.AppendLine("File Name:" + doc.OriginalFileName);//Gets the original file name of the document.
            sbDocInfo.AppendLine("Page Color Name:" + doc.PageColor.Name);//Gets or sets the page color of the document. This property is a simpler version of BackgroundShape.(Inherited from DocumentBase.)
            sbDocInfo.AppendLine("Page Color RGB:" + String.Format("({0},{1},{2})", doc.PageColor.R, doc.PageColor.G, doc.PageColor.B));


            // Each page has a PageInfo object, whose index is the respective page's number
            for (int idxPage = 0; idxPage < doc.PageCount; idxPage++)
            {
                Aspose.Words.Rendering.PageInfo pgInfo = doc.GetPageInfo(idxPage);
                sbDocInfo.AppendLine(String.Format("Page {0} Info, PageSize:{1}", idxPage + 1, pgInfo.PaperSize));//Gets the paper size as enumeration.
                sbDocInfo.AppendLine(String.Format("Page {0} Info, Landscape:{1}", idxPage + 1, pgInfo.Landscape)); //Returns true if the page orientation specified in the document for this page is landscape.
                sbDocInfo.AppendLine(String.Format("Page {0} Info, PaperTray:{1}", idxPage + 1, pgInfo.PaperTray)); //Gets the paper tray (bin) for this page as specified in the document. The value is implementation (printer) specific.
                sbDocInfo.AppendLine(String.Format("Page {0} Info, HeightInPoints:{1}", idxPage + 1, pgInfo.HeightInPoints)); //Gets the height of the page in points.
                sbDocInfo.AppendLine(String.Format("Page {0} Info, WidthInPoints{1}:", idxPage + 1, pgInfo.WidthInPoints)); //Gets the width of the page in points.
                sbDocInfo.AppendLine(String.Format("Page {0} Info, SizeInPoints.Width:", idxPage + 1, pgInfo.SizeInPoints.Width));//Gets the page size in points.
                sbDocInfo.AppendLine(String.Format("Page {0} Info, SizeInPoints.Height:", idxPage + 1, pgInfo.SizeInPoints.Height));//Gets the page size in points. 
            }
            TextArea_Output.InnerText = sbDocInfo.ToString();
        }
    }
}

在創建檔案時效果



讀取檔案內容


獲取Word相關設定(word有不只一頁每一頁都有各自的設定)




Ref Link:
https://apireference.aspose.com/words/net/aspose.words/font/properties/index
https://apireference.aspose.com/words/net/aspose.words
http://www.runpc.com.tw/content/content.aspx?id=109944
https://dpez9gb12h.pixnet.net/blog/post/198240926-aspose-total-for-.net-%28%E7%A8%8B%E5%BC%8F%E9%96%8B%E7%99%BC%29-%28%E4%B8%8B%E8%BC%89%E7%89%88%29%E4%BD%BF%E7%94%A8%E5%BF%83%E5%BE%97
http://www.tsima.org.tw/wiki/dian-zi-qi-kan/2008nian6yue-hao/apose-word-yuan-jian-jie-shao-shang
http://www.tsima.org.tw/wiki/dian-zi-qi-kan/2008nian8yue-hao/aspose-word-yuan-jian-jie-shao-xia


留言

這個網誌中的熱門文章

何謂淨重(Net Weight)、皮重(Tare Weight)與毛重(Gross Weight)

Architecture(架構) 和 Framework(框架) 有何不同?_軟體設計前的事前規劃的藍圖概念

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header