使用pdfbox,将PDF转图片后,中文乱码问题

时间:2025-02-07 17:52:19

参考:/p/b8692da38692

在项目中创建这个包,然后复制下面这个类

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .type1.Type1Font;

final class FontMapperImpl implements FontMapper {
    private static final FontCache fontCache = new FontCache();
    private FontProvider fontProvider;
    private Map<String, FontInfo> fontInfoByName;
    private final TrueTypeFont lastResortFont;
    private final Map<String, List<String>> substitutes = new HashMap();

    FontMapperImpl() {
        ("Courier", ("CourierNew", "CourierNewPSMT", "LiberationMono", "NimbusMonL-Regu"));
        ("Courier-Bold", ("CourierNewPS-BoldMT", "CourierNew-Bold", "LiberationMono-Bold", "NimbusMonL-Bold"));
        ("Courier-Oblique", ("CourierNewPS-ItalicMT", "CourierNew-Italic", "LiberationMono-Italic", "NimbusMonL-ReguObli"));
        ("Courier-BoldOblique", ("CourierNewPS-BoldItalicMT", "CourierNew-BoldItalic", "LiberationMono-BoldItalic", "NimbusMonL-BoldObli"));
        ("Helvetica", ("ArialMT", "Arial", "LiberationSans", "NimbusSanL-Regu"));
        ("Helvetica-Bold", ("Arial-BoldMT", "Arial-Bold", "LiberationSans-Bold", "NimbusSanL-Bold"));
        ("Helvetica-Oblique", ("Arial-ItalicMT", "Arial-Italic", "Helvetica-Italic", "LiberationSans-Italic", "NimbusSanL-ReguItal"));
        ("Helvetica-BoldOblique", ("Arial-BoldItalicMT", "Helvetica-BoldItalic", "LiberationSans-BoldItalic", "NimbusSanL-BoldItal"));
        ("Times-Roman", ("TimesNewRomanPSMT", "TimesNewRoman", "TimesNewRomanPS", "LiberationSerif", "NimbusRomNo9L-Regu"));
        ("Times-Bold", ("TimesNewRomanPS-BoldMT", "TimesNewRomanPS-Bold", "TimesNewRoman-Bold", "LiberationSerif-Bold", "NimbusRomNo9L-Medi"));
        ("Times-Italic", ("TimesNewRomanPS-ItalicMT", "TimesNewRomanPS-Italic", "TimesNewRoman-Italic", "LiberationSerif-Italic", "NimbusRomNo9L-ReguItal"));
        ("Times-BoldItalic", ("TimesNewRomanPS-BoldItalicMT", "TimesNewRomanPS-BoldItalic", "TimesNewRoman-BoldItalic", "LiberationSerif-BoldItalic", "NimbusRomNo9L-MediItal"));
        ("Symbol", ("Symbol", "SymbolMT", "StandardSymL"));
        ("ZapfDingbats", ("ZapfDingbatsITC", "Dingbats", "MS-Gothic"));
        ("STSong-Light", ("AdobeSongStd-Light","DengXian"));//自己加的
        Iterator var1 = ().iterator();

        while(()) {
            String baseName = (String)();
            if (!(baseName)) {
                String mappedName = (baseName);
                (baseName, (mappedName));
            }
        }

        try {
            String ttfName = "org/apache/pdfbox/resources/ttf/";
            URL url = ().getResource(ttfName);
            if (url == null) {
                throw new IOException("Error loading resource: " + ttfName);
            } else {
                InputStream ttfStream = ();
                TTFParser ttfParser = new TTFParser();
                 = (ttfStream);
            }
        } catch (IOException var5) {
            throw new RuntimeException(var5);
        }
    }

    public synchronized void setProvider(FontProvider fontProvider) {
         = (());
         = fontProvider;
    }

    public synchronized FontProvider getProvider() {
        if ( == null) {
            ();
        }

        return ;
    }

    public FontCache getFontCache() {
        return fontCache;
    }

    private Map<String, FontInfo> createFontInfoByName(List<? extends FontInfo> fontInfoList) {
        Map<String, FontInfo> map = new LinkedHashMap();
        Iterator var3 = ();

        while(()) {
            FontInfo info = (FontInfo)();
            Iterator var5 = (()).iterator();

            while(()) {
                String name = (String)();
                (name, info);
            }
        }

        return map;
    }

    private Set<String> getPostScriptNames(String postScriptName) {
        Set<String> names = new HashSet();
        (postScriptName);
        (("-", ""));
        return names;
    }

    private List<String> copySubstitutes(String postScriptName) {
        return new ArrayList((Collection)(postScriptName));
    }

    public void addSubstitute(String match, String replace) {
        if (!(match)) {
            (match, new ArrayList());
        }

        ((List)(match)).add(replace);
    }

    private List<String> getSubstitutes(String postScriptName) {
        List<String> subs = (List)((" ", ""));
        return subs != null ? subs : ();
    }

    private String getFallbackFontName(PDFontDescriptor fontDescriptor) {
        String fontName;
        if (fontDescriptor != null) {
            boolean isBold = false;
            String name = ();
            if (name != null) {
                String lower = ().toLowerCase();
                isBold = ("bold") || ("black") || ("heavy");
            }

            if (()) {
                fontName = "Courier";
                if (isBold && ()) {
                    fontName = fontName + "-BoldOblique";
                } else if (isBold) {
                    fontName = fontName + "-Bold";
                } else if (()) {
                    fontName = fontName + "-Oblique";
                }
            } else if (()) {
                fontName = "Times";
                if (isBold && ()) {
                    fontName = fontName + "-BoldItalic";
                } else if (isBold) {
                    fontName = fontName + "-Bold";
                } else if (()) {
                    fontName = fontName + "-Italic";
                } else {
                    fontName = fontName + "-Roman";
                }
            } else {
                fontName = "Helvetica";
                if (isBold && ()) {
                    fontName = fontName + "-BoldOblique";
                } else if (isBold) {
                    fontName = fontName + "-Bold";
                } else if (()) {
                    fontName = fontName + "-Oblique";
                }
            }
        } else {
            fontName = "Times-Roman";
        }

        return fontName;
    }

    public FontMapping<TrueTypeFont> getTrueTypeFont(String baseFont, PDFontDescriptor fontDescriptor) {
        TrueTypeFont ttf = (TrueTypeFont)(, baseFont);
        if (ttf != null) {
            return new FontMapping(ttf, false);
        } else {
            String fontName = (fontDescriptor);
            ttf = (TrueTypeFont)(, fontName);
            if (ttf == null) {
                ttf = ;
            }

            return new FontMapping(ttf, true);
        }
    }

    public FontMapping<FontBoxFont> getFontBoxFont(String baseFont, PDFontDescriptor fontDescriptor) {
        FontBoxFont font = (baseFont);
        if (font != null) {
            return new FontMapping(font, false);
        } else {
            String fallbackName = (fontDescriptor);
            FontBoxFont font2 = (fallbackName);
            if (font2 == null) {
                font = ;
            }else {
                font= font2;
            }

            return new FontMapping((FontBoxFont)font, true);
        }
    }

    private FontBoxFont findFontBoxFont(String postScriptName) {
        Type1Font t1 = (Type1Font)(, postScriptName);
        if (t1 != null) {
            return t1;
        } else {
            TrueTypeFont ttf = (TrueTypeFont)(, postScriptName);
            if (ttf != null) {
                return ttf;
            } else {
                OpenTypeFont otf = (OpenTypeFont)(, postScriptName);
                return otf != null ? otf : null;
            }
        }
    }

    private FontBoxFont findFont(FontFormat format, String postScriptName) {
        if (postScriptName == null) {
            return null;
        } else {
            if ( == null) {
                ();
            }

            FontInfo info = (format, postScriptName);
            if (info != null) {
                return ();
            } else {
                info = (format, ("-", ""));
                if (info != null) {
                    return ();
                } else {
                    Iterator var4 = (postScriptName).iterator();

                    do {
                        if (!()) {
                            info = (format, (",", "-"));
                            if (info != null) {
                                return ();
                            }

                            info = (format, postScriptName + "-Regular");
                            if (info != null) {
                                return ();
                            }

                            return null;
                        }

                        String substituteName = (String)();
                        info = (format, substituteName);
                    } while(info == null);

                    return ();
                }
            }
        }
    }

    private FontInfo getFont(FontFormat format, String postScriptName) {
        if (("+")) {
            postScriptName = ((43) + 1);
        }

        FontInfo info = (FontInfo)(postScriptName);
        return info != null && () == format ? info : null;
    }

    public CIDFontMapping getCIDFont(String baseFont, PDFontDescriptor fontDescriptor, PDCIDSystemInfo cidSystemInfo) {
        OpenTypeFont otf1 = (OpenTypeFont)(, baseFont);
        if (otf1 != null) {
            return new CIDFontMapping(otf1, (FontBoxFont)null, false);
        } else {
            TrueTypeFont ttf = (TrueTypeFont)(, baseFont);
            if (ttf != null) {
                return new CIDFontMapping((OpenTypeFont)null, ttf, false);
            } else {
                if (cidSystemInfo != null) {
                    String collection = () + "-" + ();
                    if (("Adobe-GB1") || ("Adobe-CNS1") || ("Adobe-Japan1") || ("Adobe-Korea1")) {
                        PriorityQueue<> queue = (fontDescriptor, cidSystemInfo);
                         bestMatch = ()();
                        if (bestMatch != null) {
                            FontBoxFont font = ();
                            if (font instanceof OpenTypeFont) {
                                return new CIDFontMapping((OpenTypeFont)font, (FontBoxFont)null, true);
                            }

                            if (font != null) {
                                return new CIDFontMapping((OpenTypeFont)null, font, true);
                            }
                        }
                    }
                }

                return new CIDFontMapping((OpenTypeFont)null, , true);
            }
        }
    }

    private PriorityQueue<> getFontMatches(PDFontDescriptor fontDescriptor, PDCIDSystemInfo cidSystemInfo) {
        PriorityQueue<> queue = new PriorityQueue(20);
        Iterator var4 = ().iterator();

        while(true) {
             match;
            while(true) {
                FontInfo info;
                do {
                    if (!()) {
                        return queue;
                    }

                    info = (FontInfo)();
                } while(cidSystemInfo != null && !(cidSystemInfo, info));

                match = new (info);
                if (() != null && () != null) {
                    PDPanoseClassification panose = ().getPanose();
                    if (() != ().getFamilyKind()) {
                        break;
                    }

                    if (() == 0 && (().toLowerCase().contains("barcode") || ().startsWith("Code")) && !(fontDescriptor)) {
                        continue;
                    }

                    if (() == ().getSerifStyle()) {
                         += 2.0D;
                    } else if (() >= 2 && () <= 5 && ().getSerifStyle() >= 2 && ().getSerifStyle() <= 5) {
                        ++;
                    } else if (() >= 11 && () <= 13 && ().getSerifStyle() >= 11 && ().getSerifStyle() <= 13) {
                        ++;
                    } else if (() != 0 && ().getSerifStyle() != 0) {
                        --;
                    }

                    int weight = ().getWeight();
                    int weightClass = ();
                    if ((weight - weightClass) > 2) {
                        weight = weightClass;
                    }

                    if (() == weight) {
                         += 2.0D;
                    } else if (() > 1 && weight > 1) {
                        float dist = (float)(() - weight);
                         += 1.0D - (double)dist * 0.5D;
                    }
                    break;
                }

                if (() > 0.0F && () > 0) {
                    float dist = (() - (float)());
                     += 1.0D - (double)(dist / 100.0F) * 0.5D;
                }
                break;
            }

            (match);
        }
    }

    private boolean probablyBarcodeFont(PDFontDescriptor fontDescriptor) {
        String ff = ();
        if (ff == null) {
            ff = "";
        }

        String fn = ();
        if (fn == null) {
            fn = "";
        }

        return ("Code") || ().contains("barcode") || ("Code") || ().contains("barcode");
    }

    private boolean isCharSetMatch(PDCIDSystemInfo cidSystemInfo, FontInfo info) {
        if (() != null) {
            return ().getRegistry().equals(()) && ().getOrdering().equals(());
        } else {
            long codePageRange = ();
            long JIS_JAPAN = 131072L;
            long CHINESE_SIMPLIFIED = 262144L;
            long KOREAN_WANSUNG = 524288L;
            long CHINESE_TRADITIONAL = 1048576L;
            long KOREAN_JOHAB = 2097152L;
            if (().equals("GB1") && (codePageRange & CHINESE_SIMPLIFIED) == CHINESE_SIMPLIFIED) {
                return true;
            } else if (().equals("CNS1") && (codePageRange & CHINESE_TRADITIONAL) == CHINESE_TRADITIONAL) {
                return true;
            } else if (().equals("Japan1") && (codePageRange & JIS_JAPAN) == JIS_JAPAN) {
                return true;
            } else {
                return ().equals("Korea1") && (codePageRange & KOREAN_WANSUNG) == KOREAN_WANSUNG || (codePageRange & KOREAN_JOHAB) == KOREAN_JOHAB;
            }
        }
    }

    private  printMatches(PriorityQueue<> queue) {
         bestMatch = ()();
        ("-------");

        while(!()) {
             match = ()();
            FontInfo info = ;
            ( + " | " + () + " " + () + " " + () + " " + () + " " + () + " " + ());
        }

        ("-------");
        return bestMatch;
    }

    private static class FontMatch implements Comparable<> {
        double score;
        final FontInfo info;

        FontMatch(FontInfo info) {
             = info;
        }

        public int compareTo( match) {
            return (, );
        }
    }

    private static class DefaultFontProvider {
        private static final FontProvider INSTANCE;

        private DefaultFontProvider() {
        }

        static {
            INSTANCE = new FileSystemFontProvider();
        }
    }
}