We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Words for Java pdf转html,pdf转换后部分文字消失

pdf转换后部分文字消失.zip (347.6 KB)

代码:
private void pdf2Html(String sourceFileName, String targetFileName) throws Exception {
com.aspose.pdf.Document document = new com.aspose.pdf.Document(sourceFileName);

File path = new File(targetFileName.substring(0, targetFileName.lastIndexOf('.')));
path.mkdirs();

Resolution resolution = new Resolution(200); 
JpegDevice device = new JpegDevice(1080,1920,resolution);
PageCollection pages = document.getPages();
for (int i = 1; i <= pages.size(); i++){
	File file = new File(path+"/"+(i-1)+".jpg");// 输出路径 
	file.createNewFile();
	FileOutputStream fileOS = new FileOutputStream(file); 
	device.process(pages.get_Item(i), fileOS); 
	fileOS.close(); 
}
buildSlideMobileHtml(targetFileName, path, pages.size());
document.close();

}

@xiangma

您似乎正在将 PDF 转换为 JPG。我们已经使用 21.7 版本的 API 测试了您的代码片段,并获得了缺少文本的附加图像:

Converted_Image_1.jpg (451.7 KB)

为了更正,我们在问题跟踪系统中将工单记录为 PDFJAVA-40711。我们将调查其详细信息,并及时通知您其整改状态。请耐心等待,给我们一些时间。

我们对造成的不便很抱歉。

@xiangma

他使用的字体没有嵌入到 PDF 中,也不在系统中。因此,有两种方法可以修复对话:

  • 安装所有使用的字体或通过代码 FontRepository.addLocalFontPath(PATH_TO_FONTS) 设置字体的路径;

  • 用下面的代码用其他兼容字体替换不存在的字体(使用转换前安装的 SimSan.ttf 字体进行测试)。
FontRepository.getSubstitutions().add(new FontSubstitute());
Document pdfDocument = new Document(dataDir + "pdf+¬++¦=¦++++-++-v-º.pdf");
for (Page page : pdfDocument.getPages()) {
    long totalStart = System.currentTimeMillis();
    java.io.OutputStream imageStream = new java.io.FileOutputStream(dataDir + "Converted_Image_" + page.getNumber() + ".jpg");
    com.aspose.pdf.devices.Resolution resolution = new com.aspose.pdf.devices.Resolution(300);
    com.aspose.pdf.devices.JpegDevice jpegDevice = new com.aspose.pdf.devices.JpegDevice(resolution);
    jpegDevice.process(page, imageStream);
    imageStream.close();
    long totalEnd = System.currentTimeMillis();
    System.out.println("Page Number=" + page.getNumber() + " Total time taken was " + (totalEnd - totalStart) / 1000 + " seconds for conversion");
}
...
public class FontSubstitute extends CustomFontSubstitutionBase {
    public boolean trySubstitute(OriginalFontSpecification originalFontSpecification, com.aspose.pdf.Font[] substitutionFont) {         

        if (!originalFontSpecification.isEmbedded()) {
            substitutionFont[0] = FontRepository.findFont("SimSun");
            System.out.print(originalFontSpecification.getOriginalFontName() + " was changed to " + substitutionFont[0].getFontName());
            return true;
        }

        return false;
    }
}