Aspose.Words 文档合并器 for .NET
Aspose.Words Document Merger for .NET 允许您在一个单一输出中编程 添加任何数量的 Word 兼容的文件 ,同时保持格式化、风格、页面布局、标题/脚本和字段.
在 Windows、Linux 和 macOS 上工作,不需要 Microsoft Office.
安装和设置
- 安装 NuGet 包
Aspose.Words
. - 在应用程序初创公司申请测量许可,以避免评估水标 参见 Metered Licensing .
- 在 安装指南 中查看框架要求/).
支持的输入(选择): DOC, DOCX, DOTX/DOTM, RTF, ODT, HTML/MHTML, PDF, TXT, WordML.支持的输出: DOCX、DOC、PDF、HTML(固定/流),MHTML、ODT、RTF、图像、EPUB、XPS等.
快速启动:添加多文档
using Aspose.Words;
// Destination (master) document
var master = new Document();
var builder = new DocumentBuilder(master);
// Start on a clean page
builder.InsertBreak(BreakType.SectionBreakNewPage);
// Append each source with chosen import mode
ImportFormatMode mode = ImportFormatMode.KeepSourceFormatting; // or UseDestinationStyles
foreach (string path in new [] { "intro.docx", "spec.docx", "annex.rtf" })
{
var src = new Document(path);
master.AppendDocument(src, mode);
// Optional: force each source to start on a new page
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.SectionBreakNewPage);
}
// Refresh fields (page numbers, TOC, cross‑refs)
master.UpdateFields();
master.Save("MergedOutput.pdf");
进口形式模式
- KeepSourceFormatting - 保留每个源的风格和直接格式化(适合品牌插件).
- UseDestinationStyles - 地图与目的地的定义相同的风格名称,以获得统一的外观.
控制部分和页面配置
开始每个文档在 新页面 , 连续的 或 在 / 添加页面的 上:
var sect = master.LastSection;
sect.PageSetup.SectionStart = SectionStart.NewPage; // Continuous, EvenPage, OddPage
通过复制统一边界/页面大小 PageSetup
从一个模板部分:
var template = new Document("template.docx");
var pageSetup = template.FirstSection.PageSetup;
foreach (Section s in master.Sections)
{
s.PageSetup.Orientation = pageSetup.Orientation;
s.PageSetup.PageWidth = pageSetup.PageWidth;
s.PageSetup.PageHeight = pageSetup.PageHeight;
s.PageSetup.TopMargin = pageSetup.TopMargin;
s.PageSetup.BottomMargin = pageSetup.BottomMargin;
s.PageSetup.LeftMargin = pageSetup.LeftMargin;
s.PageSetup.RightMargin = pageSetup.RightMargin;
}
头、脚和水标
保持源头/脚,或在合并后将其替换为主组:
// Copy headers/footers from a master template into every section
var hft = new Document("header-footer-template.docx");
foreach (Section s in master.Sections)
{
s.HeadersFooters.Clear();
s.HeadersFooters.AddClone(hft.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary]);
s.HeadersFooters.AddClone(hft.FirstSection.HeadersFooters[HeaderFooterType.FooterPrimary]);
}
// Add a simple text watermark
foreach (Section s in master.Sections)
{
var header = s.HeadersFooters[HeaderFooterType.HeaderPrimary] ?? new HeaderFooter(master, HeaderFooterType.HeaderPrimary);
if (header.ParentNode == null) s.HeadersFooters.Add(header);
var shape = new Shape(master, ShapeType.TextPlainText)
{
RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
RelativeVerticalPosition = RelativeVerticalPosition.Page,
WrapType = WrapType.None,
Rotation = -40,
Width = 400, Height = 100, Left = 100, Top = 200,
BehindText = true
};
shape.TextPath.Text = "CONFIDENTIAL";
header.AppendChild(shape);
}
字段、十字路口和TOC
合并后,更新字段,以便页数、参考和 ** 内容表** 匹配新配置:
master.UpdateFields();
master.UpdatePageLayout(); // ensures accurate page count
master.UpdateTableLayout(); // improves complex table pagination
如果您保留一个单一的主 TOC,请一次输入并让它自动恢复:
var b = new DocumentBuilder(master);
b.MoveToDocumentStart();
b.InsertTableOfContents("TOC \o \h \z \u"); // classic TOC switch set
master.UpdateFields();
密码保护输入与安全输出
using Aspose.Words.Loading;
var load = new LoadOptions { Password = "secret" };
var protectedDoc = new Document("protected.docx", load);
master.AppendDocument(protectedDoc, ImportFormatMode.KeepSourceFormatting);
// Save encrypted PDF
using Aspose.Words.Saving;
var pdf = new PdfSaveOptions
{
EncryptionDetails = new PdfEncryptionDetails("openPwd", "ownerPwd",
PdfEncryptionAlgorithm.RC4_128)
{ Permissions = PdfPermissions.DisallowAll }
};
master.Save("MergedEncrypted.pdf", pdf);
第一流 / 高容量流
using (var output = new MemoryStream())
{
var dst = new Document();
foreach (Stream srcStream in sourceStreams)
{
using var s = srcStream; // e.g., S3/Blob stream
var src = new Document(s); // auto‑detects format
dst.AppendDocument(src, ImportFormatMode.UseDestinationStyles);
}
dst.UpdateFields();
dst.Save(output, SaveFormat.Pdf);
output.Position = 0;
// return/output stream
}
性能提示
- Batch 存储在记忆中;呼叫
UpdateFields()
最后一次. - 偏好 UseDestinationStyles 当您希望在数十个输入中保持一致的风格.
- Dispose
Document
早期在长管道的例子,以减少峰值记忆.
处理混合格式(HTML、PDF、TXT)
- HTML/MHTML:与风格和图像(链接或嵌入)的进口).
- PDF :页面在可能的情况下作为重新流的内容进口;复杂的布局在输出时保存到固定格式(例如,PDF).
- TXT :添加为平面段落;应用基础风格后进口一致性.
最佳实践
- 提前有效 腐败/密码,以避免部分合并.
- 選擇 KeepSourceFormatting 當品牌忠誠有關時;為單一的外觀選取 UseDestinationStyles.
- 在每个附件之前输入一个 ** 部分中断** 以保护页面设置.
- 连接头/脚 如果您想要一个单一的公司模板.
- 更新字段 & TOC 作为最后步骤.
- 对于非常大的集合,分成组,并将组合结合起来,以保持记忆的稳定.
FAQ
**我可以强迫每个源头从一个新的页面开始吗?**是的,在每个页面之前插入一个部分或分裂 AppendDocument
打电话或设置 SectionStart
到 NewPage
.
**我如何在进口时保持目的地风格?**使用 ImportFormatMode.UseDestinationStyles
在 AppendDocument
.
**想要页面号码和 TOC 自动更新?**呼叫 UpdateFields()
(和 UpdatePageLayout()
精确的页面) 随后所有附件.
**我可以合并加密输入并产生加码输出吗?**是的,提供 LoadOptions.Password
用于输入和使用 PdfSaveOptions.EncryptionDetails
(或 OoxmlSaveOptions.Password
) 出售.
**办公室需要吗?**Aspose.Words 是一個獨立的圖書館.