Word合并的功能是有的,不过多数都是直接追加,即不需要去专门找位置。
而即便可以找位置了,合并功能没有提供指定位置插入的功能,那么也是白搭。
只能想方设法,让文档先在指定位置分为两个文档,然后在进行三个文档的合并。
举例如下:目前有A文档,现在需要在A文档的第2页插入B文档。
那么可以这么搞:
先把A文档根据页面2分为2个文档A1、A2,
然后A1合并B形成B1,B1再合并A2,形成B2即最终文档。
思路暂时有了,可是怎么实现呢?
AsPose.Words虽说很厉害的样子,但是度娘告诉我,它是没有明确的页面对象的,都是以内容Node节点为对象。
可是我不死心,继续各种换关键字找啊找,找到一个类似的,想想看是啥,又可以怎么启发我?
附文档链接:ASPOSE转换问题_已解决_博问_博客园 (cnblogs.com)
人家是要将Word每页转换为一个图片,这里不就可以按页码处理了吗?
那我也就先Save(根据页面来),不过不支持DocSaveOption页面格式。
还好可以支持Pdf保存的,那就先保存为PDF,再转换回Word好了,总归是个思路吧。
Aspose.Words.Saving.PdfSaveOptions pdfSaveOptions = new Aspose.Words.Saving.PdfSaveOptions();
pdfSaveOptions.PageIndex = 1;
pdfSaveOptions.PageCount = 1;
doc.Save("A1.doc", pdfSaveOptions);
对于A2.doc,类似的处理,处理好页面范围即可。
然后再把Pdf转为Word
//打开源PDF文档
Document pdfDocument = new Document(dataDir + "PDFToDOC.pdf");
//将文件保存为MS文档格式
pdfDocument.Save(dataDir + "PDFToDOC_out.doc", SaveFormat.Doc);
接下来就是合并了。
Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc = new Document(gDataDir + "TestFile.Source.doc");
// Set the appended document to start on a new page.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
// Append the source document using the original styles found in the source document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
dstDoc.Save(gDataDir + "TestFile.JoinNewPage Out.doc");
思路基本就这样了。