📄 pdfaction.java
字号:
}
else if (!unicode && code.length() < 100) {
js.put(PdfName.JS, new PdfString(code));
}
else {
try {
byte b[] = PdfEncodings.convertToBytes(code, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING);
PdfStream stream = new PdfStream(b);
stream.flateCompress();
js.put(PdfName.JS, writer.addToBody(stream).getIndirectReference());
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
}
return js;
}
/** Creates a JavaScript action. If the JavaScript is smaller than
* 50 characters it will be place as a string, otherwise it will
* be placed as a compressed stream.
* @param code the JavaScript code
* @param writer the writer for this action
* @return the JavaScript action
*/
public static PdfAction javaScript(String code, PdfWriter writer) {
return javaScript(code, writer, false);
}
/**
* A Hide action hides or shows an object.
* @param obj object to hide or show
* @param hide true is hide, false is show
* @return a Hide Action
*/
static PdfAction createHide(PdfObject obj, boolean hide) {
PdfAction action = new PdfAction();
action.put(PdfName.S, PdfName.HIDE);
action.put(PdfName.T, obj);
if (!hide)
action.put(PdfName.H, PdfBoolean.PDFFALSE);
return action;
}
/**
* A Hide action hides or shows an annotation.
* @param annot
* @param hide
* @return A Hide Action
*/
public static PdfAction createHide(PdfAnnotation annot, boolean hide) {
return createHide(annot.getIndirectReference(), hide);
}
/**
* A Hide action hides or shows an annotation.
* @param name
* @param hide
* @return A Hide Action
*/
public static PdfAction createHide(String name, boolean hide) {
return createHide(new PdfString(name), hide);
}
static PdfArray buildArray(Object names[]) {
PdfArray array = new PdfArray();
for (int k = 0; k < names.length; ++k) {
Object obj = names[k];
if (obj instanceof String)
array.add(new PdfString((String)obj));
else if (obj instanceof PdfAnnotation)
array.add(((PdfAnnotation)obj).getIndirectReference());
else
throw new RuntimeException("The array must contain String or PdfAnnotation.");
}
return array;
}
/**
* A Hide action hides or shows objects.
* @param names
* @param hide
* @return A Hide Action
*/
public static PdfAction createHide(Object names[], boolean hide) {
return createHide(buildArray(names), hide);
}
/**
* Creates a submit form.
* @param file the URI to submit the form to
* @param names the objects to submit
* @param flags submit properties
* @return A PdfAction
*/
public static PdfAction createSubmitForm(String file, Object names[], int flags) {
PdfAction action = new PdfAction();
action.put(PdfName.S, PdfName.SUBMITFORM);
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.F, new PdfString(file));
dic.put(PdfName.FS, PdfName.URL);
action.put(PdfName.F, dic);
if (names != null)
action.put(PdfName.FIELDS, buildArray(names));
action.put(PdfName.FLAGS, new PdfNumber(flags));
return action;
}
/**
* Creates a resetform.
* @param names the objects to reset
* @param flags submit properties
* @return A PdfAction
*/
public static PdfAction createResetForm(Object names[], int flags) {
PdfAction action = new PdfAction();
action.put(PdfName.S, PdfName.RESETFORM);
if (names != null)
action.put(PdfName.FIELDS, buildArray(names));
action.put(PdfName.FLAGS, new PdfNumber(flags));
return action;
}
/**
* Creates an Import field.
* @param file
* @return A PdfAction
*/
public static PdfAction createImportData(String file) {
PdfAction action = new PdfAction();
action.put(PdfName.S, PdfName.IMPORTDATA);
action.put(PdfName.F, new PdfString(file));
return action;
}
/** Add a chained action.
* @param na the next action
*/
public void next(PdfAction na) {
PdfObject nextAction = get(PdfName.NEXT);
if (nextAction == null)
put(PdfName.NEXT, na);
else if (nextAction.isDictionary()) {
PdfArray array = new PdfArray(nextAction);
array.add(na);
put(PdfName.NEXT, array);
}
else {
((PdfArray)nextAction).add(na);
}
}
/** Creates a GoTo action to an internal page.
* @param page the page to go. First page is 1
* @param dest the destination for the page
* @param writer the writer for this action
* @return a GoTo action
*/
public static PdfAction gotoLocalPage(int page, PdfDestination dest, PdfWriter writer) {
PdfIndirectReference ref = writer.getPageReference(page);
dest.addPage(ref);
PdfAction action = new PdfAction();
action.put(PdfName.S, PdfName.GOTO);
action.put(PdfName.D, dest);
return action;
}
/**
* Creates a GoTo action to a named destination.
* @param dest the named destination
* @param isName if true sets the destination as a name, if false sets it as a String
* @return a GoTo action
*/
public static PdfAction gotoLocalPage(String dest, boolean isName) {
PdfAction action = new PdfAction();
action.put(PdfName.S, PdfName.GOTO);
if (isName)
action.put(PdfName.D, new PdfName(dest));
else
action.put(PdfName.D, new PdfString(dest, null));
return action;
}
/**
* Creates a GoToR action to a named destination.
* @param filename the file name to go to
* @param dest the destination name
* @param isName if true sets the destination as a name, if false sets it as a String
* @param newWindow open the document in a new window if <CODE>true</CODE>, if false the current document is replaced by the new document.
* @return a GoToR action
*/
public static PdfAction gotoRemotePage(String filename, String dest, boolean isName, boolean newWindow) {
PdfAction action = new PdfAction();
action.put(PdfName.F, new PdfString(filename));
action.put(PdfName.S, PdfName.GOTOR);
if (isName)
action.put(PdfName.D, new PdfName(dest));
else
action.put(PdfName.D, new PdfString(dest, null));
if (newWindow)
action.put(PdfName.NEWWINDOW, PdfBoolean.PDFTRUE);
return action;
}
/**
* Creates a GoToE action to an embedded file.
* @param filename the root document of the target (null if the target is in the same document)
* @param dest the named destination
* @param isName if true sets the destination as a name, if false sets it as a String
* @return a GoToE action
*/
public static PdfAction gotoEmbedded(String filename, PdfTargetDictionary target, String dest, boolean isName, boolean newWindow) {
if (isName)
return gotoEmbedded(filename, target, new PdfName(dest), newWindow);
else
return gotoEmbedded(filename, target, new PdfString(dest, null), newWindow);
}
/**
* Creates a GoToE action to an embedded file.
* @param filename the root document of the target (null if the target is in the same document)
* @param target a path to the target document of this action
* @param dest the destination inside the target document, can be of type PdfDestination, PdfName, or PdfString
* @param newWindow if true, the destination document should be opened in a new window
* @return a GoToE action
*/
public static PdfAction gotoEmbedded(String filename, PdfTargetDictionary target, PdfObject dest, boolean newWindow) {
PdfAction action = new PdfAction();
action.put(PdfName.S, PdfName.GOTOE);
action.put(PdfName.T, target);
action.put(PdfName.D, dest);
action.put(PdfName.NEWWINDOW, new PdfBoolean(newWindow));
if (filename != null) {
action.put(PdfName.F, new PdfString(filename));
}
return action;
}
/**
* A set-OCG-state action (PDF 1.5) sets the state of one or more optional content
* groups.
* @param state an array consisting of any number of sequences beginning with a <CODE>PdfName</CODE>
* or <CODE>String</CODE> (ON, OFF, or Toggle) followed by one or more optional content group dictionaries
* <CODE>PdfLayer</CODE> or a <CODE>PdfIndirectReference</CODE> to a <CODE>PdfLayer</CODE>.<br>
* The array elements are processed from left to right; each name is applied
* to the subsequent groups until the next name is encountered:
* <ul>
* <li>ON sets the state of subsequent groups to ON</li>
* <li>OFF sets the state of subsequent groups to OFF</li>
* <li>Toggle reverses the state of subsequent groups</li>
* </ul>
* @param preserveRB if <CODE>true</CODE>, indicates that radio-button state relationships between optional
* content groups (as specified by the RBGroups entry in the current configuration
* dictionary) should be preserved when the states in the
* <CODE>state</CODE> array are applied. That is, if a group is set to ON (either by ON or Toggle) during
* processing of the <CODE>state</CODE> array, any other groups belong to the same radio-button
* group are turned OFF. If a group is set to OFF, there is no effect on other groups.<br>
* If <CODE>false</CODE>, radio-button state relationships, if any, are ignored
* @return the action
*/
public static PdfAction setOCGstate(ArrayList state, boolean preserveRB) {
PdfAction action = new PdfAction();
action.put(PdfName.S, PdfName.SETOCGSTATE);
PdfArray a = new PdfArray();
for (int k = 0; k < state.size(); ++k) {
Object o = state.get(k);
if (o == null)
continue;
if (o instanceof PdfIndirectReference)
a.add((PdfIndirectReference)o);
else if (o instanceof PdfLayer)
a.add(((PdfLayer)o).getRef());
else if (o instanceof PdfName)
a.add((PdfName)o);
else if (o instanceof String) {
PdfName name = null;
String s = (String)o;
if (s.equalsIgnoreCase("on"))
name = PdfName.ON;
else if (s.equalsIgnoreCase("off"))
name = PdfName.OFF;
else if (s.equalsIgnoreCase("toggle"))
name = PdfName.TOGGLE;
else
throw new IllegalArgumentException("A string '" + s + " was passed in state. Only 'ON', 'OFF' and 'Toggle' are allowed.");
a.add(name);
}
else
throw new IllegalArgumentException("Invalid type was passed in state: " + o.getClass().getName());
}
action.put(PdfName.STATE, a);
if (!preserveRB)
action.put(PdfName.PRESERVERB, PdfBoolean.PDFFALSE);
return action;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -