0
Follow
0
View

itextpdf Chinese font problem

crazyuv000 注册会员
2023-02-27 08:11

< br / >

< div class =" aw - list - img > img

< br / > the first question:
There is an error when I write my path directly. I don't know why.
I registered the right way, why simsun error when using directly

img

img

img

happyjian88 注册会员
2023-02-27 08:11

img


So pdf Chinese is not displayed again

clownzhl 注册会员
2023-02-27 08:11


You can use the getResourstream method to retrieve the input stream of the font file. This is then converted to a byte array, and the BaseFont.createFont method is used to create a custom font. Here is a sample code:

String fontPath = "/static/font/simsun.ttc";
InputStream fontStream = this.getClass().getResourceAsStream(fontPath);
if (fontStream == null) {
    throw new FileNotFoundException("Font file not found: " + fontPath);
}
byte[] fontData = IOUtils.toByteArray(fontStream);
BaseFont customFont = BaseFont.createFont("simsun", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, false, fontData, null);
Font font = new Font(customFont, 12, Font.NORMAL);

This example assumes that the font files are in the /static/font/simsun.ttc path of the classpath. If your font files are on a different path, you can change the value of the fontPath variable accordingly.

djpraul 注册会员
2023-02-27 08:11

In iText PDF, you can use the FontFactory class to load custom fonts. To avoid absolute paths, you can use relative paths to specify the location of the font file.

Assuming the font library file name is simsun.ttc in the resources/static/font/ directory of your project, you can load it as follows:

  1. introduces the FontFactory class into the code:
import com.itextpdf.text.FontFactory;
  1. Sets the path to the font library file:
String fontPath = "resources/static/font/simsun.ttc";
  1. Loading font:
FontFactory.register(fontPath, "simsun");

This allows you to register the font library file with iText and give it a name of "simsun". You can then refer to the font by that name in your code.

xuhui820407 注册会员
2023-02-27 08:11

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > when using itextpdf library, you can use the relative path to read from the definition of font library. Assume that the path of the font library's resources/static/font/simsun TTC, can be read through the following ways to font library:

String fontPath = "resources/static/font/simsun.ttc";
FontFactory.register(fontPath);

when using relative paths, itextpdf will default from the root directory under the classpath begins searching for the resources, so the font library can be a directory in the classpath, for example in the SRC/main/resources/static/font, then you can use the following ways to read font library:

String fontPath = "static/font/simsun.ttc";
FontFactory.register(fontPath);

This makes it possible to use relative paths to read from defined font libraries on both Windows and Linux platforms.

dd330825197610124545 注册会员
2023-02-27 08:11

< br / >

< div class =" aw - list - img > img

< br / > the first question:
There is an error when I write my path directly. I don't know why.
I registered the right way, why simsun error when using directly

img

img

img

About the Author

Question Info

Publish Time
2023-02-27 08:11
Update Time
2023-02-27 08:11