เอสโพซ.Words เอกสารเชื่อมต่อสำหรับ .NET
Aspose.Words Document Merger for .NET ช่วยให้คุณสามารถเข้าร่วมเอกสาร Word-Compatible หมายเลขใด ๆ ในรายการเดียวโดยการรักษารูปแบบสไตล์การจัดตั้งหน้าหัวข้อ / ฟุตและฟิลด์ การใช้งานทั่วไปรวมถึงรายงานการประกอบการบรรทัดสัญญาหรือการรวมเนื้อหาที่สร้างขึ้นสําหรับการจัดเก็บ.
ทํางานได้อย่างราบรื่นบน Windows, Linux และ macOS ไม่จําเป็นต้องใช้ Microsoft Office.
การติดตั้งและการตั้งค่า
- ติดตั้งแพคเกจ NuGet
Aspose.Words
. - ใช้ใบอนุญาตวัดในแอปพลิเคชันเริ่มต้นเพื่อหลีกเลี่ยงการประเมินตัวอักษร ฯลฯ ดู Metered Licenseing .
- ตรวจสอบความต้องการกรอบใน แนะนําการติดตั้ง .
ป้อนที่ได้รับการสนับสนุน (เลือก): 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 - แผนที่ชื่อสไตล์ที่คล้ายกันกับคําอธิบายของจุดหมายปลายทางเพื่อให้ดูเหมือนกัน.
การควบคุมส่วนและ Page Layout
เริ่มต้นแต่ละเอกสารด้วย ** Page ใหม่ ** , ** Continuous ** หรือ ** Even/Odd Page**:
var sect = master.LastSection;
sect.PageSetup.SectionStart = SectionStart.NewPage; // Continuous, EvenPage, OddPage
Unify margins / ขนาดหน้าโดยการคัดลอก PageSetup
จากส่วน template:
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();
Password-Protected Inputs และ Secure Output
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);
Stream-First / สูงปริมาณ Merges
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 appends ในหน่วยความจํา; โทรศัพท
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()
สําหรับหน้าแปลนที่แม่นยํา) หลังจากทั้งหมด appends.
**ฉันสามารถรวมเข้าเข้ารหัสและผลิตการส่งออกเข้ารับได้หรือไม?**ใช่ ให LoadOptions.Password
สําหรับการเข้าและใช PdfSaveOptions.EncryptionDetails
(หรือ OoxmlSaveOptions.Password
) สําหรับการผลิต.
**ต้องใช้สํานักงานหรือไม?**No. Aspose.Words เป็นห้องสมุดที่แยกต่างหาก.