📄 pdfwriter.java
字号:
public static final int FitWindow = 1 << 15;
/** A viewer preference */
public static final int CenterWindow = 1 << 16;
/** A viewer preference */
public static final int DisplayDocTitle = 1 << 17;
/** A viewer preference */
public static final int NonFullScreenPageModeUseNone = 1 << 18;
/** A viewer preference */
public static final int NonFullScreenPageModeUseOutlines = 1 << 19;
/** A viewer preference */
public static final int NonFullScreenPageModeUseThumbs = 1 << 20;
/** A viewer preference */
public static final int NonFullScreenPageModeUseOC = 1 << 21;
/** A viewer preference */
public static final int DirectionL2R = 1 << 22;
/** A viewer preference */
public static final int DirectionR2L = 1 << 23;
/** A viewer preference */
public static final int PrintScalingNone = 1 << 24;
/** @see com.lowagie.text.pdf.interfaces.PdfViewerPreferences#setViewerPreferences(int) */
public void setViewerPreferences(int preferences) {
pdf.setViewerPreferences(preferences);
}
/** @see com.lowagie.text.pdf.interfaces.PdfViewerPreferences#addViewerPreference(com.lowagie.text.pdf.PdfName, com.lowagie.text.pdf.PdfObject) */
public void addViewerPreference(PdfName key, PdfObject value) {
pdf.addViewerPreference(key, value);
}
// [C4] Page labels
/**
* Use this method to add page labels
* @param pageLabels the page labels
*/
public void setPageLabels(PdfPageLabels pageLabels) {
pdf.setPageLabels(pageLabels);
}
// [C5] named objects: named destinations, javascript, embedded files
/**
* Use this method to add a JavaScript action at the document level.
* When the document opens, all this JavaScript runs.
* @param js The JavaScrip action
*/
public void addJavaScript(PdfAction js) {
pdf.addJavaScript(js);
}
/**
* Use this method to add a JavaScript action at the document level.
* When the document opens, all this JavaScript runs.
* @param code the JavaScript code
* @param unicode select JavaScript unicode. Note that the internal
* Acrobat JavaScript engine does not support unicode,
* so this may or may not work for you
*/
public void addJavaScript(String code, boolean unicode) {
addJavaScript(PdfAction.javaScript(code, this, unicode));
}
/**
* Use this method to adds a JavaScript action at the document level.
* When the document opens, all this JavaScript runs.
* @param code the JavaScript code
*/
public void addJavaScript(String code) {
addJavaScript(code, false);
}
/**
* Use this method to add a file attachment at the document level.
* @param description 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
*/
public void addFileAttachment(String description, byte fileStore[], String file, String fileDisplay) throws IOException {
addFileAttachment(description, PdfFileSpecification.fileEmbedded(this, file, fileDisplay, fileStore));
}
/**
* Use this method to add a file attachment at the document level.
* @param description the file description
* @param fs the file specification
*/
public void addFileAttachment(String description, PdfFileSpecification fs) throws IOException {
pdf.addFileAttachment(description, fs);
}
/**
* Use this method to add a file attachment at the document level.
* @param fs the file specification
*/
public void addFileAttachment(PdfFileSpecification fs) throws IOException {
addFileAttachment(null, fs);
}
// [C6] Actions (open and additional)
/** action value */
public static final PdfName DOCUMENT_CLOSE = PdfName.WC;
/** action value */
public static final PdfName WILL_SAVE = PdfName.WS;
/** action value */
public static final PdfName DID_SAVE = PdfName.DS;
/** action value */
public static final PdfName WILL_PRINT = PdfName.WP;
/** action value */
public static final PdfName DID_PRINT = PdfName.DP;
/** @see com.lowagie.text.pdf.interfaces.PdfDocumentActions#setOpenAction(java.lang.String) */
public void setOpenAction(String name) {
pdf.setOpenAction(name);
}
/** @see com.lowagie.text.pdf.interfaces.PdfDocumentActions#setOpenAction(com.lowagie.text.pdf.PdfAction) */
public void setOpenAction(PdfAction action) {
pdf.setOpenAction(action);
}
/** @see com.lowagie.text.pdf.interfaces.PdfDocumentActions#setAdditionalAction(com.lowagie.text.pdf.PdfName, com.lowagie.text.pdf.PdfAction) */
public void setAdditionalAction(PdfName actionType, PdfAction action) throws DocumentException {
if (!(actionType.equals(DOCUMENT_CLOSE) ||
actionType.equals(WILL_SAVE) ||
actionType.equals(DID_SAVE) ||
actionType.equals(WILL_PRINT) ||
actionType.equals(DID_PRINT))) {
throw new DocumentException("Invalid additional action type: " + actionType.toString());
}
pdf.addAdditionalAction(actionType, action);
}
// [C7] portable collections
/**
* Use this method to add the Collection dictionary.
* @param collection a dictionary of type PdfCollection
*/
public void setCollection(PdfCollection collection) {
setAtLeastPdfVersion(VERSION_1_7);
pdf.setCollection(collection);
}
// [C8] AcroForm
/** signature value */
public static final int SIGNATURE_EXISTS = 1;
/** signature value */
public static final int SIGNATURE_APPEND_ONLY = 2;
/** @see com.lowagie.text.pdf.interfaces.PdfAnnotations#getAcroForm() */
public PdfAcroForm getAcroForm() {
return pdf.getAcroForm();
}
/** @see com.lowagie.text.pdf.interfaces.PdfAnnotations#addAnnotation(com.lowagie.text.pdf.PdfAnnotation) */
public void addAnnotation(PdfAnnotation annot) {
pdf.addAnnotation(annot);
}
void addAnnotation(PdfAnnotation annot, int page) {
addAnnotation(annot);
}
/** @see com.lowagie.text.pdf.interfaces.PdfAnnotations#addCalculationOrder(com.lowagie.text.pdf.PdfFormField) */
public void addCalculationOrder(PdfFormField annot) {
pdf.addCalculationOrder(annot);
}
/** @see com.lowagie.text.pdf.interfaces.PdfAnnotations#setSigFlags(int) */
public void setSigFlags(int f) {
pdf.setSigFlags(f);
}
// [C9] Metadata
/** XMP Metadata for the document. */
protected byte[] xmpMetadata = null;
/**
* Use this method to set the XMP Metadata.
* @param xmpMetadata The xmpMetadata to set.
*/
public void setXmpMetadata(byte[] xmpMetadata) {
this.xmpMetadata = xmpMetadata;
}
/**
* Use this method to creates XMP Metadata based
* on the metadata in the PdfDocument.
*/
public void createXmpMetadata() {
setXmpMetadata(createXmpMetadataBytes());
}
/**
* @return an XmpMetadata byte array
*/
private byte[] createXmpMetadataBytes() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
XmpWriter xmp = new XmpWriter(baos, pdf.getInfo());
xmp.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
return baos.toByteArray();
}
// [C10] PDFX Conformance
/** A PDF/X level. */
public static final int PDFXNONE = 0;
/** A PDF/X level. */
public static final int PDFX1A2001 = 1;
/** A PDF/X level. */
public static final int PDFX32002 = 2;
/** Stores the PDF/X level. */
private PdfXConformanceImp pdfxConformance = new PdfXConformanceImp();
/** @see com.lowagie.text.pdf.interfaces.PdfXConformance#setPDFXConformance(int) */
public void setPDFXConformance(int pdfx) {
if (pdfxConformance.getPDFXConformance() == pdfx)
return;
if (pdf.isOpen())
throw new PdfXConformanceException("PDFX conformance can only be set before opening the document.");
if (crypto != null)
throw new PdfXConformanceException("A PDFX conforming document cannot be encrypted.");
if (pdfx!= PDFXNONE)
setPdfVersion(VERSION_1_3);
pdfxConformance.setPDFXConformance(pdfx);
}
/** @see com.lowagie.text.pdf.interfaces.PdfXConformance#getPDFXConformance() */
public int getPDFXConformance() {
return pdfxConformance.getPDFXConformance();
}
/** @see com.lowagie.text.pdf.interfaces.PdfXConformance#isPdfX() */
public boolean isPdfX() {
return pdfxConformance.isPdfX();
}
// [C11] Output intents
/**
* Use this method to set the values of the output intent dictionary.
* Null values are allowed to suppress any key.
* @param outputConditionIdentifier a value
* @param outputCondition a value
* @param registryName a value
* @param info a value
* @param destOutputProfile a value
* @throws IOException on error
*/
public void setOutputIntents(String outputConditionIdentifier, String outputCondition, String registryName, String info, byte destOutputProfile[]) throws IOException {
getExtraCatalog();
PdfDictionary out = new PdfDictionary(PdfName.OUTPUTINTENT);
if (outputCondition != null)
out.put(PdfName.OUTPUTCONDITION, new PdfString(outputCondition, PdfObject.TEXT_UNICODE));
if (outputConditionIdentifier != null)
out.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString(outputConditionIdentifier, PdfObject.TEXT_UNICODE));
if (registryName != null)
out.put(PdfName.REGISTRYNAME, new PdfString(registryName, PdfObject.TEXT_UNICODE));
if (info != null)
out.put(PdfName.INFO, new PdfString(info, PdfObject.TEXT_UNICODE));
if (destOutputProfile != null) {
PdfStream stream = new PdfStream(destOutputProfile);
stream.flateCompress();
out.put(PdfName.DESTOUTPUTPROFILE, addToBody(stream).getIndirectReference());
}
out.put(PdfName.S, PdfName.GTS_PDFX);
extraCatalog.put(PdfName.OUTPUTINTENTS, new PdfArray(out));
}
/**
* Use this method to copy the output intent dictionary
* from another document to this one.
* @param reader the other document
* @param checkExistence <CODE>true</CODE> to just check for the existence of a valid output intent
* dictionary, <CODE>false</CODE> to insert the dictionary if it exists
* @throws IOException on error
* @return <CODE>true</CODE> if the output intent dictionary exists, <CODE>false</CODE>
* otherwise
*/
public boolean setOutputIntents(PdfReader reader, boolean checkExistence) throws IOException {
PdfDictionary catalog = reader.getCatalog();
PdfArray outs = (PdfArray)PdfReader.getPdfObject(catalog.get(PdfName.OUTPUTINTENTS));
if (outs == null)
return false;
ArrayList arr = outs.getArrayList();
if (arr.isEmpty())
return false;
PdfDictionary out = (PdfDictionary)PdfReader.getPdfObject((PdfObject)arr.get(0));
PdfObject obj = PdfReader.getPdfObject(out.get(PdfName.S));
if (obj == null || !PdfName.GTS_PDFX.equals(obj))
return false;
if (checkExistence)
return true;
PRStream stream = (PRStream)PdfReader.getPdfObject(out.get(PdfName.DESTOUTPUTPROFILE));
byte destProfile[] = null;
if (stream != null) {
destProfile = PdfReader.getStreamBytes(stream);
}
setOutputIntents(getNameString(out, PdfName.OUTPUTCONDITIONIDENTIFIER), getNameString(out, PdfName.OUTPUTCONDITION),
getNameString(out, PdfName.REGISTRYNAME), getNameString(out, PdfName.INFO), destProfile);
return true;
}
private static String getNameString(PdfDictionary dic, PdfName key) {
PdfObject obj = PdfReader.getPdfObject(dic.get(key));
if (obj == null || !obj.isString())
return null;
return ((PdfString)obj).toUnicodeString();
}
// PDF Objects that have an impact on the PDF body
// [F1] PdfEncryptionSettings interface
// types of encryption
/** Type of encryption */
public static final int STANDARD_ENCRYPTION_40 = 0;
/** Type of encryption */
public static final int STANDARD_ENCRYPTION_128 = 1;
/** Type of encryption */
public static final int ENCRYPTION_AES_128 = 2;
/** Mask to separate the encryption type from the encryption mode. */
static final int ENCRYPTION_MASK = 7;
/** Add this to the mode to keep the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -