Unintelligible codes

Mac OS 10.14.4
java version “1.8.0_181”
Java™ SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot™ 64-Bit Server VM (build 25.181-b13, mixed mode)

original.pdf (12.5 KB)
font.zip (8.8 MB)

Export Image

without_font.jpg (668.9 KB)

public void pageJPEG(OutputStream fileOutputStream, int page) {
        Resolution resolution = new Resolution(300);
        JpegDevice jpegDevice = new JpegDevice(resolution, 100);
        jpegDevice.process(pdfDocument.getPages().get_Item(page), fileOutputStream);
    }

Export Image With Font Substitute
with_local_font.jpg (810.2 KB)

FontRepository.setReplaceNotFoundFonts(true);
FontRepository.setThreadStaticConfigEnabled(true);
FontRepository.addLocalFontPath(getFontsPath());
FontRepository.loadFonts();

public class CustomSubstitution extends CustomFontSubstitutionBase {
@Override
public boolean trySubstitute(OriginalFontSpecification originalFontSpecification, Font[] substitutionFont) {
    substitutionFont[0] = FontRepository.findFont("SimSun");
    return true;
}
}


public void pageJPEG(OutputStream fileOutputStream, int page) {
    FontRepository.getSubstitutions().add(new CustomSubstitution());
    pdfDocument.FontSubstitution.add(new Document.FontSubstitutionHandler() {
        public void invoke(Font font, Font newFont) {
            System.out.println("Warning: Font " + font.getFontName() + " was substituted with another font -> " + newFont.getFontName());
        }
    });
    Resolution resolution = new Resolution(300);
    JpegDevice jpegDevice = new JpegDevice(resolution, 100);
    jpegDevice.process(pdfDocument.getPages().get_Item(page), fileOutputStream);
}

Export PDF With Font Substitute
export.pdf (38.1 KB)

 public void resetPDFFont() throws IOException {
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
        pdfDocument.getPages().get_Item(1).accept(textFragmentAbsorber);
        for (TextFragment item : textFragmentAbsorber.getTextFragments()) {
            item.getTextState().setFont(FontRepository.findFont("SimSun"));
        }
        pdfDocument.save(***Save File Path**);
    }

@JamesGuo

Thank you for contacting support.

The source PDF document uses a custom font which is not embedded in the document. Therefore the text appears garbled when image or PDF is generated. So you may first try to set the font for the text and then render the image using below code snippet and Aspose.PDF for Java 19.6.

Document pdfDocument = new Document(dataDir + "block_2.pdf");
FontRepository.addLocalFontPath(dataDir);
Font f = FontRepository.findFont("SimSun");
f.setEmbedded(true);

TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
pdfDocument.getPages().get_Item(1).accept(textFragmentAbsorber);
for (TextFragment item : textFragmentAbsorber.getTextFragments()) {
item.getTextState().setFont(f);
}

Resolution resolution = new Resolution(300);
com.aspose.pdf.devices.JpegDevice jpegDevice = new com.aspose.pdf.devices.JpegDevice(resolution, 100);
jpegDevice.process(pdfDocument.getPages().get_Item(1), dataDir + "Aspose.PDF_19.6.jpg");

pdfDocument.save(dataDir + "Export_19.6.pdf");

Export_19.6.pdf
Aspose.PDF_19.6.jpg

We have also attached generated image and PDF document for your kind reference. Please feel free to contact us, while sharing some screenshots, if you need any further assistance.

Like the files you uploaded
Even import the font file, still cannot export correct
the correct image should like this (exported by pc pdf tools)
Also I tried import fonts like “DejaVuSans” used by pdf tools
block_3_backup.jpg (464.4 KB)
Screenshot_20190702_104305.png (518.2 KB)

@JamesGuo

Thank you for elaborating further.

We have been able to notice the differences as in attached screenshot and have logged a ticket with ID PDFJAVA-38672 in our issue management system for further investigations. We will let you know as soon as the ticket will be resolved.

Difference.PNG

We are sorry for the inconvenience.