📄 pdfannotation.java
字号:
for (int k = 0; k < quadPoints.length; ++k)
array.add(new PdfNumber(quadPoints[k]));
annot.put(PdfName.QUADPOINTS, array);
return annot;
}
/**
* Adds a Stamp to your document. Move over the stamp and a tooltip is shown
* @param writer
* @param rect
* @param contents
* @param name
* @return A PdfAnnotation
*/
public static PdfAnnotation createStamp(PdfWriter writer, Rectangle rect, String contents, String name) {
PdfAnnotation annot = new PdfAnnotation(writer, rect);
annot.put(PdfName.SUBTYPE, PdfName.STAMP);
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
annot.put(PdfName.NAME, new PdfName(name));
return annot;
}
public static PdfAnnotation createInk(PdfWriter writer, Rectangle rect, String contents, float inkList[][]) {
PdfAnnotation annot = new PdfAnnotation(writer, rect);
annot.put(PdfName.SUBTYPE, PdfName.INK);
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
PdfArray outer = new PdfArray();
for (int k = 0; k < inkList.length; ++k) {
PdfArray inner = new PdfArray();
float deep[] = inkList[k];
for (int j = 0; j < deep.length; ++j)
inner.add(new PdfNumber(deep[j]));
outer.add(inner);
}
annot.put(PdfName.INKLIST, outer);
return annot;
}
/** Creates a file attachment annotation.
* @param writer the <CODE>PdfWriter</CODE>
* @param rect the dimensions in the page of the annotation
* @param contents the file description
* @param fileStore an array with the file. If it's <CODE>null</CODE>
* the file will be read from the disk
* @param file the path to the file. It will only be used if
* <CODE>fileStore</CODE> is not <CODE>null</CODE>
* @param fileDisplay the actual file name stored in the pdf
* @throws IOException on error
* @return the annotation
*/
public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String contents, byte fileStore[], String file, String fileDisplay) throws IOException {
return createFileAttachment(writer, rect, contents, PdfFileSpecification.fileEmbedded(writer, file, fileDisplay, fileStore));
}
/** Creates a file attachment annotation
* @param writer
* @param rect
* @param contents
* @param fs
* @return the annotation
* @throws IOException
*/
public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String contents, PdfFileSpecification fs) throws IOException {
PdfAnnotation annot = new PdfAnnotation(writer, rect);
annot.put(PdfName.SUBTYPE, PdfName.FILEATTACHMENT);
if (contents != null)
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
annot.put(PdfName.FS, fs.getReference());
return annot;
}
/**
* Adds a popup to your document.
* @param writer
* @param rect
* @param contents
* @param open
* @return A PdfAnnotation
*/
public static PdfAnnotation createPopup(PdfWriter writer, Rectangle rect, String contents, boolean open) {
PdfAnnotation annot = new PdfAnnotation(writer, rect);
annot.put(PdfName.SUBTYPE, PdfName.POPUP);
if (contents != null)
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
if (open)
annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
return annot;
}
public void setDefaultAppearanceString(PdfContentByte cb) {
byte b[] = cb.getInternalBuffer().toByteArray();
int len = b.length;
for (int k = 0; k < len; ++k) {
if (b[k] == '\n')
b[k] = 32;
}
put(PdfName.DA, new PdfString(b));
}
public void setFlags(int flags) {
if (flags == 0)
remove(PdfName.F);
else
put(PdfName.F, new PdfNumber(flags));
}
public void setBorder(PdfBorderArray border) {
put(PdfName.BORDER, border);
}
public void setBorderStyle(PdfBorderDictionary border) {
put(PdfName.BS, border);
}
/**
* Sets the annotation's highlighting mode. The values can be
* <CODE>HIGHLIGHT_NONE</CODE>, <CODE>HIGHLIGHT_INVERT</CODE>,
* <CODE>HIGHLIGHT_OUTLINE</CODE> and <CODE>HIGHLIGHT_PUSH</CODE>;
* @param highlight the annotation's highlighting mode
*/
public void setHighlighting(PdfName highlight) {
if (highlight.equals(HIGHLIGHT_INVERT))
remove(PdfName.H);
else
put(PdfName.H, highlight);
}
public void setAppearance(PdfName ap, PdfTemplate template) {
PdfDictionary dic = (PdfDictionary)get(PdfName.AP);
if (dic == null)
dic = new PdfDictionary();
dic.put(ap, template.getIndirectReference());
put(PdfName.AP, dic);
if (!form)
return;
if (templates == null)
templates = new HashMap();
templates.put(template, null);
}
public void setAppearance(PdfName ap, String state, PdfTemplate template) {
PdfDictionary dicAp = (PdfDictionary)get(PdfName.AP);
if (dicAp == null)
dicAp = new PdfDictionary();
PdfDictionary dic;
PdfObject obj = dicAp.get(ap);
if (obj != null && obj.isDictionary())
dic = (PdfDictionary)obj;
else
dic = new PdfDictionary();
dic.put(new PdfName(state), template.getIndirectReference());
dicAp.put(ap, dic);
put(PdfName.AP, dicAp);
if (!form)
return;
if (templates == null)
templates = new HashMap();
templates.put(template, null);
}
public void setAppearanceState(String state) {
if (state == null) {
remove(PdfName.AS);
return;
}
put(PdfName.AS, new PdfName(state));
}
public void setColor(Color color) {
put(PdfName.C, new PdfColor(color));
}
public void setTitle(String title) {
if (title == null) {
remove(PdfName.T);
return;
}
put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
}
public void setPopup(PdfAnnotation popup) {
put(PdfName.POPUP, popup.getIndirectReference());
popup.put(PdfName.PARENT, getIndirectReference());
}
public void setAction(PdfAction action) {
put(PdfName.A, action);
}
public void setAdditionalActions(PdfName key, PdfAction action) {
PdfDictionary dic;
PdfObject obj = get(PdfName.AA);
if (obj != null && obj.isDictionary())
dic = (PdfDictionary)obj;
else
dic = new PdfDictionary();
dic.put(key, action);
put(PdfName.AA, dic);
}
/** Getter for property used.
* @return Value of property used.
*/
public boolean isUsed() {
return used;
}
/** Setter for property used.
*/
public void setUsed() {
used = true;
}
public HashMap getTemplates() {
return templates;
}
/** Getter for property form.
* @return Value of property form.
*/
public boolean isForm() {
return form;
}
/** Getter for property annotation.
* @return Value of property annotation.
*/
public boolean isAnnotation() {
return annotation;
}
public void setPage(int page) {
put(PdfName.P, writer.getPageReference(page));
}
public void setPage() {
put(PdfName.P, writer.getCurrentPage());
}
/** Getter for property placeInPage.
* @return Value of property placeInPage.
*/
public int getPlaceInPage() {
return placeInPage;
}
/** Places the annotation in a specified page that must be greater
* or equal to the current one. With <code>PdfStamper</code> the page
* can be any. The first page is 1.
* @param placeInPage New value of property placeInPage.
*/
public void setPlaceInPage(int placeInPage) {
this.placeInPage = placeInPage;
}
public void setRotate(int v) {
put(PdfName.ROTATE, new PdfNumber(v));
}
PdfDictionary getMK() {
PdfDictionary mk = (PdfDictionary)get(PdfName.MK);
if (mk == null) {
mk = new PdfDictionary();
put(PdfName.MK, mk);
}
return mk;
}
public void setMKRotation(int rotation) {
getMK().put(PdfName.R, new PdfNumber(rotation));
}
public static PdfArray getMKColor(Color color) {
PdfArray array = new PdfArray();
int type = ExtendedColor.getType(color);
switch (type) {
case ExtendedColor.TYPE_GRAY: {
array.add(new PdfNumber(((GrayColor)color).getGray()));
break;
}
case ExtendedColor.TYPE_CMYK: {
CMYKColor cmyk = (CMYKColor)color;
array.add(new PdfNumber(cmyk.getCyan()));
array.add(new PdfNumber(cmyk.getMagenta()));
array.add(new PdfNumber(cmyk.getYellow()));
array.add(new PdfNumber(cmyk.getBlack()));
break;
}
case ExtendedColor.TYPE_SEPARATION:
case ExtendedColor.TYPE_PATTERN:
case ExtendedColor.TYPE_SHADING:
throw new RuntimeException("Separations, patterns and shadings are not allowed in MK dictionary.");
default:
array.add(new PdfNumber(color.getRed() / 255f));
array.add(new PdfNumber(color.getGreen() / 255f));
array.add(new PdfNumber(color.getBlue() / 255f));
}
return array;
}
public void setMKBorderColor(Color color) {
if (color == null)
getMK().remove(PdfName.BC);
else
getMK().put(PdfName.BC, getMKColor(color));
}
public void setMKBackgroundColor(Color color) {
if (color == null)
getMK().remove(PdfName.BG);
else
getMK().put(PdfName.BG, getMKColor(color));
}
public void setMKNormalCaption(String caption) {
getMK().put(PdfName.CA, new PdfString(caption, PdfObject.TEXT_UNICODE));
}
public void setMKRolloverCaption(String caption) {
getMK().put(PdfName.RC, new PdfString(caption, PdfObject.TEXT_UNICODE));
}
public void setMKAlternateCaption(String caption) {
getMK().put(PdfName.AC, new PdfString(caption, PdfObject.TEXT_UNICODE));
}
public void setMKNormalIcon(PdfTemplate template) {
getMK().put(PdfName.I, template.getIndirectReference());
}
public void setMKRolloverIcon(PdfTemplate template) {
getMK().put(PdfName.RI, template.getIndirectReference());
}
public void setMKAlternateIcon(PdfTemplate template) {
getMK().put(PdfName.IX, template.getIndirectReference());
}
public void setMKIconFit(PdfName scale, PdfName scalingType, float leftoverLeft, float leftoverBottom, boolean fitInBounds) {
PdfDictionary dic = new PdfDictionary();
if (!scale.equals(PdfName.A))
dic.put(PdfName.SW, scale);
if (!scalingType.equals(PdfName.P))
dic.put(PdfName.S, scalingType);
if (leftoverLeft != 0.5f || leftoverBottom != 0.5f) {
PdfArray array = new PdfArray(new PdfNumber(leftoverLeft));
array.add(new PdfNumber(leftoverBottom));
dic.put(PdfName.A, array);
}
if (fitInBounds)
dic.put(PdfName.FB, PdfBoolean.PDFTRUE);
getMK().put(PdfName.IF, dic);
}
public void setMKTextPosition(int tp) {
getMK().put(PdfName.TP, new PdfNumber(tp));
}
/**
* Sets the layer this annotation belongs to.
* @param layer the layer this annotation belongs to
*/
public void setLayer(PdfOCG layer) {
put(PdfName.OC, layer.getRef());
}
/**
* Sets the name of the annotation.
* With this name the annotation can be identified among
* all the annotations on a page (it has to be unique).
*/
public void setName(String name) {
put(PdfName.NM, new PdfString(name));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -