use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class PDFreeTextAppearanceHandler method extractFontDetails.
// TODO extractNonStrokingColor and extractFontDetails
// might somehow be replaced with PDDefaultAppearanceString, which is quite similar.
private void extractFontDetails(PDAnnotationMarkup annotation) {
String defaultAppearance = annotation.getDefaultAppearance();
if (defaultAppearance == null && document != null && document.getDocumentCatalog().getAcroForm() != null) {
defaultAppearance = document.getDocumentCatalog().getAcroForm().getDefaultAppearance();
}
if (defaultAppearance == null) {
return;
}
try {
// not sure if charset is correct, but we only need numbers and simple characters
PDFStreamParser parser = new PDFStreamParser(defaultAppearance.getBytes(Charsets.US_ASCII));
COSArray arguments = new COSArray();
COSArray fontArguments = new COSArray();
for (Object token = parser.parseNextToken(); token != null; token = parser.parseNextToken()) {
if (token instanceof COSObject) {
arguments.add(((COSObject) token).getObject());
} else if (token instanceof Operator) {
Operator op = (Operator) token;
String name = op.getName();
if (OperatorName.SET_FONT_AND_SIZE.equals(name)) {
fontArguments = arguments;
}
arguments = new COSArray();
} else {
arguments.add((COSBase) token);
}
}
if (fontArguments.size() >= 2) {
COSBase base = fontArguments.get(0);
if (base instanceof COSName) {
fontName = (COSName) base;
}
base = fontArguments.get(1);
if (base instanceof COSNumber) {
fontSize = ((COSNumber) base).floatValue();
}
}
} catch (IOException ex) {
Log.w("PdfBox-Android", "Problem parsing /DA, will use default 'Helv 10'", ex);
}
}
use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class PDAnnotation method getNormalAppearanceStream.
/**
* Returns the appearance stream for this annotation, if any. The annotation state is taken into account, if
* present.
*/
public PDAppearanceStream getNormalAppearanceStream() {
PDAppearanceDictionary appearanceDict = getAppearance();
if (appearanceDict == null) {
return null;
}
PDAppearanceEntry normalAppearance = appearanceDict.getNormalAppearance();
if (normalAppearance == null) {
return null;
}
if (normalAppearance.isSubDictionary()) {
COSName state = getAppearanceState();
return normalAppearance.getSubDictionary().get(state);
} else {
return normalAppearance.getAppearanceStream();
}
}
use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class PDAppearanceEntry method getSubDictionary.
/**
* Returns the entry as an appearance subdictionary.
*
* @throws IllegalStateException if this entry is not an appearance subdictionary
*/
public Map<COSName, PDAppearanceStream> getSubDictionary() {
if (!isSubDictionary()) {
throw new IllegalStateException("This entry is not an appearance subdictionary");
}
COSDictionary dict = (COSDictionary) entry;
Map<COSName, PDAppearanceStream> map = new HashMap<COSName, PDAppearanceStream>();
for (COSName name : dict.keySet()) {
COSBase value = dict.getDictionaryObject(name);
// the file from PDFBOX-1599 contains /null as its entry, so we skip non-stream entries
if (value instanceof COSStream) {
map.put(name, new PDAppearanceStream((COSStream) value));
}
}
return new COSDictionaryMap<COSName, PDAppearanceStream>(map, dict);
}
use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class DrawObject method process.
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
if (arguments.isEmpty()) {
throw new MissingOperandException(operator, arguments);
}
COSBase base0 = arguments.get(0);
if (!(base0 instanceof COSName)) {
return;
}
COSName name = (COSName) base0;
if (context.getResources().isImageXObject(name)) {
// we're done here, don't decode images when doing text extraction
return;
}
PDXObject xobject = context.getResources().getXObject(name);
if (xobject instanceof PDFormXObject) {
try {
context.increaseLevel();
if (context.getLevel() > 25) {
Log.e("PdfBox-Android", "recursion is too deep, skipping form XObject");
return;
}
PDFormXObject form = (PDFormXObject) xobject;
if (form instanceof PDTransparencyGroup) {
context.showTransparencyGroup((PDTransparencyGroup) form);
} else {
context.showForm(form);
}
} finally {
context.decreaseLevel();
}
}
}
use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class DrawObject method process.
@Override
public void process(Operator operator, List<COSBase> operands) throws IOException {
if (operands.isEmpty()) {
throw new MissingOperandException(operator, operands);
}
COSBase base0 = operands.get(0);
if (!(base0 instanceof COSName)) {
return;
}
COSName objectName = (COSName) base0;
PDXObject xobject = context.getResources().getXObject(objectName);
if (xobject == null) {
throw new MissingResourceException("Missing XObject: " + objectName.getName());
} else if (xobject instanceof PDImageXObject) {
PDImageXObject image = (PDImageXObject) xobject;
context.drawImage(image);
} else if (xobject instanceof PDFormXObject) {
try {
context.increaseLevel();
if (context.getLevel() > 25) {
Log.e("PdfBox-Android", "recursion is too deep, skipping form XObject");
return;
}
PDFormXObject form = (PDFormXObject) xobject;
if (form instanceof PDTransparencyGroup) {
context.showTransparencyGroup((PDTransparencyGroup) form);
} else {
context.showForm(form);
}
} finally {
context.decreaseLevel();
}
}
}
Aggregations