use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.
the class PDFreeTextAppearanceHandler method extractNonStrokingColor.
// get the last non stroking color from the /DA entry
private PDColor extractNonStrokingColor(PDAnnotationMarkup annotation) {
// It could also work with a regular expression, but that should be written so that
// "/LucidaConsole 13.94766 Tf .392 .585 .93 rg" does not produce "2 .585 .93 rg" as result
// Another alternative might be to create a PDDocument and a PDPage with /DA content as /Content,
// process the whole thing and then get the non stroking color.
PDColor strokingColor = new PDColor(new float[] { 0 }, PDDeviceGray.INSTANCE);
String defaultAppearance = annotation.getDefaultAppearance();
if (defaultAppearance == null) {
return strokingColor;
}
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 colors = null;
Operator graphicOp = null;
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.NON_STROKING_GRAY.equals(name) || OperatorName.NON_STROKING_RGB.equals(name) || OperatorName.NON_STROKING_CMYK.equals(name)) {
graphicOp = op;
colors = arguments;
}
arguments = new COSArray();
} else {
arguments.add((COSBase) token);
}
}
if (graphicOp != null) {
String graphicOpName = graphicOp.getName();
if (OperatorName.NON_STROKING_GRAY.equals(graphicOpName)) {
strokingColor = new PDColor(colors, PDDeviceGray.INSTANCE);
} else if (OperatorName.NON_STROKING_RGB.equals(graphicOpName)) {
strokingColor = new PDColor(colors, PDDeviceRGB.INSTANCE);
} else if (OperatorName.NON_STROKING_CMYK.equals(graphicOpName)) {
// strokingColor = new PDColor(colors, PDDeviceCMYK.INSTANCE); TODO: PdfBox-Android
}
}
} catch (IOException ex) {
Log.w("PdfBox-Android", "Problem parsing /DA, will use default black", ex);
}
return strokingColor;
}
use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.
the class PDInkAppearanceHandler method generateNormalAppearance.
@Override
public void generateNormalAppearance() {
PDAnnotationMarkup ink = (PDAnnotationMarkup) getAnnotation();
// PDF spec does not mention /Border for ink annotations, but it is used if /BS is not available
AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(ink, ink.getBorderStyle());
PDColor color = ink.getColor();
if (color == null || color.getComponents().length == 0 || Float.compare(ab.width, 0) == 0) {
return;
}
PDAppearanceContentStream cs = null;
try {
cs = getNormalAppearanceAsContentStream();
setOpacity(cs, ink.getConstantOpacity());
cs.setStrokingColor(color);
if (ab.dashArray != null) {
cs.setLineDashPattern(ab.dashArray, 0);
}
cs.setLineWidth(ab.width);
for (float[] pathArray : ink.getInkList()) {
int nPoints = pathArray.length / 2;
// in an implementation-dependent way" - we do lines.
for (int i = 0; i < nPoints; ++i) {
float x = pathArray[i * 2];
float y = pathArray[i * 2 + 1];
if (i == 0) {
cs.moveTo(x, y);
} else {
cs.lineTo(x, y);
}
}
cs.stroke();
}
} catch (IOException ex) {
Log.e("PdfBox-Android", ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(cs);
}
}
use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.
the class TestCheckBox method testCheckBoxNoAppearance.
/**
* PDFBOX-4366: Create and test a checkbox with no /AP. The created file works with Adobe Reader!
*
* @throws IOException
*/
public void testCheckBoxNoAppearance() throws IOException {
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(doc);
// need this or it won't appear on Adobe Reader
acroForm.setNeedAppearances(true);
doc.getDocumentCatalog().setAcroForm(acroForm);
List<PDField> fields = new ArrayList<PDField>();
PDCheckBox checkBox = new PDCheckBox(acroForm);
checkBox.setPartialName("checkbox");
PDAnnotationWidget widget = checkBox.getWidgets().get(0);
widget.setRectangle(new PDRectangle(50, 600, 100, 100));
PDBorderStyleDictionary bs = new PDBorderStyleDictionary();
bs.setStyle(PDBorderStyleDictionary.STYLE_SOLID);
bs.setWidth(1);
COSDictionary acd = new COSDictionary();
PDAppearanceCharacteristicsDictionary ac = new PDAppearanceCharacteristicsDictionary(acd);
ac.setBackground(new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE));
ac.setBorderColour(new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE));
// 4 is checkmark, 8 is cross
ac.setNormalCaption("4");
widget.setAppearanceCharacteristics(ac);
widget.setBorderStyle(bs);
checkBox.setValue("Off");
fields.add(checkBox);
page.getAnnotations().add(widget);
acroForm.setFields(fields);
assertEquals("Off", checkBox.getValue());
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.
the class PDPageContentStream method setStrokingColor.
/**
* Set the stroking color using an AWT color. Conversion uses the default sRGB color space.
*
* @param color The color to set.
* @throws IOException If an IO error occurs while writing to the stream.
*/
public void setStrokingColor(AWTColor color) throws IOException {
float[] components = new float[] { color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f };
PDColor pdColor = new PDColor(components, PDDeviceRGB.INSTANCE);
setStrokingColor(pdColor);
}
use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor in project PdfBox-Android by TomRoush.
the class PDFStreamEngine method processTilingPattern.
/**
* Process the given tiling pattern. Allows the pattern matrix to be overridden for custom
* rendering.
*
* @param tilingPattern the tiling pattern
* @param color color to use, if this is an uncoloured pattern, otherwise null.
* @param colorSpace color space to use, if this is an uncoloured pattern, otherwise null.
* @param patternMatrix the pattern matrix, may be overridden for custom rendering.
* @throws IOException if there is an error reading or parsing the tiling pattern content stream.
*/
protected final void processTilingPattern(PDTilingPattern tilingPattern, PDColor color, PDColorSpace colorSpace, Matrix patternMatrix) throws IOException {
PDResources parent = pushResources(tilingPattern);
Matrix parentMatrix = initialMatrix;
initialMatrix = Matrix.concatenate(initialMatrix, patternMatrix);
// save the original graphics state
Deque<PDGraphicsState> savedStack = saveGraphicsStack();
// save a clean state (new clipping path, line path, etc.)
RectF bbox = new RectF();
tilingPattern.getBBox().transform(patternMatrix).computeBounds(bbox, true);
PDRectangle rect = new PDRectangle((float) bbox.left, (float) bbox.top, (float) bbox.width(), (float) bbox.height());
graphicsStack.push(new PDGraphicsState(rect));
// non-colored patterns have to be given a color
if (colorSpace != null) {
color = new PDColor(color.getComponents(), colorSpace);
getGraphicsState().setNonStrokingColorSpace(colorSpace);
getGraphicsState().setNonStrokingColor(color);
getGraphicsState().setStrokingColorSpace(colorSpace);
getGraphicsState().setStrokingColor(color);
}
// transform the CTM using the stream's matrix
getGraphicsState().getCurrentTransformationMatrix().concatenate(patternMatrix);
// clip to bounding box
clipToRect(tilingPattern.getBBox());
processStreamOperators(tilingPattern);
initialMatrix = parentMatrix;
restoreGraphicsStack(savedStack);
popResources(parent);
}
Aggregations