HashMap<String, PdfObject> names = reader.getNamedDestinationFromNames(); for (Map.Entry<String, PdfObject> entry: names.entrySet()) { System.out.print(entry.getKey()); System.out.print(": p"); PdfArray arr = (PdfArray)entry.getValue(); System.out.println(arr.getAsNumber(0).intValue() + 1); } reader.close();
// given the above stamper AcroFields fields = stamper.getAcroFields(); PdfDictionary replaceAnnot = fields.getFieldItem("ReplaceMe").getMerged(0); //we can remove the field from the PDF without breaking the info in replaceAnnot. fields.removeField("ReplaceMe"); // rects are laid out [llx, lly, urx, ury] PdfArray rect = replaceAnnot.getAsArray(PdfName.RECT); float x = rect.getAsNumber(0).floatValue(); float y = rect.getAsNumber(1).floatValue(); float width = rect.getAsNumber(2).floatValue() - x; float height = rect.getAsNumber(3).floatValue() - y;
private boolean colorsEqual(PdfArray parentColor, float [] color){ if (Float.compare(color[0], parentColor.getAsNumber(0).floatValue()) != 0){ return false; } if (Float.compare(color[1], parentColor.getAsNumber(1).floatValue()) != 0){ return false; } if (Float.compare(color[2], parentColor.getAsNumber(2).floatValue()) != 0){ return false; } return true; }
private boolean colorsEqual(PdfArray parentColor, float [] color){ if (Float.compare(color[0], parentColor.getAsNumber(0).floatValue()) != 0){ return false; } if (Float.compare(color[1], parentColor.getAsNumber(1).floatValue()) != 0){ return false; } if (Float.compare(color[2], parentColor.getAsNumber(2).floatValue()) != 0){ return false; } return true; }
/** * * @return this PdfArray's values as a long[] * @since 5.3.5 */ public long[] asLongArray(){ long[] rslt = new long[size()]; for (int k = 0; k < rslt.length; ++k) { rslt[k] = getAsNumber(k).longValue(); } return rslt; }
/** * * @return this PdfArray's values as a long[] * @since 5.3.5 */ public long[] asLongArray(){ long[] rslt = new long[size()]; for (int k = 0; k < rslt.length; ++k) { rslt[k] = getAsNumber(k).longValue(); } return rslt; }
/** * * @return this PdfArray's values as a double[] * @since 5.5.6 */ public double[] asDoubleArray() { double[] rslt = new double[size()]; for (int k = 0; k < rslt.length; ++k) { rslt[k] = getAsNumber(k).doubleValue(); } return rslt; } }
BaseColor getMKColor(PdfArray ar) { if (ar == null) return null; switch (ar.size()) { case 1: return new GrayColor(ar.getAsNumber(0).floatValue()); case 3: return new BaseColor(ExtendedColor.normalize(ar.getAsNumber(0).floatValue()), ExtendedColor.normalize(ar.getAsNumber(1).floatValue()), ExtendedColor.normalize(ar.getAsNumber(2).floatValue())); case 4: return new CMYKColor(ar.getAsNumber(0).floatValue(), ar.getAsNumber(1).floatValue(), ar.getAsNumber(2).floatValue(), ar.getAsNumber(3).floatValue()); default: return null; } }
public void applyCTM(AffineTransform ctm) { PdfArray origRect = getAsArray(PdfName.RECT); if (origRect != null) { PdfRectangle rect; if (origRect.size() == 4) { rect = new PdfRectangle(origRect.getAsNumber(0).floatValue(), origRect.getAsNumber(1).floatValue(), origRect.getAsNumber(2).floatValue(), origRect.getAsNumber(3).floatValue()); } else { rect = new PdfRectangle(origRect.getAsNumber(0).floatValue(), origRect.getAsNumber(1).floatValue()); } put(PdfName.RECT, rect.transform(ctm)); } }
PdfReader reader = new PdfReader("in.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("out.pdf")); PdfDictionary pagedict = reader.getPageN(1); PdfArray mediabox = pagedict.getAsArray(PdfName.MEDIABOX); mediabox.set(1, new PdfNumber(mediabox.getAsNumber(1).intValue()+50)); mediabox.set(3, new PdfNumber(mediabox.getAsNumber(3).intValue()+50)); stamper.close();
public void applyCTM(AffineTransform ctm) { PdfArray origRect = getAsArray(PdfName.RECT); if (origRect != null) { PdfRectangle rect; if (origRect.size() == 4) { rect = new PdfRectangle(origRect.getAsNumber(0).floatValue(), origRect.getAsNumber(1).floatValue(), origRect.getAsNumber(2).floatValue(), origRect.getAsNumber(3).floatValue()); } else { rect = new PdfRectangle(origRect.getAsNumber(0).floatValue(), origRect.getAsNumber(1).floatValue()); } put(PdfName.RECT, rect.transform(ctm)); } }
BaseColor getMKColor(PdfArray ar) { if (ar == null) return null; switch (ar.size()) { case 1: return new GrayColor(ar.getAsNumber(0).floatValue()); case 3: return new BaseColor(ExtendedColor.normalize(ar.getAsNumber(0).floatValue()), ExtendedColor.normalize(ar.getAsNumber(1).floatValue()), ExtendedColor.normalize(ar.getAsNumber(2).floatValue())); case 4: return new CMYKColor(ar.getAsNumber(0).floatValue(), ar.getAsNumber(1).floatValue(), ar.getAsNumber(2).floatValue(), ar.getAsNumber(3).floatValue()); default: return null; } }
/** * * @return this PdfArray's values as a double[] * @since 5.5.6 */ public double[] asDoubleArray() { double[] rslt = new double[size()]; for (int k = 0; k < rslt.length; ++k) { rslt[k] = getAsNumber(k).doubleValue(); } return rslt; } }
/** * Checks whether the dashed pattern is solid or not. It's solid when the * size of a dash array is even and sum of all the units off in the array * is 0.<br/> * For example: [3 0 4 0 5 0 6 0] (sum is 0), [3 0 4 0 5 1] (sum is 1). */ public boolean isSolid() { if (dashArray.size() % 2 != 0) { return false; } float unitsOffSum = 0; for (int i = 1; i < dashArray.size(); i += 2) { unitsOffSum += dashArray.getAsNumber(i).floatValue(); } return Float.compare(unitsOffSum, 0) == 0; }
/** * Checks whether the dashed pattern is solid or not. It's solid when the * size of a dash array is even and sum of all the units off in the array * is 0.<br/> * For example: [3 0 4 0 5 0 6 0] (sum is 0), [3 0 4 0 5 1] (sum is 1). */ public boolean isSolid() { if (dashArray.size() % 2 != 0) { return false; } float unitsOffSum = 0; for (int i = 1; i < dashArray.size(); i += 2) { unitsOffSum += dashArray.getAsNumber(i).floatValue(); } return Float.compare(unitsOffSum, 0) == 0; }
public void transformDestination(float a, float b, float c, float d, float e, float f) { if (!isInternal()) throw new IllegalArgumentException(MessageLocalization.getComposedMessage("cannot.change.destination.of.external.link")); if (destination.getAsName(1).equals(PdfName.XYZ)) { float x = destination.getAsNumber(2).floatValue(); float y = destination.getAsNumber(3).floatValue(); float xx = x * a + y * c + e; float yy = x * b + y * d + f; destination.set(2, new PdfNumber(xx)); destination.set(3, new PdfNumber(yy)); } }
public void transformDestination(float a, float b, float c, float d, float e, float f) { if (!isInternal()) throw new IllegalArgumentException(MessageLocalization.getComposedMessage("cannot.change.destination.of.external.link")); if (destination.getAsName(1).equals(PdfName.XYZ)) { float x = destination.getAsNumber(2).floatValue(); float y = destination.getAsNumber(3).floatValue(); float xx = x * a + y * c + e; float yy = x * b + y * d + f; destination.set(2, new PdfNumber(xx)); destination.set(3, new PdfNumber(yy)); } }
private void initFirst(float phase) { if (dashArray.size() > 0) { while (phase > 0) { phase -= dashArray.getAsNumber(currentIndex).floatValue(); currentIndex = (currentIndex + 1) % dashArray.size(); elemOrdinalNumber++; } if (phase < 0) { --elemOrdinalNumber; --currentIndex; currentElem = new DashArrayElem(-phase, isEven(elemOrdinalNumber)); } else { currentElem = new DashArrayElem(dashArray.getAsNumber(currentIndex).floatValue(), isEven(elemOrdinalNumber)); } } }
/** * Calculates and returns the next element which is either gap or dash. * @return The next dash array's element. */ public DashArrayElem next() { DashArrayElem ret = currentElem; if (dashArray.size() > 0) { currentIndex = (currentIndex + 1) % dashArray.size(); currentElem = new DashArrayElem(dashArray.getAsNumber(currentIndex).floatValue(), isEven(++elemOrdinalNumber)); } return ret; }
/** * Calculates and returns the next element which is either gap or dash. * @return The next dash array's element. */ public DashArrayElem next() { DashArrayElem ret = currentElem; if (dashArray.size() > 0) { currentIndex = (currentIndex + 1) % dashArray.size(); currentElem = new DashArrayElem(dashArray.getAsNumber(currentIndex).floatValue(), isEven(++elemOrdinalNumber)); } return ret; }