使用aspose-words-21.5.0-jdk17.jar,源文件17页,转换PDF后有18页,请查看附件。格式错乱.zip (1001.7 KB)
安装此字体后依然存在问题,麻烦解决一下。字体太大无法上传,麻烦你们网上下载一下。
您提供的PDF文件中有不同的字体。
"转换后pdf.pdf"中的字体,由Microsoft Word for Mac创建:
- Arial v.6.80
- ArialUnicode MS v.1.00
- Courier New v.5.00
- DengXian v.1.13
- Microsoft YaHei v.6.11
- SimSun v.5.04
- Times New Roman v.5.01
"转换后的文件.pdf"中的字体,由Aspose.Words for Java 21.1.0创建:
- Arial v.7.00
- Courier New v.6.92
- MicrosoftJhengHei v.6.13
- SimSun v.5.16
- SourceHanSansCN-Normal v.1.0
- SourceHanSansCN-Regular v.1.0
- Times New Roman v.7.00
请确保使用相同的Microsoft Word和Aspose.Words的字体组。 如果字体不同,您将获得不同的PDF和不同的页数。
这些文章可能有所帮助:
- 非Windows系统上的字体
- 您可以使用
SetFontsFolder
方法指定具有字体的自定义文件夹: 从文件夹加载字体 - 您可以使用
IWarningCallback
捕获字体问题
你们可以在同一台机器上做验证啊,分别用Microsoft Word 和 Aspose.Words for Java 做转换,看是否页数相同,也是不同的。在同一台机器上验证应该保持一致才对。
您说得对。 它应该在同一台机器上保持一致,但仅在使用相同的字体时。
在我的计算机上使用Windows 10和Microsoft Word 2019,我转换您的文档并获得此PDF:
源文件.word2019.pdf 有20页。
此PDF具有以下嵌入式字体:
- Arial v.7.00
- Calibri v.6.23
- DengXian v.1.17
- Microsoft YaHei v.6.25
- SimSun v.5.16
- Times New Roman v.7.00
源文件.docx使用我的计算机上未安装的字体。对于这些缺少的字体,Microsoft Word在我的计算机上产生以下字体替换:
让我们尝试使用 Aspose.Words for Java 22.1.0将您的文档转换为 PDF 并检查字体替换警告:
import com.aspose.words.*;
import java.util.Iterator;
public class MainClass {
public static void main(String[] args) throws Exception {
License lic = new License();
lic.setLicense("yourlicense.lic");
Document doc = new Document("源文件.docx");
FontWarningCollector callback = new FontWarningCollector();
doc.setWarningCallback(callback);
doc.save("源文件.asposewordsjava.nofontsubstitution.pdf");
Iterator<WarningInfo> warnings = callback.FontWarnings.iterator();
while (warnings.hasNext())
System.out.println(warnings.next().getDescription());
}
private static class FontWarningCollector implements IWarningCallback {
/// <summary>
/// Called every time a warning occurs during loading/saving.
/// </summary>
public void warning(WarningInfo info) {
if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
FontWarnings.warning(info);
}
public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}
}
源文件.asposewordsjava.nofontsubstitution.pdf 有18页
以下字体替换信息将打印到控制台:
Font '思源黑体 CN Normal' has not been found. Using 'Arial Unicode MS' font instead. Reason: font info substitution.
Font '思源黑体 CN Regular' has not been found. Using 'Arial' font instead. Reason: font info substitution.
Font 'Montserrat' has not been found. Using 'Courier New' font instead. Reason: font info substitution.
这意味着aspose.words执行以下字体替换:
- 思源黑体 CN Normal -> Arial Unicode MS
- 思源黑体 CN Regular -> Arial
- Montserrat -> Courier
但Microsoft Word执行了不同的字体替换。让我们修复它并添加正确的字体替换规则:
import com.aspose.words.*;
import java.util.Iterator;
public class MainClass {
public static void main(String[] args) throws Exception {
License lic = new License();
lic.setLicense("yourlicense.lic");
Document doc = new Document("源文件.docx");
FontSettings fontSettings = FontSettings.getDefaultInstance();
FontSubstitutionSettings substitutionSettings = fontSettings.getSubstitutionSettings();
substitutionSettings.getFontInfoSubstitution().setEnabled(true);
TableSubstitutionRule tableSubstitution = substitutionSettings.getTableSubstitution();
tableSubstitution.addSubstitutes("思源黑体 CN Normal", "Microsoft YaHei");
tableSubstitution.addSubstitutes("思源黑体 CN Regular", "Microsoft YaHei");
tableSubstitution.addSubstitutes("Montserrat", "Calibri");
FontWarningCollector callback = new FontWarningCollector();
doc.setWarningCallback(callback);
doc.save("源文件.asposewordsjava.fontsubstitution.pdf");
Iterator<WarningInfo> warnings = callback.FontWarnings.iterator();
while (warnings.hasNext())
System.out.println(warnings.next().getDescription());
}
private static class FontWarningCollector implements IWarningCallback {
/// <summary>
/// Called every time a warning occurs during loading/saving.
/// </summary>
public void warning(WarningInfo info) {
if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
FontWarnings.warning(info);
}
public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}
}
以下字体替换信息将打印到控制台:
Font '思源黑体 CN Normal' has not been found. Using 'Microsoft YaHei' font instead. Reason: table substitution.
Font '思源黑体 CN Regular' has not been found. Using 'Microsoft YaHei' font instead. Reason: table substitution.
Font 'Montserrat' has not been found. Using 'Calibri' font instead. Reason: table substitution.
源文件.asposewordsjava.fontsubstitution.pdf 有20页。
此PDF具有以下嵌入式字体:
- Arial Version v.7.00
- Calibri Version v6.23
- Microsoft YaHei v.6.25
- SimSun Version v.5.16
- Times New Roman v.7.00
使用相同的字体时,Aspose.Words可以生成非常相似的PDF文件。源文章.word2019.pdf"和"源文件.asposesjava.fontsubstitution.pdf"都有20页. 它们看起来几乎相同,但在2个地方有点不同。