文將使用Free Spire.Doc for Java免費控件來演示如何替換Word文檔中添加有書簽的文本段落。具體內容如下:
在運行代碼前,需將jar包導入IDEA。可通過下載產品包手動導入或在Maven下的pom.xml文件中添加如下引用。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
示例1 用文本替換書簽內容
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class ReplaceWithText {
public static void main(String[] args) {
//加載Word文檔
Document doc = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");
//定位到書簽"MyBookmark"
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
bookmarkNavigator.moveToBookmark("MyBookmark");
//使用文本替換原書簽的內容, false表示不保留原來的格式
bookmarkNavigator.replaceBookmarkContent("使用文本替換書簽內容", false);
//保存文檔
doc.saveToFile("output/ReplaceWithText.docx", FileFormat.Docx);
}
}
或使用Html String替換書簽內容
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ParagraphBase;
public class ReplaceWithHTMLString {
public static void main(String[] args) {
//加載Word文檔
Document doc = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");
//臨時添加一個section
Section tempSection = doc.addSection();
//添加段落到section并添加Html string到段落
String html = "使用文本替換書簽內容";
tempSection.addParagraph().appendHTML(html);
//獲取段落的第一項和最后一項
ParagraphBase firstItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getFirstItem();
ParagraphBase lastItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getLastItem();
//創建TextBodySelection對象
TextBodySelection selection = new TextBodySelection(firstItem, lastItem);
//創建TextBodyPart對象
TextBodyPart bodyPart = new TextBodyPart(selection);
//定位到書簽"MyBookmark"
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
bookmarkNavigator.moveToBookmark("MyBookmark");
//使用Html string替換原書簽的內容
bookmarkNavigator.replaceBookmarkContent(bodyPart);
//移除臨時添加的section
doc.getSections().remove(tempSection);
//保存結果文檔
doc.saveToFile("output/ReplaceWithHTMLString.docx", FileFormat.Docx);
}
}
替換前后對比:
示例2 用表格替換書簽內容
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class ReplaceWithTable {
public static void main(String[] args) {
//加載Word文檔
Document doc = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");
String[][] data =
{
new String[]{"名稱", "額定容量", "電源", "工作時間"},
new String[]{"LED-901充電式手電筒", "900mAH", "AC110V/220V", "26個小時"},
};
//創建表格
Table table = new Table(doc, true);
table.resetCells(2, 4);
for (int i = 0; i < data.length; i++) {
TableRow dataRow = table.getRows().get(i);
for (int j = 0; j < data[i].length; j++) {
dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
}
}
//創建TextBodyPart對象
TextBodyPart bodyPart= new TextBodyPart(doc);
bodyPart.getBodyItems().add(table);
//定位到書簽"MyBookmark"
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
bookmarkNavigator.moveToBookmark("MyBookmark");
//使用表格替換原書簽的內容
bookmarkNavigator.replaceBookmarkContent(bodyPart);
//保存文檔
doc.saveToFile("output/ReplaceWithTable.docx", FileFormat.Docx);
}
}
替換效果:
推薦閱讀:
如何添加、讀取、刪除Word書簽
件傳遞有兩種方式,冒泡和捕獲。事件傳遞定義了元素事件觸發的順序,在冒泡中,內部元素的事件會先被觸發,然后在觸發外部元素,如先觸發p元素然后觸發div元素。在捕獲事件中,外部元素先被觸發,然后內部事件觸發。addEventListener() 方法可以指定 "useCapture" 參數來設置傳遞類型:addEventListener(event, function, useCapture);
創建新的HTML元素(節點)appendChild()
HTML Collection與NodeList的區別:NodeList 與 HTMLCollection 有很多類似的地方。NodeList 與 HTMLCollection 都與數組對象有點類似,可以使用索引 (0, 1, 2, 3, 4, ...) 來獲取元素。NodeList 與 HTMLCollection 都有 length 屬性。節點列表不是一個數組!節點列表無法使用數組的方法: valueOf(), pop(), push(), 或 join() 。
頁數據提取的方法很多,從其基本原理來說很多就是通過模擬http請求,發送給服務器,然后接收響應,解析響應的結果。整個過程說簡單也簡單,說復雜也復雜。這里來整理下做過的一些事,走過的路,遇到的坑。
1,基本思路
這里舉一個java下載的例子,說明簡單的思路。
public void downPDF(String urlString, String filename, String pdf,
String chk, String chk1, String chk2, String chk3) throws Exception {
URL server = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) server
.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.addRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.addRequestProperty("Accept-Language", "zh-cn,zh;q=0.5");
connection.addRequestProperty("Accept-Encoding", "gzip, deflate");
connection.addRequestProperty("Accept-Charset","GB2312,utf-8;q=0.7,*;q=0.7");
connection.addRequestProperty("Cookie","chk=");
connection.addRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR2.0.50727; MS-RTC LM 8)");
// if (headers != null) {
// for (Header h : headers) {
// System.out.println(h.getName() + ":" + h.getValue());
// connection.addRequestProperty(h.getName(), h.getValue());
// }
// }
connection.connect();
InputStream is = connection.getInputStream();
OutputStream os = new FileOutputStream(filename);
byte[] buffer = new byte[1024 * 128];
if (true) {
int byteReaded = is.read(buffer);
while (byteReaded != -1) {
os.write(buffer, 0, byteReaded);
byteReaded = is.read(buffer);
}
*請認真填寫需求信息,我們會在24小時內與您取得聯系。