I’m trying to load a font by its bytes with method Font.loadFont(InputStream var0, double var1), but it doesn’t load the font
The font bytes I get from PDFBox library and I already write the bytes into a ttf file and opened the file sucessfully
It’s a JavaFx application
This is the first method called
public void adicionarNovaTab(Ebook ebookSelecionado) {
try (PDDocument document = Loader.loadPDF(ebookSelecionado.getConteudo())) {
PDPageTree pages = document.getPages();
CustomTab customTab = new CustomTab(ebookSelecionado.getNome());
int pageNumber = 1;
ScrollPane scrollPane = new ScrollPane();
VBox vBox = new VBox();
vBox.setSpacing(20);
vBox.setMinWidth(Region.USE_COMPUTED_SIZE);
vBox.setMinHeight(Region.USE_COMPUTED_SIZE);
vBox.setMaxWidth(Region.USE_COMPUTED_SIZE);
vBox.setMaxHeight(Region.USE_COMPUTED_SIZE);
for (PDPage page : pages) {
Pane pane = new Pane();
Background background = new Background(new BackgroundFill(Paint.valueOf("WHITE"), null, null));
pane.setBackground(background);
double pageWidth = page.getMediaBox().getWidth();
double pageHeight = page.getMediaBox().getHeight();
pane.setPrefSize(pageWidth, pageHeight);
pane.setMinSize(pageWidth, pageHeight);
pane.setMaxSize(pageWidth, pageHeight);
List<TextoComCoordenadas> textoComCoordenadas = extrairTextoDaPagina(pageNumber, page, document);
textoComCoordenadas.forEach(texto -> {
Text text = new Text(String.valueOf(texto.getCaractere()));
text.setX(texto.getX());
text.setY(texto.getY());
if (texto.getFonte() != null && texto.getFonte().getInputStream() != null) {
Font font = Font.loadFont(texto.getFonte().getInputStream(), texto.getFontSize());
text.setFont(font);
} else {
Font font = Font.font(texto.getFonte() != null && texto.getFonte().getNome() != null ? texto.getFonte().getNome() : "Arial",
texto.isBold() ? FontWeight.BOLD : FontWeight.NORMAL,
texto.isItalic() ? FontPosture.ITALIC : FontPosture.REGULAR,
texto.getFontSize());
text.setFont(font);
}
pane.getChildren().add(text);
});
vBox.getChildren().add(pane);
pageNumber++;
scrollPane.setContent(vBox);
scrollPane.setFitToWidth(true); // Ajusta a largura do conteúdo ao ScrollPane
scrollPane.setFitToHeight(true); // Ajusta a altura do conteúdo ao ScrollPane
customTab.setContent(scrollPane);
if (!tabPaneVisualizador.getTabs().contains(customTab)) {
tabPaneVisualizador.getTabs().add(customTab);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Then the adicionarNovaTab
method calls extrairTextoDaPagina
in which try to set the loadFont into a Text object:
private List<TextoComCoordenadas> extrairTextoDaPagina(int numeroDaPagina, PDPage page, PDDocument document) throws IOException {
List<TextoComCoordenadas> textosComCoordenadas = new ArrayList<>();
PDResources resources = page.getResources();
Iterable<COSName> fontNames = resources.getFontNames();
fontNames.forEach(font -> {
try {
PDFont pdFont = resources.getFont(font);
String nomeFonte = pdFont.getName();
boolean temMesmoNome = fontesDosEbooks.stream().anyMatch(fonte -> fonte.getNome().equals(nomeFonte));
if (!temMesmoNome && pdFont.getFontDescriptor() != null && pdFont.getFontDescriptor().getFontFile() != null) {
COSInputStream cosInputStream = pdFont.getFontDescriptor().getFontFile().createInputStream();
FonteDoEbook fonte = FonteDoEbook.builder().nome(nomeFonte).inputStream(cosInputStream).build();
fontesDosEbooks.add(fonte);
System.out.println("Adicionado uma fonte: " + nomeFonte);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
PDFTextStripper textStripper = new PDFTextStripper() {
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
for (int i = 0; i < textPositions.size(); i++) {
TextPosition textPosition = textPositions.get(i);
float x = textPosition.getXDirAdj();
float y = textPosition.getYDirAdj();
boolean italic = false;
boolean forceBold = false;
float fontSize = textPosition.getFontSize();
final String nomeDaFonte = textPositions.get(i).getFont().getName();
if (textPosition.getFont().getFontDescriptor() != null) {
italic = textPosition.getFont().getFontDescriptor().isItalic();
forceBold = textPosition.getFont().getFontDescriptor().isForceBold();
if (!italic && nomeDaFonte.toLowerCase().contains("-it")) {
italic = true;
}
if (!forceBold && nomeDaFonte.toLowerCase().contains("bold")) {
forceBold = true;
}
}
TextoComCoordenadas.TextoComCoordenadasBuilder textoComCoordenadasBuilder =
TextoComCoordenadas.builder()
.caractere(text.charAt(i))
.x(x)
.y(y)
.fontSize(fontSize)
.bold(forceBold)
.italic(italic);
Optional<FonteDoEbook> first = fontesDosEbooks.stream()
.filter(fonte -> fonte.getNome().equals(nomeDaFonte)).findFirst();
if (first.isPresent())
textoComCoordenadasBuilder.fonte(first.get());
else {
FonteDoEbook fonte = FonteDoEbook.builder().nome(nomeDaFonte).build();
fontesDosEbooks.add(fonte);
}
textosComCoordenadas.add(textoComCoordenadasBuilder.build());
}
}
};
textStripper.setStartPage(numeroDaPagina);
textStripper.setEndPage(numeroDaPagina);
textStripper.getText(document);
return textosComCoordenadas;
}
I already tried, instead of using the COS InputStream object, using new ByteArrayInputStream(bytes Here) but it didn’t work either