|
|
@ -34,80 +34,80 @@ import java.io.FileInputStream; |
|
|
|
@DisplayName("标签单元测试案例") |
|
|
|
public class TagUnitTest { |
|
|
|
|
|
|
|
@Tag("dev") |
|
|
|
@DisplayName("测试 @Tag dev") |
|
|
|
@Test |
|
|
|
public void testTagDev() throws Exception { |
|
|
|
String inputFilePath="C:\\Users\\gy051\\Desktop\\1223.doc"; |
|
|
|
String outputFilePath="D:\\JPG\\test.html"; |
|
|
|
InputStream is = new FileInputStream(inputFilePath); |
|
|
|
// InputStream is = FileHelper.convertWordToUTF8Stream(inputFilePath);
|
|
|
|
System.out.println("outputFilePath:"+outputFilePath); |
|
|
|
HWPFDocument wordDocument = new HWPFDocument(is); |
|
|
|
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); |
|
|
|
String rootPath = "D:\\JPG\\"; |
|
|
|
final int[] fileIndex = {0}; |
|
|
|
wordToHtmlConverter.setPicturesManager(new PicturesManager() { |
|
|
|
|
|
|
|
@Override |
|
|
|
public String savePicture(byte[] bytes, PictureType pictureType, String s, float v, float v1) { |
|
|
|
fileIndex[0]++; |
|
|
|
File file = new File(rootPath + fileIndex[0]+".png"); |
|
|
|
try { |
|
|
|
FileOutputStream out = new FileOutputStream(file); |
|
|
|
out.write(bytes); |
|
|
|
out.close(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return fileIndex[0]+".png"; // 返回图片的相对路径或者URL
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
wordToHtmlConverter.processDocument(wordDocument); |
|
|
|
Document htmlDocument = wordToHtmlConverter.getDocument(); |
|
|
|
|
|
|
|
// 将Document转换为XHTML
|
|
|
|
StringWriter writer = new StringWriter(); |
|
|
|
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
|
|
Transformer transformer = transformerFactory.newTransformer(); |
|
|
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
|
|
|
transformer.setOutputProperty(OutputKeys.METHOD, "html"); |
|
|
|
transformer.transform(new DOMSource(htmlDocument), new StreamResult(writer)); |
|
|
|
// 自定义处理标题
|
|
|
|
// W3CDocument w3cDocument = converter.getDocument();
|
|
|
|
// Range range = hwpfDocument.getRange();
|
|
|
|
// @Tag("dev")
|
|
|
|
// @DisplayName("测试 @Tag dev")
|
|
|
|
// @Test
|
|
|
|
// public void testTagDev() throws Exception {
|
|
|
|
// String inputFilePath="C:\\Users\\gy051\\Desktop\\1223.doc";
|
|
|
|
// String outputFilePath="D:\\JPG\\test.html";
|
|
|
|
// InputStream is = new FileInputStream(inputFilePath);
|
|
|
|
//// InputStream is = FileHelper.convertWordToUTF8Stream(inputFilePath);
|
|
|
|
// System.out.println("outputFilePath:"+outputFilePath);
|
|
|
|
// HWPFDocument wordDocument = new HWPFDocument(is);
|
|
|
|
// WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
|
|
|
|
// String rootPath = "D:\\JPG\\";
|
|
|
|
// final int[] fileIndex = {0};
|
|
|
|
// wordToHtmlConverter.setPicturesManager(new PicturesManager() {
|
|
|
|
//
|
|
|
|
// for (int i = 0; i < range.numParagraphs(); i++) {
|
|
|
|
// ParagraphProperties pp = range.getParagraph(i).getParagraphProperties();
|
|
|
|
// if (pp != null && pp.getParaStyleIndex() >= 0) {
|
|
|
|
// String styleName = hwpfDocument.getStyleSheet().getParagraphStyle(pp.getParaStyleIndex()).getName();
|
|
|
|
// // 判断标题样式并转换为对应的 HTML 标签
|
|
|
|
// if (styleName.startsWith("Heading")) {
|
|
|
|
// int headingLevel = Character.getNumericValue(styleName.charAt(7));
|
|
|
|
// // 这里只处理 H1 到 H6
|
|
|
|
// if (headingLevel >= 1 && headingLevel <= 6) {
|
|
|
|
// // 你可以将段落内容直接插入到 HTML 中,适当修改格式
|
|
|
|
// String headingTag = "h" + headingLevel;
|
|
|
|
// writer.append("<").append(headingTag).append(">")
|
|
|
|
// .append(range.getParagraph(i).text().trim())
|
|
|
|
// .append("</").append(headingTag).append(">");
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// writer.append("<p>").append(range.getParagraph(i).text().trim()).append("</p>");
|
|
|
|
// @Override
|
|
|
|
// public String savePicture(byte[] bytes, PictureType pictureType, String s, float v, float v1) {
|
|
|
|
// fileIndex[0]++;
|
|
|
|
// File file = new File(rootPath + fileIndex[0]+".png");
|
|
|
|
// try {
|
|
|
|
// FileOutputStream out = new FileOutputStream(file);
|
|
|
|
// out.write(bytes);
|
|
|
|
// out.close();
|
|
|
|
// } catch (Exception e) {
|
|
|
|
// e.printStackTrace();
|
|
|
|
// }
|
|
|
|
// return fileIndex[0]+".png"; // 返回图片的相对路径或者URL
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// 输出 HTML 字符串
|
|
|
|
String htmlString = writer.getBuffer().toString(); |
|
|
|
// 将 HTML 字符串写入文件
|
|
|
|
FileOutputStream out = new FileOutputStream(outputFilePath); |
|
|
|
byte[] bytes = htmlString.getBytes("UTF-8"); |
|
|
|
out.write(bytes); |
|
|
|
out.close(); |
|
|
|
|
|
|
|
} |
|
|
|
//
|
|
|
|
//
|
|
|
|
// });
|
|
|
|
// wordToHtmlConverter.processDocument(wordDocument);
|
|
|
|
// Document htmlDocument = wordToHtmlConverter.getDocument();
|
|
|
|
//
|
|
|
|
// // 将Document转换为XHTML
|
|
|
|
// StringWriter writer = new StringWriter();
|
|
|
|
// TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
|
|
|
// Transformer transformer = transformerFactory.newTransformer();
|
|
|
|
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
|
|
|
// transformer.setOutputProperty(OutputKeys.METHOD, "html");
|
|
|
|
// transformer.transform(new DOMSource(htmlDocument), new StreamResult(writer));
|
|
|
|
//// 自定义处理标题
|
|
|
|
//// W3CDocument w3cDocument = converter.getDocument();
|
|
|
|
//// Range range = hwpfDocument.getRange();
|
|
|
|
////
|
|
|
|
//// for (int i = 0; i < range.numParagraphs(); i++) {
|
|
|
|
//// ParagraphProperties pp = range.getParagraph(i).getParagraphProperties();
|
|
|
|
//// if (pp != null && pp.getParaStyleIndex() >= 0) {
|
|
|
|
//// String styleName = hwpfDocument.getStyleSheet().getParagraphStyle(pp.getParaStyleIndex()).getName();
|
|
|
|
//// // 判断标题样式并转换为对应的 HTML 标签
|
|
|
|
//// if (styleName.startsWith("Heading")) {
|
|
|
|
//// int headingLevel = Character.getNumericValue(styleName.charAt(7));
|
|
|
|
//// // 这里只处理 H1 到 H6
|
|
|
|
//// if (headingLevel >= 1 && headingLevel <= 6) {
|
|
|
|
//// // 你可以将段落内容直接插入到 HTML 中,适当修改格式
|
|
|
|
//// String headingTag = "h" + headingLevel;
|
|
|
|
//// writer.append("<").append(headingTag).append(">")
|
|
|
|
//// .append(range.getParagraph(i).text().trim())
|
|
|
|
//// .append("</").append(headingTag).append(">");
|
|
|
|
//// }
|
|
|
|
//// } else {
|
|
|
|
//// writer.append("<p>").append(range.getParagraph(i).text().trim()).append("</p>");
|
|
|
|
//// }
|
|
|
|
//// }
|
|
|
|
//// }
|
|
|
|
// // 输出 HTML 字符串
|
|
|
|
// String htmlString = writer.getBuffer().toString();
|
|
|
|
// // 将 HTML 字符串写入文件
|
|
|
|
// FileOutputStream out = new FileOutputStream(outputFilePath);
|
|
|
|
// byte[] bytes = htmlString.getBytes("UTF-8");
|
|
|
|
// out.write(bytes);
|
|
|
|
// out.close();
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
//
|
|
|
|
// @Tag("prod")
|
|
|
|