Document

Document()

创建一个空白 Word 文档。

public Document()

评论

默认情况下,文档纸张尺寸为 Letter。如果您想更改页面设置,请使用 PageSetup

创建完成后就可以使用DocumentBuilder轻松添加文档内容。

例子

演示如何使用其字体属性设置文本串的格式。

Document doc = new Document();
Run run = new Run(doc, "Hello world!");

Aspose.Words.Font font = run.Font;
font.Name = "Courier New";
font.Size = 36;
font.HighlightColor = Color.Yellow;

doc.FirstSection.Body.FirstParagraph.AppendChild(run);
doc.Save(ArtifactsDir + "Font.CreateFormattedRun.docx");

演示如何创建和加载文档。

// 使用 Aspose.Words 创建 Document 对象有两种方法。
// 1 - 创建一个空白文档:
Document doc = new Document();

// 新的 Document 对象默认带有最小的节点集
// 开始添加文本和形状等内容所需的:节、正文和段落。
doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, "Hello world!"));

// 2 - 加载本地文件系统中存在的文档:
doc = new Document(MyDir + "Document.docx");

// 加载的文档将包含我们可以访问和编辑的内容。
Assert.AreEqual("Hello World!", doc.FirstSection.Body.FirstParagraph.GetText().Trim());

// 加载过程中需要发生的一些操作,例如使用密码解密文档,
// 可以通过在加载文档时传递 LoadOptions 对象来完成。
doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword"));

Assert.AreEqual("Test encrypted document.", doc.FirstSection.Body.FirstParagraph.GetText().Trim());

也可以看看


Document(string)

从文件中打开现有文档。自动检测文件格式。

public Document(string fileName)
范围类型描述
fileNameString要打开的文档的文件名。

例外

例外(健康)状况
UnsupportedFileFormatException无法识别或不支持文档格式。
FileCorruptedException该文档似乎已损坏且无法加载。
Exception该文档存在问题,应报告给 Aspose.Words 开发人员。
IOException存在输入/输出异常。
IncorrectPasswordException该文档已加密,需要密码才能打开,但您提供的密码不正确。
ArgumentException文件名不能为空或空字符串。

例子

演示如何打开文档并将其转换为 .PDF。

Document doc = new Document(MyDir + "Document.docx");

doc.Save(ArtifactsDir + "Document.ConvertToPdf.pdf");

演示如何将 PDF 转换为 .docx。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Hello world!");

doc.Save(ArtifactsDir + "PDF2Word.ConvertPdfToDocx.pdf");

// 加载我们刚刚保存的PDF文档,并将其转换为.docx。
Document pdfDoc = new Document(ArtifactsDir + "PDF2Word.ConvertPdfToDocx.pdf");

pdfDoc.Save(ArtifactsDir + "PDF2Word.ConvertPdfToDocx.docx");

展示如何加载 PDF。

Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.Write("Hello world!");

doc.Save(ArtifactsDir + "PDF2Word.LoadPdf.pdf");

// 以下是使用 Aspose 产品加载 PDF 文档的两种方法。
// 1 - 加载为 Aspose.Words 文档:
Aspose.Words.Document asposeWordsDoc = new Aspose.Words.Document(ArtifactsDir + "PDF2Word.LoadPdf.pdf");

Assert.AreEqual("Hello world!", asposeWordsDoc.GetText().Trim());

// 2 - 加载为 Aspose.Pdf 文档:
Aspose.Pdf.Document asposePdfDoc = new Aspose.Pdf.Document(ArtifactsDir + "PDF2Word.LoadPdf.pdf");

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
asposePdfDoc.Pages.Accept(textFragmentAbsorber);

Assert.AreEqual("Hello world!", textFragmentAbsorber.Text.Trim());

也可以看看


Document(string, LoadOptions)

从文件中打开现有文档。允许指定其他选项,例如加密密码。

public Document(string fileName, LoadOptions loadOptions)
范围类型描述
fileNameString要打开的文档的文件名。
loadOptionsLoadOptions加载文档时使用的其他选项。可无效的

例外

例外(健康)状况
UnsupportedFileFormatException无法识别或不支持文档格式。
FileCorruptedException该文档似乎已损坏且无法加载。
Exception该文档存在问题,应报告给 Aspose.Words 开发人员。
IOException存在输入/输出异常。
IncorrectPasswordException该文档已加密,需要密码才能打开,但您提供的密码不正确。
ArgumentException文件名不能为空或空字符串。

例子

演示如何加载加密的 Microsoft Word 文档。

Document doc;

// 如果我们尝试在没有密码的情况下打开加密文档,Aspose.Words 会抛出异常。
Assert.Throws<IncorrectPasswordException>(() => doc = new Document(MyDir + "Encrypted.docx"));

// 加载此类文档时,密码将使用 LoadOptions 对象传递给文档的构造函数。
LoadOptions options = new LoadOptions("docPassword");

// 使用 LoadOptions 对象加载加密文档有两种方法。
// 1 - 按文件名从本地文件系统加载文档:
doc = new Document(MyDir + "Encrypted.docx", options);
// 2 - 从流加载文档:
using (Stream stream = File.OpenRead(MyDir + "Encrypted.docx"))
{
    doc = new Document(stream, options);
}

演示如何创建和加载文档。

// 使用 Aspose.Words 创建 Document 对象有两种方法。
// 1 - 创建一个空白文档:
Document doc = new Document();

// 新的 Document 对象默认带有最小的节点集
// 开始添加文本和形状等内容所需的:节、正文和段落。
doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, "Hello world!"));

// 2 - 加载本地文件系统中存在的文档:
doc = new Document(MyDir + "Document.docx");

// 加载的文档将包含我们可以访问和编辑的内容。
Assert.AreEqual("Hello World!", doc.FirstSection.Body.FirstParagraph.GetText().Trim());

// 加载过程中需要发生的一些操作,例如使用密码解密文档,
// 可以通过在加载文档时传递 LoadOptions 对象来完成。
doc = new Document(MyDir + "Encrypted.docx", new LoadOptions("docPassword"));

Assert.AreEqual("Test encrypted document.", doc.FirstSection.Body.FirstParagraph.GetText().Trim());

也可以看看


Document(Stream)

从流中打开现有文档。自动检测文件格式。

public Document(Stream stream)
范围类型描述
streamStream流式加载文档的位置。

例外

例外(健康)状况
UnsupportedFileFormatException无法识别或不支持文档格式。
FileCorruptedException该文档似乎已损坏且无法加载。
Exception该文档存在问题,应报告给 Aspose.Words 开发人员。
IOException存在输入/输出异常。
IncorrectPasswordException该文档已加密,需要密码才能打开,但您提供的密码不正确。
ArgumentNullException流不能为空。
NotSupportedException该流不支持读取或查找。
ObjectDisposedException流是一个已处理的对象。

评论

文档必须存储在流的开头。流必须支持随机定位。

例子

演示如何使用流加载文档。

using (Stream stream = File.OpenRead(MyDir + "Document.docx"))
{
    Document doc = new Document(stream);

    Assert.AreEqual("Hello World!\r\rHello Word!\r\r\rHello World!", doc.GetText().Trim());
}

演示如何从 URL 加载文档。

// 创建一个指向 Microsoft Word 文档的 URL。
const string url = "https://filesamples.com/samples/document/docx/sample3.docx";

// 将文档下载到字节数组中,然后使用内存流将该数组加载到文档中。
using (HttpClient webClient = new HttpClient())
{
    byte[] dataBytes = await webClient.GetByteArrayAsync(url);

    using (MemoryStream byteStream = new MemoryStream(dataBytes))
    {
        Document doc = new Document(byteStream);

        // 在此阶段,我们可以读取和编辑文档的内容,然后将其保存到本地文件系统。
        Assert.AreEqual("There are eight section headings in this document. At the beginning, \"Sample Document\" is a level 1 heading. " +
            "The main section headings, such as \"Headings\" and \"Lists\" are level 2 headings. " +
            "The Tables section contains two sub-headings, \"Simple Table\" and \"Complex Table,\" which are both level 3 headings.",                         
            doc.FirstSection.Body.Paragraphs[3].GetText().Trim());

        doc.Save(ArtifactsDir + "Document.LoadFromWeb.docx");
    }
}

也可以看看


Document(Stream, LoadOptions)

从流中打开现有文档。允许指定其他选项,例如加密密码。

public Document(Stream stream, LoadOptions loadOptions)
范围类型描述
streamStream从中加载文档的流。
loadOptionsLoadOptions加载文档时使用的其他选项。可无效的

例外

例外(健康)状况
UnsupportedFileFormatException无法识别或不支持文档格式。
FileCorruptedException该文档似乎已损坏且无法加载。
Exception该文档存在问题,应报告给 Aspose.Words 开发人员。
IOException存在输入/输出异常。
IncorrectPasswordException该文档已加密,需要密码才能打开,但您提供的密码不正确。
ArgumentNullException流不能为空。
NotSupportedException该流不支持读取或查找。
ObjectDisposedException流是一个已处理的对象。

评论

文档必须存储在流的开头。流必须支持随机定位。

例子

演示如何使用基本 URI 打开包含来自流的图像的 HTML 文档。

using (Stream stream = File.OpenRead(MyDir + "Document.html"))
{
    // 加载时传递基本文件夹的 URI
    // 这样就可以找到 HTML 文档中具有相对 URI 的任何图像。
    LoadOptions loadOptions = new LoadOptions();
    loadOptions.BaseUri = ImageDir;

    Document doc = new Document(stream, loadOptions);

    // 验证文档的第一个形状是否包含有效图像。
    Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);

    Assert.IsTrue(shape.IsImage);
    Assert.IsNotNull(shape.ImageData.ImageBytes);
    Assert.AreEqual(32.0, ConvertUtil.PointToPixel(shape.Width), 0.01);
    Assert.AreEqual(32.0, ConvertUtil.PointToPixel(shape.Height), 0.01);
}

演示如何将网页另存为 .docx 文件。

const string url = "https://www.aspose.com/";

using (HttpClient client = new HttpClient()) 
{
    var bytes = await client.GetByteArrayAsync(url);
    using (MemoryStream stream = new MemoryStream(bytes))
    {
        // URL 再次用作 baseUri,以确保正确检索任何相对图像路径。
        LoadOptions options = new LoadOptions(LoadFormat.Html, "", url);

        // 从流加载 HTML 文档并传递 LoadOptions 对象。
        Document doc = new Document(stream, options);

        // 在此阶段,我们可以读取和编辑文档的内容,然后将其保存到本地文件系统。
        doc.Save(ArtifactsDir + "Document.InsertHtmlFromWebPage.docx");
    }
}

演示如何加载加密的 Microsoft Word 文档。

Document doc;

// 如果我们尝试在没有密码的情况下打开加密文档,Aspose.Words 会抛出异常。
Assert.Throws<IncorrectPasswordException>(() => doc = new Document(MyDir + "Encrypted.docx"));

// 加载此类文档时,密码将使用 LoadOptions 对象传递给文档的构造函数。
LoadOptions options = new LoadOptions("docPassword");

// 使用 LoadOptions 对象加载加密文档有两种方法。
// 1 - 按文件名从本地文件系统加载文档:
doc = new Document(MyDir + "Encrypted.docx", options);
// 2 - 从流加载文档:
using (Stream stream = File.OpenRead(MyDir + "Encrypted.docx"))
{
    doc = new Document(stream, options);
}

也可以看看