借助给定的强大 API,C# 开发人员可以创建几乎任何格式的文档。为此,您需要使用我们的 .NET 库执行几个步骤:
值得注意的是,从技术上讲,一个空白文档应该包含一个段落,因此当您以编程方式创建文档时,您将获得准确的基本文档结构。
请注意,您可以立即将内容添加到新创建的文档中。因此,您将获得的不仅仅是一个空文档,而是一个包含必要内容的文档。有关如何编辑文档的更多信息,请参阅编辑页面。
给定的 .NET 库允许您以任何受支持的格式(PDF、DOCX、DOC、RTF、ODT、EPUB、HTML 等)以编程方式创建文档。
试试我们强大的功能,看看如何使用以下示例创建某些格式的文档:
dotnet add package Aspose.Words
复制
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var font = builder.Font;
font.Name = "Courier New";
font.Color = Color.Blue;
font.Size = 36;
font.HighlightColor = Color.Yellow;
builder.Write("Morbi enim nunc faucibus a.");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
var saveOptions = new PdfSaveOptions();
saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1);
doc.Save("Output.docx", saveOptions);
using Aspose.Words;
var doc = new Document();
var run = new Run(doc, "Proin eros metus, sagittis sed.");
var para = doc.FirstSection.Body.FirstParagraph;
para.AppendChild(run);
var comment = new Comment(doc)
{
Author = "John Doe",
Initial = "JD",
DateTime = DateTime.Now
};
comment.SetText("Quisque fringilla leo.");
var commentRangeStart = new CommentRangeStart(doc, comment.Id);
var commentRangeEnd = new CommentRangeEnd(doc, comment.Id);
run.ParentNode.InsertBefore(commentRangeStart, run);
run.ParentNode.InsertAfter(commentRangeEnd, run);
commentRangeEnd.ParentNode.InsertAfter(comment, commentRangeEnd);
comment.AddReply("Jane Doe", "JD", DateTime.Now, "Pellentesque vel sapien justo.");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
var saveOptions = new PdfSaveOptions();
saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1);
doc.Save("Output.docx", saveOptions);
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
var saveOptions = new PdfSaveOptions();
saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1);
doc.Save("Output.docx", saveOptions);
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Pie, 432, 252);
Chart chart = shape.Chart;
chart.Title.Text = "Demo Chart";
chart.Series.Clear();
chart.Series.Add("Series 1",
new string[] { "Category1", "Category2", "Category3" },
new double[] { 2.7, 3.2, 0.8 });
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
doc.Save("Output.docx");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var firstRun = new Run(doc, "Proin eros metus, sagittis sed. ");
var secondRun = new Run(doc, "Morbi enim nunc faucibus a.");
doc.FirstSection.Body.FirstParagraph.AppendChild(firstRun);
doc.FirstSection.Body.FirstParagraph.AppendChild(secondRun);
builder.MoveTo(secondRun);
builder.StartBookmark("Aspose bookmark");
// 如果 NextSibling 为 null,则很可能这是该段落中的最后一次 Run 。
if (secondRun.NextSibling != null)
builder.MoveTo(secondRun.NextSibling);
else
builder.MoveTo(secondRun.ParentParagraph);
builder.EndBookmark("Aspose bookmark");
var saveOptions = new PdfSaveOptions();
saveOptions.OutlineOptions.BookmarksOutlineLevels.Add("Aspose bookmark", 1);
doc.Save("Output.docx", saveOptions);
在您的系统上 "Aspose.Words for .NET" 有三个备选选项。请选择符合您需求的一种,并按照分步说明进行操作:
我们的产品是完全跨平台的,并支持所有主要的 .NET 实现:
由于 .NET 代码不依赖于底层硬件或操作系统,而只依赖于虚拟机,因此您可以自由地为 Windows、macOS、Android、iOS 和 Linux 开发任何类型的软件。只需确保您已安装相应版本的.NET Framework, .NET Core, Windows Azure, Mono或Xamarin即可。
我们建议使用 Microsoft Visual Studio、Xamarin 和 MonoDevelop 集成开发环境来创建C#, F#, VB.NET应用程序。
更多详细信息请参阅产品文档。