第一種郵件合併插入java
public class InsertDocumentAtMailMergeBlobHandler implements IFieldMergingCallback { /** * This handler makes special processing for the "Document_1" field. * The field value contains the path to load the document. * We load the document and insert it into the current merge field. */ public void fieldMerging(FieldMergingArgs e) throws Exception { if (Pattern.matches("NestDoc_.*" , e.getDocumentFieldName())) { // Use document builder to navigate to the merge field with the specified name. DocumentBuilder builder = new DocumentBuilder(e.getDocument()); builder.moveToMergeField(e.getDocumentFieldName()); // The name of the document to load and insert is stored in the field value. Document subDoc = new Document((String)e.getFieldValue()); // Insert the document. AsposeWordUitl. insertDocument(builder.getCurrentParagraph(), subDoc); // The paragraph that contained the merge field might be empty now and you probably want to delete it. if (!builder.getCurrentParagraph().hasChildNodes()) builder.getCurrentParagraph().remove(); // Indicate to the mail merge engine that we have inserted what we wanted. e.setText( null); } } public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception { // Do nothing. } }
郵件合併域以NestDoc_開頭如:node
context.put("NestDoc_doc2", "template/nestDoc.doc" );app
第二種,利用書籤插入ide
@Override public void insertDocument(String bookmarkName, Document nestDoc) { try { Bookmark bookmark = this. document.getRange().getBookmarks().get(bookmarkName); bookmark.setText( ""); //將標籤內容置空 DocumentBuilder builder = new DocumentBuilder(this.document ); builder.moveToBookmark(bookmarkName); //移至標籤處 AsposeWordUitl. insertDocument(bookmark.getBookmarkStart().getParentNode(),nestDoc); } catch (Exception e) { throw new ReportException(e); } }
Document dstDoc = new Document("TestFile.Destination.doc"); Document srcDoc = new Document("TestFile.Source.doc"); // Append the source document to the destination document while keeping the original formatting of the source document. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); dstDoc.save("TestFile Out.docx");
public class AsposeWordUitl { private AsposeWordUitl() { super(); } /** * 在指定的節點後面插入入另外一個文檔的內容 * * @param insertAfterNode Node in the destination document after which the content * should be inserted. This node should be a block level node (paragraph or table). * @param srcDoc The document to insert. */ public static void InsertDocumentWithSectionFormatting(Node insertAfterNode, Document srcDoc) throws Exception { // Make sure that the node is either a pargraph or table. if ((insertAfterNode.getNodeType() != NodeType. PARAGRAPH) & (insertAfterNode.getNodeType() != NodeType. TABLE)) throw new Exception( "The destination node should be either a paragraph or table."); // Document to insert srcDoc into. Document dstDoc = (Document)insertAfterNode.getDocument(); // To retain section formatting, split the current section into two at the marker node and then import the content from srcDoc as whole sections. // The section of the node which the insert marker node belongs to Section currentSection = (Section)insertAfterNode.getAncestor(NodeType.SECTION ); // Don't clone the content inside the section, we just want the properties of the section retained. Section cloneSection = (Section)currentSection.deepClone(false ); // However make sure the clone section has a body, but no empty first paragraph. cloneSection.ensureMinimum(); cloneSection.getBody().getFirstParagraph().remove(); // Insert the cloned section into the document after the original section. insertAfterNode.getDocument().insertAfter(cloneSection, currentSection); // Append all nodes after the marker node to the new section. This will split the content at the section level at // the marker so the sections from the other document can be inserted directly. Node currentNode = insertAfterNode.getNextSibling(); while (currentNode != null) { Node nextNode = currentNode.getNextSibling(); cloneSection.getBody().appendChild(currentNode); currentNode = nextNode; } // This object will be translating styles and lists during the import. NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.USE_DESTINATION_STYLES ); // Loop through all sections in the source document. for (Section srcSection : srcDoc.getSections()) { Node newNode = importer.importNode(srcSection, true); // Append each section to the destination document. Start by inserting it after the split section. dstDoc.insertAfter(newNode, currentSection); currentSection = (Section)newNode; } } /** * Inserts content of the external document after the specified node. * 將忽略插入文檔中的空白區與格式區 * Section breaks and section formatting of the inserted document are ignored. * * @param insertAfterNode Node in the destination document after which the content * should be inserted. This node should be a block level node (paragraph or table). * @param srcDoc The document to insert. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void insertDocument(Node insertAfterNode, Document srcDoc) throws Exception { // 確保節點是一個段落或是表格 if ((insertAfterNode.getNodeType() != NodeType. PARAGRAPH) & (insertAfterNode.getNodeType() != NodeType. TABLE)) throw new IllegalArgumentException( "The destination node should be either a paragraph or table."); // 將插入以目標段落的父容器中 CompositeNode dstStory = insertAfterNode.getParentNode(); // 爲導入對象添加風格 NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING ); // 遍歷源文檔中的全部部分。 for (Section srcSection : srcDoc.getSections()) { //遍歷全部的塊級別節點(段落和表)的主體部分。 for (Node srcNode : ((Iterable<Node>) srcSection.getBody())) { // 跳過節點若是是最後一個空段部分。 if (srcNode.getNodeType() == (NodeType. PARAGRAPH)) { Paragraph para = (Paragraph)srcNode; if (para.isEndOfSection() && !para.hasChildNodes()) continue; } // 這將建立一個克隆節點的,插入到目標文檔。 Node newNode = importer.importNode(srcNode, true); // 在引用的節點後插入新了節點 dstStory.insertAfter(newNode, insertAfterNode); insertAfterNode = newNode; } } } }