I have problems with pdfbox 2.0.2 when writing a pdf document from elements of a previously read document ( https://www.dropbox.com/s/ttxiv0dq3abh5kj/Test.pdf?dl=0 ). Everything works fine, except when I call showTextfor PDPageContentStream, where I pre-installed the font with out.setFont(textState.getFont(), textState.getFontSize())(see out.setFont(textState.getFont(), textState.getFontSize())INFORMATION), and the font is ComicSansMS or ArialBlack. textStateis a (clone from) state from a previously read document. Writing text using Helvetica or Times-Roman works great.
INFORMATION: set font PDTrueTypeFont RXNQOL+ComicSansMS,Bold/18.0 embedded
SEVERE: error writing <w>U+0077 is not available in this font encoding: built-in (TTF)
I believe that the problem may be caused by a missing hyphen or a space in the font name, but I don’t know how to fix it.
Here is the complete code
import java.awt.Point;
import java.awt.geom.Point2D;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.contentstream.PDFGraphicsStreamEngine;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.graphics.image.PDImage;
import org.apache.pdfbox.pdmodel.graphics.state.PDTextState;
import org.apache.pdfbox.util.Matrix;
import org.apache.pdfbox.util.Vector;
public class Test extends PDFGraphicsStreamEngine {
public static void main(String[] args) throws IOException {
test();
}
public static void test() throws IOException {
PDDocument document = PDDocument.load(new File("Test.pdf"));
PDPage pageIn = document.getPage(0);
PDDocument saveDoc = new PDDocument();
PDPage savePage = new PDPage(pageIn.getMediaBox());
saveDoc.addPage(savePage);
try (PDPageContentStream out = new PDPageContentStream(saveDoc, savePage)) {
Test test = new Test(pageIn, out);
test.processPage(pageIn);
}
}
private final PDPageContentStream out;
public Test(PDPage pageIn, PDPageContentStream out) {
super(pageIn);
this.out = out;
}
@Override
public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException {
}
@Override
public void clip(int windingRule) throws IOException {
}
@Override
public void closePath() throws IOException {
}
@Override
public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException {
}
@Override
public void drawImage(PDImage pdImage) throws IOException {
}
@Override
public void endPath() throws IOException {
}
@Override
public void fillAndStrokePath(int windingRule) throws IOException {
}
@Override
public void fillPath(int windingRule) throws IOException {
}
@Override
public Point2D getCurrentPoint() {
return new Point(0, 0);
}
@Override
public void lineTo(float x, float y) throws IOException {
}
@Override
public void moveTo(float x, float y) throws IOException {
}
@Override
public void shadingFill(COSName shadingName) throws IOException {
}
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement) throws IOException {
super.showGlyph(textRenderingMatrix, font, code, unicode, displacement);
PDTextState textState = getGraphicsState().getTextState();
out.beginText();
out.setTextMatrix(getTextMatrix());
out.setFont(textState.getFont(), textState.getFontSize());
out.showText(unicode);
out.endText();
}
@Override
public void strokePath() throws IOException {
}
}
Any suggestions?
Thanks Jürgen