java aspose重叠_Aspose.Words - 在特定位置合并两个文档

在您的情况下,您需要实现IReplacingCallback接口来查找文本并将其替换为文档(rtf) . 使用以下代码示例来满足您的要求 .

Document doc = new Document(MyDir + "input.rtf");

FindandInsertDocument replacedoc = new FindandInsertDocument(MyDir + "document to be inserted.rtf");

doc.Range.Replace(new Regex("text which needs to be replaced with document"), replacedoc, false);

doc.Save(MyDir + "Out.rtf");

FindandInsertDocument类:

private class FindandInsertDocument : IReplacingCallback

{

private String path;

public FindandInsertDocument(String documentpath)

{

path = documentpath;

}

///

/// This method is called by the Aspose.Words find and replace engine for each match.

///

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

// This is a Run node that contains either the beginning or the complete match.

Node currentNode = e.MatchNode;

// The first (and may be the only) run can contain text before the match,

// in this case it is necessary to split the run.

if (e.MatchOffset > 0)

currentNode = SplitRun((Run)currentNode, e.MatchOffset);

// This array is used to store all nodes of the match for further removing.

ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.

int remainingLength = e.Match.Value.Length;

while (

(remainingLength > 0) &&

(currentNode != null) &&

(currentNode.GetText().Length <= remainingLength))

{

runs.Add(currentNode);

remainingLength = remainingLength - currentNode.GetText().Length;

// Select the next Run node.

// Have to loop because there could be other nodes such as BookmarkStart etc.

do

{

currentNode = currentNode.NextSibling;

}

while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));

}

// Split the last run that contains the match if there is any text left.

if ((currentNode != null) && (remainingLength > 0))

{

SplitRun((Run)currentNode, remainingLength);

runs.Add(currentNode);

}

// Create Document Builder and insert document

DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);

builder.MoveTo((Run)runs[runs.Count - 1]);

Document doc = new Document(path);

builder.InsertDocument(doc, ImportFormatMode.KeepSourceFormatting);

// Now remove all runs in the sequence.

foreach (Run run in runs)

run.Remove();

// Signal to the replace engine to do nothing because we have already done all what we wanted.

return ReplaceAction.Skip;

}

private static Run SplitRun(Run run, int position)

{

Run afterRun = (Run)run.Clone(true);

afterRun.Text = run.Text.Substring(position);

run.Text = run.Text.Substring(0, position);

run.ParentNode.InsertAfter(afterRun, run);

return afterRun;

}

}

我和Aspose一起担任开发人员传道人 .

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/295300.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

设置 Xcode 自动生成代码片段

一、什么是代码片段当在Xcode中输入dowhile并回车后&#xff0c;Xcode会出现下图所示的提示代码&#xff1a;这就是代码片段&#xff0c;目的是使程序员以最快的速度输入常用的代码片段&#xff0c;提高编程效率。该功能是从Xcode4开始引入的。在Xcode中的位置如下图所示&#…

C# 10 新特性 —— CallerArgumentExpression

C# 10 新特性 —— CallerArgumentExpressionIntroC# 10 支持使用 CallerArgumentExpression 来自动地获取调用方的信息&#xff0c;这可以简化我们现在的一些代码&#xff0c;让代码更加简洁&#xff0c;一起看下面的示例吧Caller InfoC# 在 5.0 的时候开始支持 Caller Info 自…

一款不错的编程字体Source Code Pro

我以前一直是用的MS自家的是Consolas的字体&#xff0c;这个字体基本上具有编程字体所需的所有要素&#xff1a;等宽、支持ClearType、中文字体大小合适&#xff0c;l和1&#xff0c;o和0很容易区分。非要挑刺的话就是字体比较小&#xff0c;9号和10号字区别不大&#xff0c;长…

Android之failed for task ‘:app:dexDebug‘致gradle编译OOM问题解决(android-support-multidex)

当我们的业务越来越多,项目里面的方法和第三方的jar包也会越来越多,然后昨晚就遇到了下面这个问题 UNEXPECTED TOP-LEVEL EXCEPTION:at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.j…

当代年轻人熬夜晚睡的原因找到了!

全世界只有3.14 % 的人关注了爆炸吧知识有人熬夜为了离梦想更近有人熬夜为了给自家爱豆做数据有人熬夜只是因为深夜才有点自己的时间还有人是因为“沉迷”这些优质视频号忘记要睡在过去一段时间里&#xff0c;视频号可能是微信迭代最多&#xff0c;变化最多&#xff0c;也受到最…

怎么安装SharePoint2013 preview 在SQL2012 和 Windows Server 2008 R2 SP1

微软上周发布了其支柱产品Office2013 和SharePoint2013 preview. 对于以SharePoint 吃饭的人当然是很兴奋。今天我在这里演示一下怎么安装SharePoint2013 preview 在SQL2012 和 Windows Server 2008 R2 SP1 。 1.需要在你的Active Directory&#xff08;AD)里建一个用户 ,我把它…

c++ 与 java_Java与C++比较

本文仅从片面的角度比较Java与C的一些特性&#xff0c;如有错误的地方&#xff0c;请指正。语言特性上的一些差异&#xff1a;1、Java没有无符号整数&#xff0c;C/C#都有。2、Java中不存在指针。Java的引用是功能弱化的指针&#xff0c;只能做“调用所指对象的方法”的操作&am…

Mac 登陆 去掉 其他用户

2019独角兽企业重金招聘Python工程师标准>>> 打开 终端 sudo defaults write /Library/Preferences/com.apple.loginwindow SHOWOTHERUSERS_MANAGED -bool FALSE 转载于:https://my.oschina.net/liuchuanfeng/blog/617387

使用 Windbg 分析一个 异步操作 引发的 Crash 异常

上周我们收到了一个客户的紧急求助&#xff0c;他们的一个 iis应用程序池 经历了频繁重启&#xff0c;即使从错误日志中也不得到任何有用的信息&#xff0c;异常信息如下&#xff1a;System.NullReferenceException : Object reference not set to an instance of an object. S…

wxGlade的图标,原来是来自蒙德里安的名画!

一直用wxGlade做GUI的&#xff0c;今天突然发现它的图标和一副油画很像。 wxGlade的图标&#xff0c;图标的文件名竟然就叫做mondrian.ico 蒙德里安创造了很多这种纯粹的基本要素的作品&#xff0c;下面是其中之一&#xff0c;《构图》&#xff08;Composition 1929 - Piet Mon…

SAP HANA解读-2012 SAP商业同略会分享

7月26日和27日&#xff0c;我受邀参加了SAP在国家会议中心举办的“蕴韬略促转变共发展”为主题的中国商业同略会&#xff0c;下面就参会的一些感想和大家分享一下。 SAP中国商业同略会是第二次在北京举办&#xff0c;此次大会汇聚国内外知名商业领袖、企业高层、行业权威、专家…

java日期加减秒_Java日期——年、月、日、时、分、秒、周加减计算

Java日期——年、月、日、时、分、秒、周加减计算Java日期——年、月、日、时、分、秒、周加减计算1.Pom依赖joda-timejoda-time2.9.92.示例代码package com.example.demo.controller;import org.joda.time.DateTime;import java.text.SimpleDateFormat;import java.util.Date;…

遍历Map的四种方法

map遍历经常忘记&#xff0c;老是在网上找&#xff0c;干脆自己记录下来 public static void main(String[] args) {Map<String, String> map new HashMap<String, String>();map.put("1", "value1");map.put("2", "value2&qu…

不可思议!这篇全篇脏话的文章竟然发表了

全世界只有3.14 % 的人关注了爆炸吧知识一教授为了抗议三流科学杂志发送垃圾邮件&#xff0c;回复了一篇全文只重复七个脏话字眼的论文&#xff0c;竟被出版&#xff01;这是十几年前&#xff0c;麻省理工大学的一个教授埃迪科勒&#xff0c;发表的一篇名为 Get me off Your Fu…

设置圆角、定向设置圆角-按钮等控件

为什么80%的码农都做不了架构师&#xff1f;>>> //定向设置圆角UIBezierPath *maskPath [UIBezierPath bezierPathWithRoundedRect:whiteView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];CASha…

C# 10 新特性 —— Lambda 优化

C# 10 新特性 —— Lambda 优化IntroC# 10 对于 Lambda 做了很多的优化&#xff0c;我们可以在 C# 中更加方便地使用委托和 Lambda 了&#xff0c;下面就来看一些示例Lambda EnhancementsNatural types for lambdasC# 10 可以更好做类型推断&#xff0c;很多时候编译器可以自动…

40个最好的Tumblr主题

如果安装了一款较好的Tumblr主题&#xff0c;你的Tumblr空间将焕然一新。然而找到一款合适的主题并不是一件容易的事&#xff0c;这正是本文中我整理那么多优质的Tumblr模板作为灵感的原因。其中有一些免费的Tumblr主题&#xff0c;另外的一些付费的Tumblr主题也确实很棒&#…

以太网

以太网将数据链路层的功能划分到了两个不同的子层&#xff1a; 1) 逻辑链路控制 (LLC) 子层 2) 介质访问控制 (MAC) 子层。 逻辑链路控制 (LLC) 子层&#xff1a; 以太网&#xff0c;IEEE 802.2 标准规范 LLC 子层的功能&#xff0c;而 802.3 标准规范 MAC 子层…

Android之Universal-Image-loader

一、介绍 Android-Universal-Image-Loader是一个开源的UI组件程序&#xff0c;该项目的目的是提供一个可重复使用的仪器为异步图像加载&#xff0c;缓存和显示。所以&#xff0c;如果你的程序里需要这个功能的话&#xff0c;那么不妨试试它。因为已经封装好了一些类和方法。我们…

java 有没有with语句_Java中的try-with-resources语句

在这个Java程序示例中&#xff1a;package test;import java.sql.DriverManager;import java.sql.Connection;import java.sql.Statement;public class Test{private static void example(){String url "jdbc:oracle:thin://localhost:7856/xe";String user "…