Commit 4d197a7d authored by alex yao's avatar alex yao

test(FileUtils): FileUtils 测试

parent 0af574fa
package cn.com.poc.utils;
import cn.com.poc.common.utils.DocumentLoad;
import cn.com.poc.common.utils.FileUtils;
import cn.com.yict.framemax.core.spring.SingleContextInitializer;
import com.aspose.words.Document;
import com.aspose.words.HtmlSaveOptions;
import com.aspose.words.HtmlVersion;
import com.aspose.words.SaveFormat;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.runner.RunWith;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.io.*;
/**
* @author alex.yao
* @date 2025/6/12
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = SingleContextInitializer.class)
@WebAppConfiguration
public class FileUtilsTest {
@Test
public void test_wordConvertPDF() throws Exception {
String filePath = "";
File file = FileUtils.wordConvertPDF(new File(filePath));
System.out.println(DocumentLoad.loadPDF(file));
}
@Test
public void test_word2html() throws Exception {
String filePath = "C:\\Users\\52747\\Desktop\\test_motionDetailExport.docx";
String dataDir = "C:\\Users\\52747\\Desktop\\";
Document doc = new Document(filePath);
HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.HTML);
opts.setHtmlVersion(HtmlVersion.HTML_5);
opts.setExportImagesAsBase64(true);
opts.setExportPageMargins(true);
doc.save(dataDir + "TestFile.html", opts);
}
@Test
public void test_pdf2word() {
try {
String pdfFile = "C:\\Users\\52747\\Documents\\dataset\\EMSD_Regulatory_Services_Handbook_LPG16.pdf";
String wordFile = "C:\\Users\\52747\\Documents\\dataset\\EMSD_Regulatory_Services_Handbook_LPG16.doc";
// 加载PDF文档
PDDocument pdfDoc = PDDocument.load(new File(pdfFile));
int totalPages = pdfDoc.getNumberOfPages();
// 使用追加模式打开Word文件
try (Writer writer = new OutputStreamWriter(new FileOutputStream(wordFile, true), "UTF-8")) {
PDFTextStripper stripper = new PDFTextStripper();
stripper.setSortByPosition(true);
for (int i = 1; i <= totalPages; i++) {
stripper.setStartPage(i);
stripper.setEndPage(i);
String pageText = stripper.getText(pdfDoc);
// 追加当前页内容
writer.write(pageText);
writer.write("\f");
}
}
pdfDoc.close();
System.out.println("总计追加 " + totalPages + " 页到现有文件");
} catch (IOException e) {
System.err.println("转换失败:" + e.getMessage());
e.printStackTrace();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment