Aspose.Words for .NET下载地址 https://soft51.cc/software/175811283999782847
Aspose.Words for .NET 是一个功能强大的 .NET 类库,专门用于处理 Microsoft Word 文档。它允许开发人员在不安装 Microsoft Office 的情况下,创建、修改、转换和处理 Word 文档。
操作系统支持:
开发框架要求:
IDE 选择:
必备工具:
# 使用 .NET CLI 创建新项目
dotnet new console -n AsposeWordsDemo
cd AsposeWordsDemo
AsposeWordsDemo/
├── AsposeWordsDemo.csproj
├── Program.cs
└── README.md
在 Visual Studio 中打开 Package Manager Console,执行以下命令:
Install-Package Aspose.Words
在项目根目录下执行:
dotnet add package Aspose.Words
直接编辑 .csproj
文件,添加包引用:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspose.Words" Version="24.1.0" />
</ItemGroup>
</Project>
然后执行:
dotnet restore
创建一个简单的测试程序验证安装是否成功:
using Aspose.Words;
namespace AsposeWordsDemo
{
class Program
{
static void Main(string[] args)
{
// 检查 Aspose.Words 版本
Console.WriteLine($"Aspose.Words 版本: {typeof(Document).Assembly.GetName().Version}");
// 创建一个新文档
Document doc = new Document();
Console.WriteLine("Aspose.Words 安装成功!");
Console.WriteLine("按任意键继续...");
Console.ReadKey();
}
}
}
未授权的 Aspose.Words 具有以下限制:
方法一:从文件设置许可证
using Aspose.Words;
public class LicenseManager
{
public static void SetLicense()
{
try
{
// 创建许可证对象
License license = new License();
// 设置许可证文件路径
string licensePath = @"C:\Licenses\Aspose.Words.NET.lic";
// 应用许可证
license.SetLicense(licensePath);
Console.WriteLine("许可证设置成功!");
}
catch (Exception ex)
{
Console.WriteLine($"许可证设置失败: {ex.Message}");
}
}
}
方法二:从流设置许可证
using Aspose.Words;
using System.IO;
using System.Reflection;
public class LicenseManager
{
public static void SetLicenseFromStream()
{
try
{
License license = new License();
// 从嵌入资源加载许可证
Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream("AsposeWordsDemo.Aspose.Words.NET.lic"))
{
license.SetLicense(stream);
}
Console.WriteLine("从流设置许可证成功!");
}
catch (Exception ex)
{
Console.WriteLine($"从流设置许可证失败: {ex.Message}");
}
}
}
方法三:从字节数组设置许可证
public static void SetLicenseFromByteArray()
{
try
{
License license = new License();
// 从配置或安全存储读取许可证字节
byte[] licenseBytes = GetLicenseBytesFromSecureStorage();
using (MemoryStream stream = new MemoryStream(licenseBytes))
{
license.SetLicense(stream);
}
Console.WriteLine("从字节数组设置许可证成功!");
}
catch (Exception ex)
{
Console.WriteLine($"从字节数组设置许可证失败: {ex.Message}");
}
}
private static byte[] GetLicenseBytesFromSecureStorage()
{
// 这里应该是从安全存储(如 Azure Key Vault、加密配置等)获取许可证
// 示例返回空数组
return new byte[0];
}
using Aspose.Words;
public static class LicenseConfiguration
{
private static bool _licenseSet = false;
private static readonly object _lockObject = new object();
public static void EnsureLicenseSet()
{
if (_licenseSet) return;
lock (_lockObject)
{
if (_licenseSet) return;
try
{
SetLicense();
_licenseSet = true;
}
catch (Exception ex)
{
Console.WriteLine($"警告:许可证设置失败,将使用评估模式: {ex.Message}");
}
}
}
private static void SetLicense()
{
License license = new License();
// 尝试多种方式设置许可证
string[] possiblePaths = {
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Aspose.Words.NET.lic"),
@"C:\Licenses\Aspose.Words.NET.lic",
Environment.GetEnvironmentVariable("ASPOSE_WORDS_LICENSE_PATH")
};
foreach (string path in possiblePaths)
{
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
license.SetLicense(path);
Console.WriteLine($"许可证从 {path} 设置成功");
return;
}
}
throw new FileNotFoundException("未找到有效的许可证文件");
}
}
using Aspose.Words;
using System;
using System.IO;
namespace AsposeWordsDemo
{
class Program
{
static void Main(string[] args)
{
try
{
// 设置许可证(可选)
// LicenseConfiguration.EnsureLicenseSet();
// 创建 Hello World 文档
CreateHelloWorldDocument();
Console.WriteLine("Hello World 文档创建成功!");
}
catch (Exception ex)
{
Console.WriteLine($"错误: {ex.Message}");
}
Console.WriteLine("按任意键退出...");
Console.ReadKey();
}
static void CreateHelloWorldDocument()
{
// 创建新文档
Document doc = new Document();
// 获取文档构建器
DocumentBuilder builder = new DocumentBuilder(doc);
// 添加内容
builder.Writeln("Hello World!");
builder.Writeln("这是我的第一个 Aspose.Words 文档。");
// 添加格式化文本
builder.Font.Size = 16;
builder.Font.Bold = true;
builder.Font.Color = System.Drawing.Color.Blue;
builder.Writeln("欢迎使用 Aspose.Words for .NET!");
// 重置格式
builder.Font.ClearFormatting();
// 添加段落
builder.Writeln();
builder.Writeln("功能特性:");
// 添加项目符号列表
builder.ListFormat.List = doc.Lists.Add(ListTemplate.BulletDefault);
builder.Writeln("创建和编辑 Word 文档");
builder.Writeln("支持多种文档格式");
builder.Writeln("高性能文档处理");
builder.Writeln("无需安装 Microsoft Office");
builder.ListFormat.RemoveNumbers();
// 保存文档
string outputPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "HelloWorld.docx");
doc.Save(outputPath);
Console.WriteLine($"文档已保存到: {outputPath}");
}
}
}
using Aspose.Words;
using Aspose.Words.Drawing;
using System;
using System.Drawing;
using System.IO;
namespace AsposeWordsDemo
{
public class GettingStartedExample
{
public static void RunExample()
{
try
{
Console.WriteLine("=== Aspose.Words for .NET 入门示例 ===\n");
// 1. 创建文档
CreateBasicDocument();
// 2. 文档格式转换
ConvertDocument();
// 3. 文档信息读取
ReadDocumentInfo();
Console.WriteLine("\n所有示例执行完成!");
}
catch (Exception ex)
{
Console.WriteLine($"执行错误: {ex.Message}");
}
}
/// <summary>
/// 创建包含各种元素的基本文档
/// </summary>
static void CreateBasicDocument()
{
Console.WriteLine("1. 创建基本文档...");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// 设置文档标题
builder.ParagraphFormat.StyleName = "Heading 1";
builder.Writeln("Aspose.Words 功能演示");
// 重置为正文样式
builder.ParagraphFormat.StyleName = "Normal";
// 添加文本段落
builder.Writeln("这是一个使用 Aspose.Words for .NET 创建的文档示例。");
// 插入表格
Table table = builder.StartTable();
// 第一行
builder.InsertCell();
builder.Write("功能");
builder.InsertCell();
builder.Write("描述");
builder.EndRow();
// 第二行
builder.InsertCell();
builder.Write("文档创建");
builder.InsertCell();
builder.Write("支持创建各种格式的文档");
builder.EndRow();
// 第三行
builder.InsertCell();
builder.Write("格式转换");
builder.InsertCell();
builder.Write("支持 DOC、DOCX、PDF、HTML 等格式互转");
builder.EndRow();
builder.EndTable();
// 添加分页符
builder.InsertBreak(BreakType.PageBreak);
// 添加图像(如果存在)
string imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logo.png");
if (File.Exists(imagePath))
{
builder.InsertImage(imagePath, 200, 100);
}
// 保存文档
string outputPath = GetOutputPath("BasicDocument.docx");
doc.Save(outputPath);
Console.WriteLine($" 文档已保存: {outputPath}");
}
/// <summary>
/// 演示文档格式转换
/// </summary>
static void ConvertDocument()
{
Console.WriteLine("\n2. 文档格式转换...");
string inputPath = GetOutputPath("BasicDocument.docx");
if (!File.Exists(inputPath))
{
Console.WriteLine(" 源文档不存在,跳过转换示例");
return;
}
// 加载文档
Document doc = new Document(inputPath);
// 转换为 PDF
string pdfPath = GetOutputPath("BasicDocument.pdf");
doc.Save(pdfPath);
Console.WriteLine($" 已转换为 PDF: {pdfPath}");
// 转换为 HTML
string htmlPath = GetOutputPath("BasicDocument.html");
doc.Save(htmlPath);
Console.WriteLine($" 已转换为 HTML: {htmlPath}");
// 转换为纯文本
string txtPath = GetOutputPath("BasicDocument.txt");
doc.Save(txtPath);
Console.WriteLine($" 已转换为 TXT: {txtPath}");
}
/// <summary>
/// 读取文档信息
/// </summary>
static void ReadDocumentInfo()
{
Console.WriteLine("\n3. 读取文档信息...");
string inputPath = GetOutputPath("BasicDocument.docx");
if (!File.Exists(inputPath))
{
Console.WriteLine(" 文档不存在,跳过信息读取");
return;
}
Document doc = new Document(inputPath);
// 读取内置属性
var builtInProps = doc.BuiltInDocumentProperties;
Console.WriteLine($" 文档标题: {builtInProps.Title}");
Console.WriteLine($" 作者: {builtInProps.Author}");
Console.WriteLine($" 创建时间: {builtInProps.CreatedTime}");
Console.WriteLine($" 修改时间: {builtInProps.LastSavedTime}");
Console.WriteLine($" 页数: {builtInProps.Pages}");
Console.WriteLine($" 字数: {builtInProps.Words}");
Console.WriteLine($" 字符数: {builtInProps.Characters}");
// 读取文档统计信息
doc.UpdateWordCount();
Console.WriteLine($" 段落数: {doc.BuiltInDocumentProperties.Paragraphs}");
}
/// <summary>
/// 获取输出文件路径
/// </summary>
static string GetOutputPath(string fileName)
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string outputDir = Path.Combine(desktopPath, "AsposeWordsOutput");
if (!Directory.Exists(outputDir))
{
Directory.CreateDirectory(outputDir);
}
return Path.Combine(outputDir, fileName);
}
}
}
// Program.cs
using AsposeWordsDemo;
namespace AsposeWordsDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("欢迎使用 Aspose.Words for .NET!");
Console.WriteLine("=====================================");
// 运行入门示例
GettingStartedExample.RunExample();
Console.WriteLine("\n按任意键退出...");
Console.ReadKey();
}
}
}
解决方案:
dotnet nuget locals all --clear
解决方案:
<!-- 在 .csproj 文件中确保正确的目标框架 -->
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
解决方案:
本教程介绍了 Aspose.Words for .NET 的基本环境搭建和安装过程,包括:
完成本教程后,您应该能够:
下一步,您可以继续学习第二章"核心概念与对象模型",深入了解 Aspose.Words 的架构设计和核心概念。
Aspose.Words for .NET下载地址 https://soft51.cc/software/175811283999782847