本文整理汇总了Java中org.apache.pdfbox.cos.COSArray类的典型用法代码示例。如果您正苦于以下问题:Java COSArray类的具体用法?Java COSArray怎么用?Java COSArray使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
COSArray类属于org.apache.pdfbox.cos包,在下文中一共展示了COSArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAsArray
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
@Override
public PdfArray getAsArray(String name) {
COSArray array = (COSArray) wrapped.getDictionaryObject(name);
if (array == null) {
return null;
}
return new PdfBoxArray(array, document);
}
示例2: writeInputFieldToPDFPage
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/43604973/creating-a-checkbox-and-printing-it-to-pdf-file-is-not-working-using-pdfbox-1-8">
* Creating a checkbox and printing it to pdf file is not working using pdfbox 1.8.9 api
* </a>
* <p>
* The OP's method for checkbox creation.
* </p>
* @see #testCheckboxLikeSureshGoud()
*/
public static void writeInputFieldToPDFPage( PDPage pdPage, PDDocument document, Float x, Float y, Boolean ticked) throws IOException {
PDFont font = PDType1Font.HELVETICA;
PDResources res = new PDResources();
String fontName = res.addFont(font);
String da = ticked?"/" + fontName + " 10 Tf 0 0.4 0 rg":"";
COSDictionary acroFormDict = new COSDictionary();
acroFormDict.setBoolean(COSName.getPDFName("NeedAppearances"), true);
acroFormDict.setItem(COSName.FIELDS, new COSArray());
acroFormDict.setItem(COSName.DA, new COSString(da));
PDAcroForm acroForm = new PDAcroForm(document, acroFormDict);
acroForm.setDefaultResources(res);
document.getDocumentCatalog().setAcroForm(acroForm);
PDGamma colourBlack = new PDGamma();
PDAppearanceCharacteristicsDictionary fieldAppearance =
new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setBorderColour(colourBlack);
if(ticked) {
COSArray arr = new COSArray();
arr.add(new COSFloat(0.89f));
arr.add(new COSFloat(0.937f));
arr.add(new COSFloat(1f));
fieldAppearance.setBackground(new PDGamma(arr));
}
COSDictionary cosDict = new COSDictionary();
COSArray rect = new COSArray();
rect.add(new COSFloat(x));
rect.add(new COSFloat(new Float(y-5)));
rect.add(new COSFloat(new Float(x+10)));
rect.add(new COSFloat(new Float(y+5)));
cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
if(ticked) {
cosDict.setItem(COSName.TU, new COSString("Checkbox with PDFBox"));
}
cosDict.setItem(COSName.T, new COSString("Chk"));
//Tick mark color and size of the mark
cosDict.setItem(COSName.DA, new COSString(ticked?"/F0 10 Tf 0 0.4 0 rg":"/FF 1 Tf 0 0 g"));
cosDict.setInt(COSName.F, 4);
PDCheckbox checkbox = new PDCheckbox(acroForm, cosDict);
checkbox.setFieldFlags(PDCheckbox.FLAG_READ_ONLY);
checkbox.setValue("Yes");
checkbox.getWidget().setAppearanceCharacteristics(fieldAppearance);
pdPage.getAnnotations().add(checkbox.getWidget());
acroForm.getFields().add(checkbox);
}
示例3: validate
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
/**
* Validates the PDF file specified in the constructor
* @return PDFDocumentInfo structure
**/
public PDFDocumentInfo validate() throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {
String infoString = null;
PDDocument document = null;
try {
document = PDDocument.load(new File(path));
COSDictionary trailer = document.getDocument().getTrailer();
COSDictionary root = (COSDictionary) trailer.getDictionaryObject(COSName.ROOT);
COSDictionary acroForm = (COSDictionary) root.getDictionaryObject(COSName.ACRO_FORM);
if (acroForm == null) {
return doc;
}
COSArray fields = (COSArray) acroForm.getDictionaryObject(COSName.FIELDS);
for (int i = 0; i < fields.size(); i ++) {
COSDictionary field = (COSDictionary) fields.getObject(i);
COSName type = field.getCOSName(COSName.FT);
if (COSName.SIG.equals(type)) {
COSDictionary sig = (COSDictionary) field.getDictionaryObject(COSName.V);
if (sig != null) {
getSignatureInfo(sig);
}
}
}
}
finally {
if (document != null) {
document.close();
}
}
return doc;
}
示例4: process
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
@Override
public void process(Operator operator, List<COSBase> arguments)
throws IOException {
if (arguments.isEmpty()) {
throw new MissingOperandException(operator, arguments);
}
PDTextState textState = context.getGraphicsState().getTextState();
float fontSize = textState.getFontSize();
float horizontalScaling = textState.getHorizontalScaling() / 100f;
boolean isVertical = textState.getFont().isVertical();
COSArray array = (COSArray) arguments.get(0);
for (COSBase obj : array) {
if (obj instanceof COSNumber) {
float tj = ((COSNumber) obj).floatValue();
// calculate the combined displacements
float tx, ty;
if (isVertical) {
tx = 0;
ty = -tj / 1000 * fontSize;
} else {
tx = -tj / 1000 * fontSize * horizontalScaling;
ty = 0;
}
Matrix translate = Matrix.getTranslateInstance(tx, ty);
context.getTextMatrix().concatenate(translate);
} else if (obj instanceof COSString) {
List<COSBase> args = new ArrayList<COSBase>();
args.add(obj);
context.processOperator("Tj", args);
} else {
throw new IOException("Unknown type in array for TJ operation:" + obj);
}
}
}
示例5: clipPage
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
void clipPage(PDDocument document, PDPage page, BoundingBox clipBox) throws IOException
{
PDPageContentStream pageContentStream = new PDPageContentStream(document, page, true, false);
pageContentStream.addRect(clipBox.getLowerLeftX(), clipBox.getLowerLeftY(), clipBox.getWidth(), clipBox.getHeight());
pageContentStream.clipPath(PathIterator.WIND_NON_ZERO);
pageContentStream.close();
COSArray newContents = new COSArray();
COSStreamArray contents = (COSStreamArray) page.getContents().getStream();
newContents.add(contents.get(contents.getStreamCount()-1));
for (int i = 0; i < contents.getStreamCount()-1; i++)
{
newContents.add(contents.get(i));
}
page.setContents(new PDStream(new COSStreamArray(newContents)));
}
示例6: pushUnder
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
/**
* This method on each page moves the last content stream
* to the second position.
*/
void pushUnder(PDDocument document)
{
List<?> pages = document.getDocumentCatalog().getAllPages();
float fontSize = 70.0f;
for (int i = 0; i < pages.size(); i++) {
PDPage page = (PDPage) pages.get(i);
COSBase contents = page.getCOSDictionary().getDictionaryObject(COSName.CONTENTS);
if (contents instanceof COSStreamArray)
{
COSStreamArray contentsArray = (COSStreamArray) contents;
COSArray newArray = new COSArray();
newArray.add(contentsArray.get(0));
newArray.add(contentsArray.get(contentsArray.getStreamCount() - 1));
for (int j = 1; j < contentsArray.getStreamCount() - 1; j++)
{
newArray.add(contentsArray.get(j));
}
COSStreamArray newStreamArray = new COSStreamArray(newArray);
page.getCOSDictionary().setItem(COSName.CONTENTS, newStreamArray);
}
}
}
示例7: createMarkedAnnotations
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
private FileAnnotation createMarkedAnnotations(int pageIndex, PDPage page, PDAnnotation annotation) {
FileAnnotation annotationBelongingToMarking = new FileAnnotation(
annotation.getDictionary().getString(COSName.T), FileAnnotation.extractModifiedTime(annotation.getModifiedDate()),
pageIndex + 1, annotation.getContents(), FileAnnotationType.valueOf(annotation.getSubtype().toUpperCase(Locale.ROOT)), Optional.empty());
if (annotationBelongingToMarking.getAnnotationType().isLinkedFileAnnotationType()) {
try {
COSArray boundingBoxes = (COSArray) annotation.getDictionary().getDictionaryObject(COSName.getPDFName("QuadPoints"));
annotation.setContents(new TextExtractor(page, boundingBoxes).extractMarkedText());
} catch (IOException e) {
annotation.setContents("JabRef: Could not extract any marked text!");
}
}
//Marked text that has a sticky note on it should be linked to the sticky note
return new FileAnnotation(annotation, pageIndex + 1, annotationBelongingToMarking);
}
示例8: calculateSegmentBoundingBox
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
private Rectangle2D calculateSegmentBoundingBox(COSArray quadsArray, int segmentPointer) {
// Extract coordinate values
float upperLeftX = toFloat(quadsArray.get(segmentPointer));
float upperLeftY = toFloat(quadsArray.get(segmentPointer + 1));
float upperRightX = toFloat(quadsArray.get(segmentPointer + 2));
float upperRightY = toFloat(quadsArray.get(segmentPointer + 3));
float lowerLeftX = toFloat(quadsArray.get(segmentPointer + 4));
float lowerLeftY = toFloat(quadsArray.get(segmentPointer + 5));
// Post-processing of the raw coordinates.
PDRectangle pageSize = page.getMediaBox();
float ulx = upperLeftX - 1; // It is magic.
float uly = pageSize.getHeight() - upperLeftY;
float width = upperRightX - lowerLeftX;
float height = upperRightY - lowerLeftY;
return new Rectangle2D.Float(ulx, uly, width, height);
}
示例9: process
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
/**
* TJ Show text, allowing individual glyph positioning.
* @param operator The operator that is being executed.
* @param arguments List
* @throws IOException If there is an error processing this operator.
*/
public void process(PDFOperator operator, List arguments) throws IOException
{
((PDFObjectExtractor)context).setNewTextFragment(true);
COSArray array = (COSArray)arguments.get( 0 );
float adjustment=0;
for( int i=0; i<array.size(); i++ )
{
COSBase next = array.get( i );
if( next instanceof COSNumber )
{
adjustment = ((COSNumber)next).floatValue();
Matrix adjMatrix = new Matrix();
adjustment=(-adjustment/1000)*context.getGraphicsState().getTextState().getFontSize() *
(context.getGraphicsState().getTextState().getHorizontalScalingPercent()/100);
adjMatrix.setValue( 2, 0, adjustment );
context.setTextMatrix( adjMatrix.multiply( context.getTextMatrix() ) );
}
else if( next instanceof COSString )
{
// TEST 27.08.09
// ((PDFObjectExtractor)context).setNewTextFragment(true);
((PDFObjectExtractor)context).showString( ((COSString)next).getBytes() , i);
//((PDFObjectExtractor)context).setNewTextFragment(false);
// automatically set in addCharacter method (simplest way possible)
}
else
{
throw new IOException( "Unknown type in array for TJ operation:" + next );
}
}
}
示例10: toPDGamma
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
private static PDGamma toPDGamma(final Color color) {
COSArray values = new COSArray();
values.add(new COSFloat(color.getRed() / 255f));
values.add(new COSFloat(color.getGreen() / 255f));
values.add(new COSFloat(color.getBlue() / 255f));
return new PDGamma(values);
}
示例11: process
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
@Override
public void process(Operator operator, List<COSBase> arguments)
throws IOException {
PDColorSpace colorSpace = getColorSpace();
if (colorSpace instanceof PDDeviceColorSpace
&& arguments.size() < colorSpace.getNumberOfComponents()) {
throw new MissingOperandException(operator, arguments);
}
COSArray array = new COSArray();
array.addAll(arguments);
setColor(new PDColor(array, colorSpace));
}
示例12: createArray
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
COSArray createArray(Iterable<byte[]> datas) throws IOException
{
COSArray array = new COSArray();
array.setNeedToBeUpdate(true);
if (datas != null)
{
for (byte[] data: datas)
array.add(createStream(data));
}
return array;
}
示例13: addMetadata
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
/**
* Used when processing custom metadata entries, as PDFBox won't do the
* conversion for us in the way it does for the standard ones
*/
private void addMetadata(Metadata metadata, String name, COSBase value) {
if (value instanceof COSArray) {
for (Object v : ((COSArray) value).toList()) {
addMetadata(metadata, name, ((COSBase) v));
}
} else if (value instanceof COSString) {
addMetadata(metadata, name, ((COSString) value).getString());
} else if (value != null) {
addMetadata(metadata, name, value.toString());
}
}
示例14: PdfBoxArray
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
public PdfBoxArray(COSArray wrapped, PDDocument document) {
this.wrapped = wrapped;
this.document = document;
}
示例15: generateSimpleTemplate
import org.apache.pdfbox.cos.COSArray; //导入依赖的package包/类
/**
* Generates a sample PDF template with one form field with the name "SampleField"
*/
byte[] generateSimpleTemplate() throws IOException, COSVisitorException
{
try ( PDDocument template = new PDDocument();
InputStream fontStream = getClass().getResourceAsStream("LiberationSans-Regular.ttf");
ByteArrayOutputStream resultStream = new ByteArrayOutputStream() )
{
PDPage page = new PDPage();
template.addPage(page);
PDTrueTypeFont font = PDTrueTypeFont.loadTTF(template, fontStream);
// add a new AcroForm and add that to the document
PDAcroForm acroForm = new PDAcroForm(template);
template.getDocumentCatalog().setAcroForm(acroForm);
// Add and set the resources and default appearance
PDResources res = new PDResources();
String fontName = res.addFont(font);
acroForm.setDefaultResources(res);
String da = "/" + fontName + " 12 Tf 0 g";
//acroForm.setDefaultAppearance(da);
COSDictionary cosDict = new COSDictionary();
COSArray rect = new COSArray();
rect.add(new COSFloat(250f)); // lower x boundary
rect.add(new COSFloat(700f)); // lower y boundary
rect.add(new COSFloat(500f)); // upper x boundary
rect.add(new COSFloat(750f)); // upper y boundary
cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Tx")); // Field Type
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict.setItem(COSName.DA, new COSString(da));
// add a form field to the form
PDTextbox textBox = new PDTextbox(acroForm, cosDict);
textBox.setPartialName("SampleField");
acroForm.getFields().add(textBox);
// specify the annotation associated with the field
// and add it to the page
PDAnnotationWidget widget = textBox.getWidget();
page.getAnnotations().add(widget);
//textBox.setValue("Test");
template.save(resultStream);
return resultStream.toByteArray();
}
}