从PDFBox中的字体获取颜色 [英] Get colours from fonts in PDFBox
问题描述
我试图从PDFBox中获取字体颜色,我似乎一直在抛出一个异常。有人可以帮忙吗?我尝试获取颜色的方式是(页面是我得到的PDPage):
pre preResources = page.getResources();
可重用< COSName> fontNames = resources.getFontNames(); (COSName fontName:fontNames)
System.out.println(name:+ resources.getFont(fontName).getName()+
color:+ resources.getColorSpace(的fontName).getName());
这会打印出异常:
org.apache.pdfbox.pdmodel.MissingResourceException:缺少色彩空间:F1
有人可以告诉我如何正确地获得以这种方式获得的字体的颜色吗?
试试PrintTextColors源代码下载:
/ **
*这是一个如何获取文本颜色的例子。请注意,这将不会告诉背景
*,并且只有当文本稍后不被覆盖,并且仅当文本渲染
*模式为0,1或2时,才会正常工作。在PDF 32000规范,请阅读9.3.6文本渲染模式至
*了解更多。模式0(FILL)是默认值。模式1(STROKE)将使字形看起来空洞。模式2
*(FILL_STROKE)会使字形显得肥胖。
*
* @author Ben Litchfield
* @author Tilman Hausherr
* /
public class PrintTextColors extends PDFTextStripper
{
/ **
*实例化一个新的PDFTextStripper对象。
*
* @throws IOException如果加载属性时出错。
* /
public PrintTextColors()throws IOException
{
addOperator(new SetStrokingColorSpace());
addOperator(新的SetNonStrokingColorSpace());
addOperator(new SetStrokingDeviceCMYKColor());
addOperator(new SetNonStrokingDeviceCMYKColor());
addOperator(new SetNonStrokingDeviceRGBColor());
addOperator(new SetStrokingDeviceRGBColor());
addOperator(new SetNonStrokingDeviceGrayColor());
addOperator(new SetStrokingDeviceGrayColor());
addOperator(new SetStrokingColor());
addOperator(new SetStrokingColorN());
addOperator(new SetNonStrokingColor());
addOperator(new SetNonStrokingColorN());
}
/ **
*这将打印文档数据。
*
* @param args命令行参数。
*
* @throws IOException如果解析文档时出错。
* /
public static void main(String [] args)throws IOException
{
if(args.length!= 1)
{
usage ();
}
else
{
PDDocument document = null;
尝试
{
document = PDDocument.load(new File(args [0]));
PDFTextStripper stripper = new PrintTextColors();
stripper.setSortByPosition(true);
stripper.setStartPage(0);
stripper.setEndPage(document.getNumberOfPages());
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(document,dummy);
finally
if(document!= null)
{
document.close();
$ @覆盖
保护无效processTextPosition(TextPosition文本)
{
super.processTextPosition(文本);
PDColor strokingColor = getGraphicsState()。getStrokingColor();
PDColor nonStrokingColor = getGraphicsState()。getNonStrokingColor();
String unicode = text.getUnicode();
RenderingMode renderingMode = getGraphicsState()。getTextState()。getRenderingMode();
System.out.println(Unicode:+ unicode);
System.out.println(Rendering mode:+ renderingMode);
System.out.println(抚摸颜色:+ strokingColor);
System.out.println(Non-Stroking color:+ nonStrokingColor);
System.out.println(Non-Stroking color:+ nonStrokingColor);
System.out.println();
//查看PrintTextLocations获取更多属性
$ b $ **
*这将打印这个文档的用法。
private static void use()
{
System.err.println(Usage:java+ PrintTextColors.class.getName()+< input- PDF>中);
}
}
I am trying to get the font colour from PDFBox and I seem to keep throwing an exception. Can someone help? The way I tried to obtain the colour was (page is the PDPage I obtained):
PDResources = page.getResources();
Iterable<COSName> fontNames = resources.getFontNames();
for (COSName fontName:fontNames)
System.out.println("name: " + resources.getFont(fontName).getName() +
"colour: " + resources.getColorSpace(fontName).getName());
This prints out the exception:
org.apache.pdfbox.pdmodel.MissingResourceException: Missing color space: F1
Could someone tell me how to properly get the colour of a font obtained in this manner?
Try PrintTextColors from the source code download:
/**
* This is an example on how to get the colors of text. Note that this will not tell the background,
* and will only work properly if the text is not overwritten later, and only if the text rendering
* modes are 0, 1 or 2. In the PDF 32000 specification, please read 9.3.6 "Text Rendering Mode" to
* know more. Mode 0 (FILL) is the default. Mode 1 (STROKE) will make glyphs look "hollow". Mode 2
* (FILL_STROKE) will make glyphs look "fat".
*
* @author Ben Litchfield
* @author Tilman Hausherr
*/
public class PrintTextColors extends PDFTextStripper
{
/**
* Instantiate a new PDFTextStripper object.
*
* @throws IOException If there is an error loading the properties.
*/
public PrintTextColors() throws IOException
{
addOperator(new SetStrokingColorSpace());
addOperator(new SetNonStrokingColorSpace());
addOperator(new SetStrokingDeviceCMYKColor());
addOperator(new SetNonStrokingDeviceCMYKColor());
addOperator(new SetNonStrokingDeviceRGBColor());
addOperator(new SetStrokingDeviceRGBColor());
addOperator(new SetNonStrokingDeviceGrayColor());
addOperator(new SetStrokingDeviceGrayColor());
addOperator(new SetStrokingColor());
addOperator(new SetStrokingColorN());
addOperator(new SetNonStrokingColor());
addOperator(new SetNonStrokingColorN());
}
/**
* This will print the documents data.
*
* @param args The command line arguments.
*
* @throws IOException If there is an error parsing the document.
*/
public static void main(String[] args) throws IOException
{
if (args.length != 1)
{
usage();
}
else
{
PDDocument document = null;
try
{
document = PDDocument.load(new File(args[0]));
PDFTextStripper stripper = new PrintTextColors();
stripper.setSortByPosition(true);
stripper.setStartPage(0);
stripper.setEndPage(document.getNumberOfPages());
Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
stripper.writeText(document, dummy);
}
finally
{
if (document != null)
{
document.close();
}
}
}
}
@Override
protected void processTextPosition(TextPosition text)
{
super.processTextPosition(text);
PDColor strokingColor = getGraphicsState().getStrokingColor();
PDColor nonStrokingColor = getGraphicsState().getNonStrokingColor();
String unicode = text.getUnicode();
RenderingMode renderingMode = getGraphicsState().getTextState().getRenderingMode();
System.out.println("Unicode: " + unicode);
System.out.println("Rendering mode: " + renderingMode);
System.out.println("Stroking color: " + strokingColor);
System.out.println("Non-Stroking color: " + nonStrokingColor);
System.out.println("Non-Stroking color: " + nonStrokingColor);
System.out.println();
// See the PrintTextLocations for more attributes
}
/**
* This will print the usage for this document.
*/
private static void usage()
{
System.err.println("Usage: java " + PrintTextColors.class.getName() + " <input-pdf>");
}
}
这篇关于从PDFBox中的字体获取颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!