我正在使用文件上传控件上传.doc文件。如何获取上传的.doc文件中的页数?

时间:2014-03-24 05:20:03

标签: c# asp.net ms-word

我正在使用文件上传控件上传.doc文件。如何获取上传的.doc文件中的页数?我想要上传文件中的页数。

2 个答案:

答案 0 :(得分:1)

你可以像这样得到word文件的页数。

添加Microsoft Office 2013 COM对象的引用

using Microsoft.Office.Interop.Word;

class Program
{
    static void Main(string[] args)
    {
        var application = new Application();

        // Open YOur word file path
        var document = application.Documents.Open(@"C:\Users\Test\Desktop\Demo.docx");

        // Get the page count.
        var numberOfPages = document.ComputeStatistics(WdStatistic.wdStatisticPages, false);

        // Print out the result
        Console.WriteLine("Total number of pages in document: {0}", numberOfPages);
    }
}

答案 1 :(得分:1)

试试这个

你的.aspx页面

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Label ID="StatusLabel" runat="server" Text=""></asp:Label>
<asp:Button runat="server" Text="Upload" ID="btnupload" OnClick="BtnuploadClick" />

你的.aspx.cs页面

protected void BtnuploadClick(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(FileUpload1.FileName);
            FileUpload1.SaveAs(Server.MapPath("~/") + filename);

            var application = new Application();
            var document = application.Documents.Open(Server.MapPath("~/") + filename);

            // Get the page count.
            var numberOfPages = document.ComputeStatistics(WdStatistic.wdStatisticPages, false);

            StatusLabel.Text = string.Format("Total number of pages in document: {0}", numberOfPages);

        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }

}

并且不要忘记添加“Microsoft.Office.Interop.Word”的引用