Java Word文档合并(支持图片)-增加去除分页符/分页标识

代码

	@Test
	public void mergeWords() {
		File newFile = new File( "C:/Users/Desktop/aaaab25.docx" );
		List<File> srcfile = new ArrayList<>();
		File file1 = new File( "C:/Users/Desktop/aaaab24.docx" );
		File file2 = new File( "C:/Users/Desktop/0530.docx" );
		srcfile.add( file1 );
		srcfile.add( file2 );
		try {
			OutputStream dest = new FileOutputStream( newFile );
			ArrayList<XWPFDocument> documentList = new ArrayList<>();
			XWPFDocument doc = null;
			for( int i = 0; i < srcfile.size(); i++ ) {
				FileInputStream in = new FileInputStream( srcfile.get( i ).getPath() );
				OPCPackage open = OPCPackage.open( in );
				XWPFDocument document = new XWPFDocument( open );
				documentList.add( document );
			}
			for( int i = 0; i < documentList.size(); i++ ) {
				doc = documentList.get( 0 );
				if( i != 0 ) {
					// 分页 documentList.get( i ).createParagraph().setPageBreak( true );
					appendBody( doc, documentList.get( i ) );
				}
			}
			// 分页 doc.createParagraph().setPageBreak( true );
			doc.write( dest );
		} catch( Exception e ) {
			e.printStackTrace();
		}
	}

public void appendBody( XWPFDocument src, XWPFDocument append ) throws Exception {
		CTBody src1Body = src.getDocument().getBody();
		CTBody src2Body = append.getDocument().getBody();
		List<XWPFPictureData> allPictures = append.getAllPictures();
		// 记录图片合并前及合并后的ID
		Map<String, String> map = new HashMap<>();
		for( XWPFPictureData picture : allPictures ) {
			String before = append.getRelationId( picture );
			// 将原文档中的图片加入到目标文档中
			String after = src.addPictureData( picture.getData(), Document.PICTURE_TYPE_PNG );
			map.put( before, after );
		}
		appendBody( src1Body, src2Body, map );
	}

	private void appendBody( CTBody src, CTBody append, Map<String, String> map ) throws Exception {
		XmlOptions optionsOuter = new XmlOptions();
		optionsOuter.setSaveOuter();
		String appendString = append.xmlText( optionsOuter );
		String rgex = "<[\\s]*?w:sectPr[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?w:sectPr[\\s]*?>";
		appendString = appendString.replaceAll( rgex, "" );
		// 去除分页符
		String srcString = src.xmlText().replaceAll( "<w:p><w:r><w:br w:type=\"page\"/></w:r></w:p>", "" ).replaceAll( "<w:r><w:br w:type=\"page\"/></w:r>", "" );
		String prefix = srcString.substring( 0, srcString.indexOf( ">" ) + 1 );
		String mainPart = srcString.substring( srcString.indexOf( ">" ) + 1, srcString.lastIndexOf( "<" ) );
		String sufix = srcString.substring( srcString.lastIndexOf( "<" ) );
		String addPart = appendString.substring( appendString.indexOf( ">" ) + 1, appendString.lastIndexOf( "<" ) );
		if( map != null && !map.isEmpty() ) {
			// 对xml字符串中图片ID进行替换
			for( Map.Entry<String, String> set : map.entrySet() ) {
				addPart = addPart.replace( set.getKey(), set.getValue() );
			}
		}
		// 将两个文档的xml内容进行拼接
		CTBody makeBody = CTBody.Factory.parse( prefix + mainPart + addPart + sufix );
		src.set( makeBody );
		// 文档分页 documentList.get(i).createParagraph().setPageBreak(true);
	}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值