本文整理汇总了Java中org.apache.pdfbox.pdmodel.font.PDType3Font类的典型用法代码示例。如果您正苦于以下问题:Java PDType3Font类的具体用法?Java PDType3Font怎么用?Java PDType3Font使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PDType3Font类属于org.apache.pdfbox.pdmodel.font包,在下文中一共展示了PDType3Font类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRenderSdnList
import org.apache.pdfbox.pdmodel.font.PDType3Font; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/42032729/render-type3-font-character-as-image-using-pdfbox">
* Render Type3 font character as image using PDFBox
* </a>
* <br/>
* <a href="https://drive.google.com/file/d/0B0f6X4SAMh2KRDJTbm4tb3E1a1U/view">
* 4700198773.pdf
* </a>
* from
* <a href="http://stackoverflow.com/questions/37754112/extract-text-with-custom-font-result-non-readble">
* extract text with custom font result non readble
* </a>
* <p>
* This test shows how one can render individual Type 3 font glyphs as bitmaps.
* Unfortunately PDFBox out-of-the-box does not provide a class to render contents
* of arbitrary XObjects, merely for rendering pages; thus, we simply create a page
* with the glyph in question and render that page.
* </p>
* <p>
* As the OP did not provide a sample PDF, we simply use one from another
* stackoverflow question. There obviously might remain issues with the
* OP's files.
* </p>
*/
@Test
public void testRenderSdnList() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("sdnlist.pdf"))
{
PDDocument document = PDDocument.load(resource);
PDPage page = document.getPage(1);
PDResources pageResources = page.getResources();
COSName f1Name = COSName.getPDFName("R144");
PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name);
Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap();
COSDictionary charProcsDictionary = fontF1.getCharProcs();
for (COSName key : charProcsDictionary.keySet())
{
COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key);
PDType3CharProc charProc = new PDType3CharProc(fontF1, stream);
PDRectangle bbox = charProc.getGlyphBBox();
if (bbox == null)
bbox = charProc.getBBox();
Integer code = f1NameToCode.get(key.getName());
if (code != null)
{
PDDocument charDocument = new PDDocument();
PDPage charPage = new PDPage(bbox);
charDocument.addPage(charPage);
charPage.setResources(pageResources);
PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage);
charContentStream.beginText();
charContentStream.setFont(fontF1, bbox.getHeight());
charContentStream.getOutput().write(String.format("<%2X> Tj\n", code).getBytes());
charContentStream.endText();
charContentStream.close();
File result = new File(RESULT_FOLDER, String.format("sdnlist-%s-%s.png", key.getName(), code));
PDFRenderer renderer = new PDFRenderer(charDocument);
BufferedImage image = renderer.renderImageWithDPI(0, 96);
ImageIO.write(image, "PNG", result);
charDocument.save(new File(RESULT_FOLDER, String.format("sdnlist-%s-%s.pdf", key.getName(), code)));
charDocument.close();
}
}
}
}
示例2: testRender4700198773
import org.apache.pdfbox.pdmodel.font.PDType3Font; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/42032729/render-type3-font-character-as-image-using-pdfbox">
* Render Type3 font character as image using PDFBox
* </a>
* <br/>
* <a href="https://drive.google.com/file/d/0B0f6X4SAMh2KRDJTbm4tb3E1a1U/view">
* 4700198773.pdf
* </a>
* from
* <a href="http://stackoverflow.com/questions/37754112/extract-text-with-custom-font-result-non-readble">
* extract text with custom font result non readble
* </a>
* <p>
* This test shows how one can render individual Type 3 font glyphs as bitmaps.
* Unfortunately PDFBox out-of-the-box does not provide a class to render contents
* of arbitrary XObjects, merely for rendering pages; thus, we simply create a page
* with the glyph in question and render that page.
* </p>
* <p>
* As the OP did not provide a sample PDF, we simply use one from another
* stackoverflow question. There obviously might remain issues with the
* OP's files.
* </p>
*/
@Test
public void testRender4700198773() throws IOException
{
try ( InputStream resource = getClass().getResourceAsStream("4700198773.pdf"))
{
PDDocument document = PDDocument.load(resource);
PDPage page = document.getPage(0);
PDResources pageResources = page.getResources();
COSName f1Name = COSName.getPDFName("F1");
PDType3Font fontF1 = (PDType3Font) pageResources.getFont(f1Name);
Map<String, Integer> f1NameToCode = fontF1.getEncoding().getNameToCodeMap();
COSDictionary charProcsDictionary = fontF1.getCharProcs();
for (COSName key : charProcsDictionary.keySet())
{
COSStream stream = (COSStream) charProcsDictionary.getDictionaryObject(key);
PDType3CharProc charProc = new PDType3CharProc(fontF1, stream);
PDRectangle bbox = charProc.getGlyphBBox();
if (bbox == null)
bbox = charProc.getBBox();
Integer code = f1NameToCode.get(key.getName());
if (code != null)
{
PDDocument charDocument = new PDDocument();
PDPage charPage = new PDPage(bbox);
charDocument.addPage(charPage);
charPage.setResources(pageResources);
PDPageContentStream charContentStream = new PDPageContentStream(charDocument, charPage);
charContentStream.beginText();
charContentStream.setFont(fontF1, bbox.getHeight());
charContentStream.getOutput().write(String.format("<%2X> Tj\n", code).getBytes());
charContentStream.endText();
charContentStream.close();
File result = new File(RESULT_FOLDER, String.format("4700198773-%s-%s.png", key.getName(), code));
PDFRenderer renderer = new PDFRenderer(charDocument);
BufferedImage image = renderer.renderImageWithDPI(0, 96);
ImageIO.write(image, "PNG", result);
charDocument.save(new File(RESULT_FOLDER, String.format("4700198773-%s-%s.pdf", key.getName(), code)));
charDocument.close();
}
}
}
}
示例3: computeBasename
import org.apache.pdfbox.pdmodel.font.PDType3Font; //导入依赖的package包/类
/**
* Computes the normalized fontname of the font. This is a string with only
* the fontname and no additional style informations (like "bold" or
* "italic").
*
* @param font
* the font to analyze.
* @return the derived font name.
*/
public static String computeBasename(PDFont font) {
// Terminate all whitespaces, commas and hyphens
String fontname = null;
if (font != null) {
if (font instanceof PDType3Font) {
fontname = "type3";
} else {
fontname = font.getName().toLowerCase();
// Terminate trailing characters up to the "+".
// As far as I know, these characters are used in names of embedded
// fonts. If the embedded font can't be read, we'll try to find it here.
if (fontname.indexOf("+") > -1) {
fontname = fontname.substring(fontname.indexOf("+") + 1);
}
}
}
return fontname;
}
示例4: computeBasename
import org.apache.pdfbox.pdmodel.font.PDType3Font; //导入依赖的package包/类
/**
* Computes the normalized fontname of the font. This is a string with only
* the fontname and no additional style informations (like "bold" or
* "italic").
*
* @param font
* the font to analyze.
* @return the derived font name.
*/
public static String computeBasename(PDFont font) {
// Terminate all whitespaces, commas and hyphens
String fontname;
if (font instanceof PDType3Font) {
fontname = "type3";
} else {
fontname = font.getName().toLowerCase();
// Terminate trailing characters up to the "+".
// As far as I know, these characters are used in names of embedded fonts
// If the embedded font can't be read, we'll try to find it here
if (fontname.indexOf("+") > -1) {
fontname = fontname.substring(fontname.indexOf("+") + 1);
}
}
return fontname;
}
示例5: writeString
import org.apache.pdfbox.pdmodel.font.PDType3Font; //导入依赖的package包/类
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
text = text.toLowerCase();
int index = text.indexOf(mTextToHighlight);
if (index != -1) {
PDPage currentPage = getCurrentPage();
PDRectangle pageBoundingBox = currentPage.getBBox();
AffineTransform flip = new AffineTransform();
flip.translate(0, pageBoundingBox.getHeight());
flip.scale(1, -1);
PDRectangle mediaBox = currentPage.getMediaBox();
float mediaHeight = mediaBox.getHeight();
float mediaWidth = mediaBox.getWidth();
int size = textPositions.size();
while (index != -1) {
int last = index + mTextToHighlight.length() - 1;
for (int i = index; i <= last; i++) {
TextPosition pos = textPositions.get(i);
PDFont font = pos.getFont();
BoundingBox bbox = font.getBoundingBox();
Rectangle2D.Float rect = new Rectangle2D.Float(0, bbox.getLowerLeftY(), font.getWidth(pos.getCharacterCodes()[0]), bbox.getHeight());
AffineTransform at = pos.getTextMatrix().createAffineTransform();
if (font instanceof PDType3Font) {
at.concatenate(font.getFontMatrix().createAffineTransform());
} else {
at.scale(1 / 1000f, 1 / 1000f);
}
Shape shape = flip.createTransformedShape(at.createTransformedShape(rect));
AffineTransform transform = mGC.getTransform();
int rotation = currentPage.getRotation();
if (rotation != 0) {
switch (rotation) {
case 90:
mGC.translate(mediaHeight, 0);
break;
case 270:
mGC.translate(0, mediaWidth);
break;
case 180:
mGC.translate(mediaWidth, mediaHeight);
break;
default:
break;
}
mGC.rotate(Math.toRadians(rotation));
}
mGC.fill(shape);
if (rotation != 0) {
mGC.setTransform(transform);
}
}
index = last < size - 1 ? text.indexOf(mTextToHighlight, last + 1) : -1;
}
}
}
示例6: PdfBoxFont
import org.apache.pdfbox.pdmodel.font.PDType3Font; //导入依赖的package包/类
/**
* The default constructor.
*/
protected PdfBoxFont(PDFont font) {
this();
this.font = font;
this.name = font.getName();
this.basename = computeBasename(font);
this.isType3Font = font instanceof PDType3Font;
// pdfFont.isSymbolicFont = font.isSymbolicFont(); TODO
// Normalize all kinds of fonttypes. There are several possible version
// which have to be normalized e.g. Arial,Bold Arial-BoldMT
// Helvetica-oblique ...
this.basename = this.basename.replaceAll(" ", "")
.replaceAll(",", "")
.replaceAll("-", "");
int length1 = this.basename.length();
this.basename = this.basename.replaceAll("extrabold", "")
.replaceAll("bold", "")
.replaceAll("black", "");
int length2 = this.basename.length();
this.isBold = length2 < length1;
this.basename = this.basename.replaceAll("italic", "")
.replaceAll("oblique", "");
length1 = this.basename.length();
this.isItalic = length1 < length2;
this.basename = this.basename
.replaceAll("condensed", "")
.replaceAll("medium", "")
.replaceAll("regular", "")
.replaceAll("light", "");
}
示例7: isType3Font
import org.apache.pdfbox.pdmodel.font.PDType3Font; //导入依赖的package包/类
@Override
public boolean isType3Font() {
return font instanceof PDType3Font;
}
示例8: showLigature
import org.apache.pdfbox.pdmodel.font.PDType3Font; //导入依赖的package包/类
@Override
public void showLigature(String[] unicodes, int code, PDFont font,
Matrix trm) throws IOException {
super.showLigature(unicodes, code, font, trm);
float size = getGraphicsState().getTextState().getFontSize();
PDColor nonStrokingColor = getGraphicsState().getNonStrokingColor();
PdfBoxColor color = PdfBoxColor.create(nonStrokingColor);
PdfBoxFont pdfFont = PdfBoxFont.create(font);
Rectangle boundingBox = null;
if (font instanceof PDType3Font) {
processType3Stream(((PDType3Font) font).getCharProc(code), trm);
boundingBox = getCurrentType3GlyphBoundingBox();
} else {
boundingBox = getFontGlyphBoundingBox(code, font, trm);
}
if (boundingBox == null) {
boundingBox = getDefaultBoundingBox(code, font, trm);
} else {
Rectangle defaultBoundingBox = getDefaultBoundingBox(code, font, trm);
boundingBox.setMinX(defaultBoundingBox.getMinX());
boundingBox.setMaxX(defaultBoundingBox.getMaxX());
}
float widthPerUnicode = boundingBox.getWidth() / (float) unicodes.length;
for (int i = 0; i < unicodes.length; i++) {
float minX = boundingBox.getMinX() + i * widthPerUnicode;
float maxX = minX + widthPerUnicode;
float minY = boundingBox.getMinY();
float maxY = boundingBox.getMaxY();
Rectangle newBoundBox = new SimpleRectangle(minX, minY, maxX, maxY);
PdfBoxCharacter character = new PdfBoxCharacter(currentPage, unicodes[i]);
character.setCharCode(code);
character.setRectangle(newBoundBox);
character.setTextRenderingMatrix(trm);
character.setFont(pdfFont);
character.setFontsize(size);
character.setColor(color);
showPdfTextCharacter(character);
}
}