com.lowagie.text.pdf.collection.PdfCollectionSort Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of itext2 Show documentation
Show all versions of itext2 Show documentation
Itext is a java library to create and manipulate PDFs.
This is a fork of version 2.1.7 the last MPL/LGPL version.
It's focused basically on mantain compatibility with newer bouncycastle releases and small bugfixes.
package com.lowagie.text.pdf.collection;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfBoolean;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfObject;
public class PdfCollectionSort extends PdfDictionary {
/**
* Constructs a PDF Collection Sort Dictionary.
* @param key the key of the field that will be used to sort entries
*/
public PdfCollectionSort(String key) {
super(PdfName.COLLECTIONSORT);
put(PdfName.S, new PdfName(key));
}
/**
* Constructs a PDF Collection Sort Dictionary.
* @param keys the keys of the fields that will be used to sort entries
*/
public PdfCollectionSort(String[] keys) {
super(PdfName.COLLECTIONSORT);
PdfArray array = new PdfArray();
for (int i = 0; i < keys.length; i++) {
array.add(new PdfName(keys[i]));
}
put(PdfName.S, array);
}
/**
* Defines the sort order of the field (ascending or descending).
* @param ascending true is the default, use false for descending order
*/
public void setSortOrder(boolean ascending) {
PdfObject o = get(PdfName.S);
if (o instanceof PdfName) {
put(PdfName.A, new PdfBoolean(ascending));
}
else {
throw new IllegalArgumentException("You have to define a boolean array for this collection sort dictionary.");
}
}
/**
* Defines the sort order of the field (ascending or descending).
* @param ascending an array with every element corresponding with a name of a field.
*/
public void setSortOrder(boolean[] ascending) {
PdfObject o = get(PdfName.S);
if (o instanceof PdfArray) {
if (((PdfArray)o).size() != ascending.length) {
throw new IllegalArgumentException("The number of booleans in this array doesn't correspond with the number of fields.");
}
PdfArray array = new PdfArray();
for (int i = 0; i < ascending.length; i++) {
array.add(new PdfBoolean(ascending[i]));
}
put(PdfName.A, array);
}
else {
throw new IllegalArgumentException("You need a single boolean for this collection sort dictionary.");
}
}
}