Asked Mar 4th, 3:12 AM 42 1 0
  • 42 1 0
0

Đọc template Word trong Java?

Share
  • 42 1 0

Mình có tham khảo tài liệu https://www.javacodegeeks.com/2012/07/java-word-docx-documents-with-docx4j.html#comments

Khi mình làm theo mẫu run app thì được báo lỗi null pointer. Không biết khắc phục như thế nào ạ?

import javax.xml.bind.JAXBElement;

import org.apache.commons.lang3.StringUtils;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import org.docx4j.wml.ContentAccessor;
import org.docx4j.wml.P;
import org.docx4j.wml.Text;

public class Docx4jHelper {
	public static void main(String[] args) throws Docx4JException, IOException {
		// TODO Auto-generated method stub
		Docx4jHelper docx4jHelper = new Docx4jHelper();
		docx4jHelper.writeDocxToStream(docx4jHelper.getTemplate("C:\\Users\\my.vtt\\Documents\\template.docx"), "D:\\newExample");

	}
	private WordprocessingMLPackage getTemplate(String name) throws Docx4JException, FileNotFoundException {
		WordprocessingMLPackage template = WordprocessingMLPackage.load(new FileInputStream(new File(name)));
		return template;
	}
	
	private static List<Object> getAllElementFromObjects(Object obj, Class<?> toSearch) {
		List<Object> result = new ArrayList<Object>();
		if (obj instanceof JAXBElement) obj = ((JAXBElement<?>) obj).getValue();
		
		if (obj.getClass().equals(toSearch)) {
			result.add(obj);
		}else if (obj instanceof ContentAccessor) {
			List<?> chidren = ((ContentAccessor) obj).getContent();
			for (Object child : chidren) {
				result.addAll(getAllElementFromObjects(child, toSearch));
			}
		}
		return result;
	}
	
	private void replacePlaceholder(WordprocessingMLPackage template, String name, String placeholder) {
		List<Object> texts = getAllElementFromObjects(template.getMainDocumentPart(), Text.class);
		for (Object text : texts) {
			Text textElement = (Text) text;
			if (textElement.getValue().equals(placeholder)) {
				textElement.setValue(name);
			}
		}
	}
	
	private void writeDocxToStream(WordprocessingMLPackage template, String target) throws IOException, Docx4JException{
		File file = new File(target);
		template.save(file);
	}
	
	private void replaceParagraph(String placeholder, String textToAdd, WordprocessingMLPackage template, ContentAccessor addTo) {
		List<Object> paragraphs = getAllElementFromObjects(template.getMainDocumentPart(), P.class);
		
		P toReplace = null;
		for (Object p : paragraphs) {
			List<Object> texts =  getAllElementFromObjects(p, Text.class);
			for (Object t : texts) {
				Text content = (Text) t;
				if (content.getValue().equals(placeholder)) {
					toReplace = (P) p;
					break;
				}
			}
		}
		String as[] = StringUtils.splitPreserveAllTokens(textToAdd, '\n');
		
		for (int i = 0; i < as.length; i++) {
			String ptext = as[i];
			P coppy = (P) XmlUtils.deepCopy(toReplace);
			
			List texts = getAllElementFromObjects(coppy, Text.class);
			if (texts.size() > 0) {
				Text textToReplace = (Text) texts.get(0);
				textToReplace.setValue(ptext);
			}
			
			addTo.getContent().add(coppy);
			
			((ContentAccessor)toReplace.getParent()).getContent().remove(toReplace);
		}
	}

}