use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class PDStructureElement method attributeChanged.
/**
* Updates the revision number for the given attribute object.
*
* @param attributeObject the attribute object
*/
public void attributeChanged(PDAttributeObject attributeObject) {
COSName key = COSName.A;
COSBase a = this.getCOSObject().getDictionaryObject(key);
if (a instanceof COSArray) {
COSArray array = (COSArray) a;
for (int i = 0; i < array.size(); i++) {
COSBase entry = array.getObject(i);
if (entry.equals(attributeObject.getCOSObject())) {
COSBase next = array.get(i + 1);
if (next instanceof COSInteger) {
array.set(i + 1, COSInteger.get(this.getRevisionNumber()));
}
}
}
} else {
COSArray array = new COSArray();
array.add(a);
array.add(COSInteger.get(this.getRevisionNumber()));
this.getCOSObject().setItem(key, array);
}
}
use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class PDOptionalContentGroup method getRenderState.
// TODO Add support for "Intent"
/**
* @param destination to be rendered
* @return state or null if undefined
*/
public RenderState getRenderState(RenderDestination destination) {
COSName state = null;
COSDictionary usage = (COSDictionary) dict.getDictionaryObject("Usage");
if (usage != null) {
if (RenderDestination.PRINT.equals(destination)) {
COSDictionary print = (COSDictionary) usage.getDictionaryObject("Print");
state = print == null ? null : (COSName) print.getDictionaryObject("PrintState");
} else if (RenderDestination.VIEW.equals(destination)) {
COSDictionary view = (COSDictionary) usage.getDictionaryObject("View");
state = view == null ? null : (COSName) view.getDictionaryObject("ViewState");
}
// Fallback to export
if (state == null) {
COSDictionary export = (COSDictionary) usage.getDictionaryObject("Export");
state = export == null ? null : (COSName) export.getDictionaryObject("ExportState");
}
}
return state == null ? null : RenderState.valueOf(state);
}
use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class PDCheckBox method getOnValue.
/**
* Get the value which sets the check box to the On state.
*
* <p>The On value should be 'Yes' but other values are possible
* so we need to look for that. On the other hand the Off value shall
* always be 'Off'. If not set or not part of the normal appearance keys
* 'Off' is the default</p>
*
* @return the value setting the check box to the On state.
* If an empty string is returned there is no appearance definition.
*/
public String getOnValue() {
PDAnnotationWidget widget = this.getWidgets().get(0);
PDAppearanceDictionary apDictionary = widget.getAppearance();
String onValue = "";
if (apDictionary != null) {
PDAppearanceEntry normalAppearance = apDictionary.getNormalAppearance();
if (normalAppearance != null) {
Set<COSName> entries = normalAppearance.getSubDictionary().keySet();
for (COSName entry : entries) {
if (COSName.Off.compareTo(entry) != 0) {
onValue = entry.getName();
}
}
}
}
return onValue;
}
use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class PDDefaultAppearanceString method processSetFont.
/**
* Process the set font and font size operator.
*
* @param operands the font name and size
* @throws IOException in case there are missing operators or the font is not within the resources
*/
private void processSetFont(List<COSBase> operands) throws IOException {
if (operands.size() < 2) {
throw new IOException("Missing operands for set font operator " + Arrays.toString(operands.toArray()));
}
COSBase base0 = operands.get(0);
COSBase base1 = operands.get(1);
if (!(base0 instanceof COSName)) {
return;
}
if (!(base1 instanceof COSNumber)) {
return;
}
COSName fontName = (COSName) base0;
PDFont font = defaultResources.getFont(fontName);
float fontSize = ((COSNumber) base1).floatValue();
// todo: handle cases where font == null with special mapping logic (see PDFBOX-2661)
if (font == null) {
throw new IOException("Could not find font: /" + fontName.getName());
}
setFontName(fontName);
setFont(font);
setFontSize(fontSize);
}
use of com.tom_roush.pdfbox.cos.COSName in project PdfBox-Android by TomRoush.
the class PDFTemplateCreator method buildPDF.
/**
* Build a PDF with a visible signature step by step, and return it as a stream.
*
* @param properties
* @return InputStream
* @throws IOException
*/
public InputStream buildPDF(PDVisibleSignDesigner properties) throws IOException {
Log.i("PdfBox-Android", "pdf building has been started");
PDFTemplateStructure pdfStructure = pdfBuilder.getStructure();
// we create array of [Text, ImageB, ImageC, ImageI]
pdfBuilder.createProcSetArray();
// create page
pdfBuilder.createPage(properties);
PDPage page = pdfStructure.getPage();
// create template
pdfBuilder.createTemplate(page);
PDDocument template = pdfStructure.getTemplate();
// create /AcroForm
pdfBuilder.createAcroForm(template);
PDAcroForm acroForm = pdfStructure.getAcroForm();
// AcroForm contains signature fields
pdfBuilder.createSignatureField(acroForm);
PDSignatureField pdSignatureField = pdfStructure.getSignatureField();
// create signature
// TODO
// The line below has no effect with the CreateVisibleSignature example.
// The signature field is needed as a "holder" for the /AP tree,
// but the /P and /V PDSignatureField entries are ignored by PDDocument.addSignature
pdfBuilder.createSignature(pdSignatureField, page, "");
// that is /AcroForm/DR entry
pdfBuilder.createAcroFormDictionary(acroForm, pdSignatureField);
// create AffineTransform
pdfBuilder.createAffineTransform(properties.getTransform());
AffineTransform transform = pdfStructure.getAffineTransform();
// rectangle, formatter, image. /AcroForm/DR/XObject contains that form
pdfBuilder.createSignatureRectangle(pdSignatureField, properties);
pdfBuilder.createFormatterRectangle(properties.getFormatterRectangleParameters());
PDRectangle bbox = pdfStructure.getFormatterRectangle();
pdfBuilder.createSignatureImage(template, properties.getImage());
// create form stream, form and resource.
pdfBuilder.createHolderFormStream(template);
PDStream holderFormStream = pdfStructure.getHolderFormStream();
pdfBuilder.createHolderFormResources();
PDResources holderFormResources = pdfStructure.getHolderFormResources();
pdfBuilder.createHolderForm(holderFormResources, holderFormStream, bbox);
// that is /AP entry the appearance dictionary.
pdfBuilder.createAppearanceDictionary(pdfStructure.getHolderForm(), pdSignatureField);
// inner form stream, form and resource (holder form contains inner form)
pdfBuilder.createInnerFormStream(template);
pdfBuilder.createInnerFormResource();
PDResources innerFormResource = pdfStructure.getInnerFormResources();
pdfBuilder.createInnerForm(innerFormResource, pdfStructure.getInnerFormStream(), bbox);
PDFormXObject innerForm = pdfStructure.getInnerForm();
// inner form must be in the holder form as we wrote
pdfBuilder.insertInnerFormToHolderResources(innerForm, holderFormResources);
// Image form is in this structure: /AcroForm/DR/FRM/Resources/XObject/n2
pdfBuilder.createImageFormStream(template);
PDStream imageFormStream = pdfStructure.getImageFormStream();
pdfBuilder.createImageFormResources();
PDResources imageFormResources = pdfStructure.getImageFormResources();
pdfBuilder.createImageForm(imageFormResources, innerFormResource, imageFormStream, bbox, transform, pdfStructure.getImage());
pdfBuilder.createBackgroundLayerForm(innerFormResource, bbox);
// now inject procSetArray
pdfBuilder.injectProcSetArray(innerForm, page, innerFormResource, imageFormResources, holderFormResources, pdfStructure.getProcSet());
COSName imageFormName = pdfStructure.getImageFormName();
COSName imageName = pdfStructure.getImageName();
COSName innerFormName = pdfStructure.getInnerFormName();
// now create Streams of AP
pdfBuilder.injectAppearanceStreams(holderFormStream, imageFormStream, imageFormStream, imageFormName, imageName, innerFormName, properties);
pdfBuilder.createVisualSignature(template);
pdfBuilder.createWidgetDictionary(pdSignatureField, holderFormResources);
InputStream in = getVisualSignatureAsStream(pdfStructure.getVisualSignature());
Log.i("PdfBox-Android", "stream returning started, size= " + in.available());
// we must close the document
template.close();
// return result of the stream
return in;
}
Aggregations