📄 pdfwriter.java
字号:
int size() {
return body.size();
}
/**
* Sometimes it is necessary to know where the just added <CODE>Table</CODE> ends,
* e.g. to avoid to add another table in a page that is ending up, because
* the new table will be probably splitted just after the header (it is an
* unpleasant effect, isn't it?).
*
* Added on September 8th, 2001
* by Francesco De Milato
* francesco.demilato@tiscalinet.it
*
* @return the bottom height of the just added table
* @author francesco.demilato@tiscalinet.it
*/
public float getTableBottom(Table table)
{
return pdf.bottom(table) - pdf.indentBottom();
}
/**
* Checks if a <CODE>Table</CODE> fits the current page of the <CODE>PdfDocument</CODE>.
*
* @param table the table that has to be checked
* @param margin a certain margin
* @return <CODE>true</CODE> if the <CODE>Table</CODE> fits the page, <CODE>false</CODE> otherwise.
*/
public boolean fitsPage(Table table, float margin) {
return pdf.bottom(table) > pdf.indentBottom() + margin;
}
/**
* Checks if a <CODE>Table</CODE> fits the current page of the <CODE>PdfDocument</CODE>.
*
* @param table the table that has to be checked
* @return <CODE>true</CODE> if the <CODE>Table</CODE> fits the page, <CODE>false</CODE> otherwise.
*/
public boolean fitsPage(Table table) {
return fitsPage(table, 0);
}
/**
* Checks if a <CODE>PdfPTable</CODE> fits the current page of the <CODE>PdfDocument</CODE>.
*
* @param table the table that has to be checked
* @param margin a certain margin
* @return <CODE>true</CODE> if the <CODE>PdfPTable</CODE> fits the page, <CODE>false</CODE> otherwise.
*/
public boolean fitsPage(PdfPTable table, float margin) {
return pdf.fitsPage(table, margin);
}
/**
* Checks if a <CODE>PdfPTable</CODE> fits the current page of the <CODE>PdfDocument</CODE>.
*
* @param table the table that has to be checked
* @return <CODE>true</CODE> if the <CODE>PdfPTable</CODE> fits the page, <CODE>false</CODE> otherwise.
*/
public boolean fitsPage(PdfPTable table) {
return pdf.fitsPage(table, 0);
}
/**
* Checks if writing is paused.
*
* @return <CODE>true</CODE> if writing temporarely has to be paused, <CODE>false</CODE> otherwise.
*/
boolean isPaused() {
return pause;
}
/**
* Gets the direct content for this document. There is only one direct content,
* multiple calls to this method will allways retrieve the same.
* @return the direct content
*/
public PdfContentByte getDirectContent() {
return directContent;
}
/**
* Gets the direct content under for this document. There is only one direct content,
* multiple calls to this method will allways retrieve the same.
* @return the direct content
*/
public PdfContentByte getDirectContentUnder() {
return directContentUnder;
}
/**
* Resets all the direct contents to empty. This happens when a new page is started.
*/
void resetContent() {
directContent.reset();
directContentUnder.reset();
}
/** Gets the root outline.
* @return the root outline
*/
public PdfOutline getRootOutline() {
return directContent.getRootOutline();
}
/**
* Adds a <CODE>BaseFont</CODE> to the document and to the page resources.
* @param bf the <CODE>BaseFont</CODE> to add
* @return the name of the font in the document
*/
FontDetails add(BaseFont bf) {
FontDetails ret = (FontDetails)addSimple(bf);
pdf.addFont(ret.getFontName(), ret.getIndirectReference());
return ret;
}
/**
* Adds a <CODE>BaseFont</CODE> to the document but not to the page resources.
* It is used for templates.
* @param bf the <CODE>BaseFont</CODE> to add
* @return an <CODE>Object[]</CODE> where position 0 is a <CODE>PdfName</CODE>
* and position 1 is an <CODE>PdfIndirectReference</CODE>
*/
FontDetails addSimple(BaseFont bf) {
FontDetails ret = (FontDetails)documentFonts.get(bf);
if (ret == null) {
try {
ret = new FontDetails(new PdfName("F" + (fontNumber++)), body.getPdfIndirectReference(), bf);
}
catch (BadPdfFormatException e) {
}
documentFonts.put(bf, ret);
}
return ret;
}
/**
* Gets the <CODE>PdfDocument</CODE> associated with this writer.
* @return the <CODE>PdfDocument</CODE>
*/
PdfDocument getPdfDocument() {
return pdf;
}
/**
* Gets a <CODE>PdfIndirectReference</CODE> for an object that
* will be created in the future.
* @return the <CODE>PdfIndirectReference</CODE>
*/
PdfIndirectReference getPdfIndirectReference() {
return body.getPdfIndirectReference();
}
/**
* Adds a template to the document but not to the page resources.
* @param template the template to add
* @return the <CODE>PdfName</CODE> for this template
*/
PdfName addDirectTemplateSimple(PdfTemplate template) {
PdfName name = (PdfName)formXObjects.get(template);
try {
if (name == null) {
name = new PdfName("Xf" + formXObjectsCounter);
++formXObjectsCounter;
formXObjects.put(template, name);
}
}
catch (Exception e) {}
return name;
}
/**
* Sets the <CODE>PdfPageEvent</CODE> for this document.
* @param pageEvent the <CODE>PdfPageEvent</CODE> for this document
*/
public void setPageEvent(PdfPageEvent pageEvent) {
this.pageEvent = pageEvent;
}
/**
* Gets the <CODE>PdfPageEvent</CODE> for this document or <CODE>null</CODE>
* if none is set.
* @return the <CODE>PdfPageEvent</CODE> for this document or <CODE>null</CODE>
* if none is set
*/
public PdfPageEvent getPageEvent() {
return pageEvent;
}
/**
* Adds the local destinations to the body of the document.
* @param dest the <CODE>HashMap</CODE> containing the destinations
* @throws IOException on error
*/
void addLocalDestinations(TreeMap dest) throws IOException {
for (Iterator i = dest.keySet().iterator(); i.hasNext();) {
String name = (String)i.next();
Object obj[] = (Object[])dest.get(name);
PdfDestination destination = (PdfDestination)obj[2];
if (destination == null)
throw new RuntimeException("The name '" + name + "' has no local destination.");
if (obj[1] == null)
obj[1] = getPdfIndirectReference();
PdfIndirectObject iob = body.add(destination, (PdfIndirectReference)obj[1]);
iob.writeTo(os);
}
}
/**
* Gets the current pagenumber of this document.
*
* @return a page number
*/
public int getPageNumber() {
return pdf.getPageNumber();
}
/**
* Sets the viewer preferences by ORing some of these constants:<br>
* <ul>
* <li>The page layout to be used when the document is opened (choose one).
* <ul>
* <li><b>PageLayoutSinglePage</b> - Display one page at a time.
* <li><b>PageLayoutOneColumn</b> - Display the pages in one column.
* <li><b>PageLayoutTwoColumnLeft</b> - Display the pages in two columns, with
* oddnumbered pages on the left.
* <li><b>PageLayoutTwoColumnRight</b> - Display the pages in two columns, with
* oddnumbered pages on the right.
* </ul>
* <li>The page mode how the document should be displayed
* when opened (choose one).
* <ul>
* <li><b>PageModeUseNone</b> - Neither document outline nor thumbnail images visible.
* <li><b>PageModeUseOutlines</b> - Document outline visible.
* <li><b>PageModeUseThumbs</b> - Thumbnail images visible.
* <li><b>PageModeFullScreen</b> - Full-screen mode, with no menu bar, window
* controls, or any other window visible.
* </ul>
* <li><b>HideToolbar</b> - A flag specifying whether to hide the viewer application's tool
* bars when the document is active.
* <li><b>HideMenubar</b> - A flag specifying whether to hide the viewer application's
* menu bar when the document is active.
* <li><b>HideWindowUI</b> - A flag specifying whether to hide user interface elements in
* the document's window (such as scroll bars and navigation controls),
* leaving only the document's contents displayed.
* <li><b>FitWindow</b> - A flag specifying whether to resize the document's window to
* fit the size of the first displayed page.
* <li><b>CenterWindow</b> - A flag specifying whether to position the document's window
* in the center of the screen.
* <li>The document's page mode, specifying how to display the
* document on exiting full-screen mode. It is meaningful only
* if the page mode is <b>PageModeFullScreen</b> (choose one).
* <ul>
* <li><b>NonFullScreenPageModeUseNone</b> - Neither document outline nor thumbnail images
* visible
* <li><b>NonFullScreenPageModeUseOutlines</b> - Document outline visible
* <li><b>NonFullScreenPageModeUseThumbs</b> - Thumbnail images visible
* </ul>
* </ul>
* @param preferences the viewer preferences
*/
public void setViewerPreferences(int preferences) {
pdf.setViewerPreferences(preferences);
}
/** Sets the encryption options for this document. The userPassword and the
* ownerPassword can be null or have zero length. In this case the ownerPassword
* is replaced by a random string. The open permissions for the document can be
* AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
* AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
* The permissions can be combined by ORing them.
* @param userPassword the user password. Can be null or empty
* @param ownerPassword the owner password. Can be null or empty
* @param permissions the user permissions
* @param strength128Bits true for 128 bit key length. false for 40 bit key length
* @throws DocumentException if the document is already open
*/
public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException {
if (pdf.isOpen())
throw new DocumentException("Encryption can only be added before opening the document.");
crypto = new PdfEncryption();
crypto.setupAllKeys(userPassword, ownerPassword, permissions, strength128Bits);
body.setCrypto(crypto);
}
/**
* Sets the encryption options for this document. The userPassword and the
* ownerPassword can be null or have zero length. In this case the ownerPassword
* is replaced by a random string. The open permissions for the document can be
* AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
* AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
* The permissions can be combined by ORing them.
* @param strength true for 128 bit key length. false for 40 bit key length
* @param userPassword the user password. Can be null or empty
* @param ownerPassword the owner password. Can be null or empty
* @param permissions the user permissions
* @throws DocumentException if the document is already open
*/
public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException {
setEncryption(getISOBytes(userPassword), getISOBytes(ownerPassword), permissions, strength);
}
PdfIndirectObject addToBody(PdfObject object) throws IOException {
PdfIndirectObject iobj = body.add(object);
iobj.writeTo(os);
return iobj;
}
PdfIndirectObject addToBody(PdfObject object, PdfIndirectReference ref) throws IOException {
PdfIndirectObject iobj = body.add(object, ref);
iobj.writeTo(os);
return iobj;
}
/** When the document opens it will jump to the destination with
* this name.
* @param name the name of the destination to jump to
*/
public void setOpenAction(String name) {
pdf.setOpenAction(name);
}
/** When the document opens this <CODE>action</CODE> will be
* invoked.
* @param action the action to be invoked
*/
public void setOpenAction(PdfAction action) {
pdf.setOpenAction(action);
}
/** Sets the page labels
* @param pageLabels the page labels
*/
public void setPageLabels(PdfPageLabels pageLabels) {
pdf.setPageLabels(pageLabels);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -