Package com.pd4ml.pdf
Class PdfDocumentBase
- java.lang.Object
-
- com.pd4ml.pdf.PdfDocumentBase
-
- Direct Known Subclasses:
PdfDocument
public abstract class PdfDocumentBase extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description com.pd4ml.pdf.parser.PdfDoc
pdf
-
Constructor Summary
Constructors Constructor Description PdfDocumentBase(byte[] bytes, java.lang.String password)
PdfDocumentBase(java.io.InputStream stream, java.lang.String password)
PdfDocumentBase(java.net.URL url, java.lang.String password)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
append(PdfDocumentBase toMerge)
static boolean[]
computeScope(java.lang.String scope, int pageNum)
Utility method converts page scope description to a boolean array of flags.int
deletePage(int pageNumber)
deletes a page of a loaded document.java.lang.String
dumpOutlines()
java.lang.String
getAuthor()
java.lang.String
getCreationDate()
java.lang.String
getCreator()
java.lang.String
getKeywords()
java.lang.String
getMetadata()
java.lang.String
getModDate()
int
getNumberOfPages()
float[]
getPageBox(int pageIndex, java.lang.String boxName)
returns PDF page boundaries array.java.lang.String
getPageContent(int pageIndex)
PageSize
getPageSize(int pageIndex)
returns page sizejava.lang.String
getPdfFormatVersion()
int
getPermissions()
java.lang.String
getProducer()
java.lang.String
getSubject()
java.lang.String
getTitle()
void
overlay(PdfDocumentBase anotherDoc, int opacity)
void
overlay(PdfDocumentBase anotherDoc, java.lang.String srcScope, java.lang.String destScope, int opacity)
void
read()
reads and parses given PDF document streamvoid
setAuthor(java.lang.String author)
void
setCreator(java.lang.String creator)
void
setKeywords(java.lang.String keywords)
void
setModDate()
sets the document modification date to "now"void
setSubject(java.lang.String subject)
void
setTitle(java.lang.String title)
void
underlay(PdfDocumentBase anotherDoc, int opacity)
void
underlay(PdfDocumentBase anotherDoc, java.lang.String srcScope, java.lang.String destScope, int opacity)
void
write(java.io.OutputStream out)
writes current PDF document state to given output stream.void
write(java.io.OutputStream out, java.lang.String password, int permissions)
writes current PDF document state to given output stream.
-
-
-
Constructor Detail
-
PdfDocumentBase
public PdfDocumentBase(byte[] bytes, java.lang.String password) throws InvokeException
- Parameters:
bytes
- PDF document as an array of bytespassword
- if any. "null" for not protected PDF documents- Throws:
InvokeException
- by invalid call parameters
-
PdfDocumentBase
public PdfDocumentBase(java.io.InputStream stream, java.lang.String password) throws InvokeException
- Parameters:
stream
- PDF document input streampassword
- if any. "null" for not protected PDF documents- Throws:
InvokeException
- by invalid call parameters
-
PdfDocumentBase
public PdfDocumentBase(java.net.URL url, java.lang.String password) throws InvokeException
- Parameters:
url
- PDF document URLpassword
- if any. "null" for not protected PDF documents- Throws:
InvokeException
- by invalid call parameters
-
-
Method Detail
-
write
public void write(java.io.OutputStream out) throws java.io.IOException, InvokeException
writes current PDF document state to given output stream.- Parameters:
out
- an output stream to write to- Throws:
java.io.IOException
- write errorsInvokeException
- if the document is already appended to another one (document objects reassigned to another root).
-
write
public void write(java.io.OutputStream out, java.lang.String password, int permissions) throws java.io.IOException, InvokeException, InvalidPasswordException
writes current PDF document state to given output stream.- Parameters:
out
- an output stream to write topassword
- password string. null if no password to be appliedpermissions
- PDF file access permissions See PD4Constants.Allow* constants. Example AllowAnnotate | AllowDegradedPrint. Use PD4Constants#DefaultPermissions for defaults.- Throws:
java.io.IOException
- write errorsInvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.InvokeException
- if the document is already appended to another one (document objects reassigned to another root).- See Also:
Constants.DefaultPermissions
-
append
public void append(PdfDocumentBase toMerge) throws java.io.IOException
- Parameters:
toMerge
- another PDF document to append to the current one. Implicitly invokesread()
if has not been triggered before.- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.InvokeException
- by an attempt to merge with more than one document
-
overlay
public void overlay(PdfDocumentBase anotherDoc, int opacity) throws java.io.IOException
- Parameters:
anotherDoc
- another PDF document whose page content is to be combined with the pages of the current document. Implicitly invokesread()
if has not been triggered before. The method applies all pages of anotherDoc to all pages of this doc.opacity
- transparency of the overlay: 0 = fully transparent, 255 (or any negative value) = opaque- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.InvokeException
- by an attempt to merge with more than one document- See Also:
computeScope(String, int)
-
overlay
public void overlay(PdfDocumentBase anotherDoc, java.lang.String srcScope, java.lang.String destScope, int opacity) throws java.io.IOException
- Parameters:
anotherDoc
- another PDF document whose page content is to be combined with the pages of the current document. Implicitly invokesread()
if has not been triggered before.srcScope
- a page range of an overlay document to use.destScope
- a page range in the current document the overlay is to apply to.opacity
- transparency of the overlay: 0 = fully transparent, 255 (or any negative value) = opaque- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.InvokeException
- by an attempt to merge with more than one document- See Also:
computeScope(String, int)
-
underlay
public void underlay(PdfDocumentBase anotherDoc, int opacity) throws java.io.IOException
- Parameters:
anotherDoc
- another PDF document whose page content is to be placed under of the current document content. Implicitly invokesread()
if has not been triggered before. The method applies all pages of anotherDoc to all pages of this doc.opacity
- transparency of the overlay: 0 = fully transparent, 255 (or any negative value) = opaque- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.InvokeException
- by an attempt to merge with more than one document- See Also:
computeScope(String, int)
-
underlay
public void underlay(PdfDocumentBase anotherDoc, java.lang.String srcScope, java.lang.String destScope, int opacity) throws java.io.IOException
- Parameters:
anotherDoc
- another PDF document whose page content is to be placed under of the current document content. Implicitly invokesread()
if has not been triggered before.srcScope
- a page range of an underlay document to use.destScope
- a page range in the current document the underlay is to apply to.opacity
- transparency of the overlay: 0 = fully transparent, 255 (or any negative value) = opaque- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.InvokeException
- by an attempt to merge with more than one document- See Also:
computeScope(String, int)
-
getNumberOfPages
public int getNumberOfPages() throws java.io.IOException, InvalidPasswordException
- Returns:
- returns a number of pages in the current PDF. Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getPermissions
public int getPermissions() throws java.io.IOException, InvalidPasswordException
- Returns:
- returns permissions flag of the document
- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getTitle
public java.lang.String getTitle() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF "Title" metadata value (if any). Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
setTitle
public void setTitle(java.lang.String title) throws java.io.IOException, InvalidPasswordException
- Parameters:
title
- new document title. Implicitly invokesread()
if has not been triggered before.- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getAuthor
public java.lang.String getAuthor() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF "Author" metadata value (if any). Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
setAuthor
public void setAuthor(java.lang.String author) throws java.io.IOException
- Parameters:
author
- a value to override document "Author" name. Implicitly invokesread()
if has not been triggered before.- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getPdfFormatVersion
public java.lang.String getPdfFormatVersion() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF format version from PDF document prefix.
- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getMetadata
public java.lang.String getMetadata() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF medatata as an XMP XML string. Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getCreationDate
public java.lang.String getCreationDate() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF creation date as a string. Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
dumpOutlines
public java.lang.String dumpOutlines() throws java.io.IOException, InvalidPasswordException
- Returns:
- dumps document outline (bookmark) structure. Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getKeywords
public java.lang.String getKeywords() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF "Keywords" metadata value (if any). Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
setKeywords
public void setKeywords(java.lang.String keywords) throws java.io.IOException, InvalidPasswordException
- Parameters:
keywords
- a value to override document "Keywords" string. Implicitly invokesread()
if has not been triggered before.- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getCreator
public java.lang.String getCreator() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF "Creator" metadata value (if any). Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
setCreator
public void setCreator(java.lang.String creator) throws java.io.IOException, InvalidPasswordException
- Parameters:
creator
- a value to override document "Creator" name. Implicitly invokesread()
if has not been triggered before.- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getProducer
public java.lang.String getProducer() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF "Producer" metadata value (if any). Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getSubject
public java.lang.String getSubject() throws java.io.IOException, InvalidPasswordException
- Returns:
- PDF "Subject" metadata value (if any). Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
setSubject
public void setSubject(java.lang.String subject) throws java.io.IOException, InvalidPasswordException
- Parameters:
subject
- a value to override document "Subject" string. Implicitly invokesread()
if has not been triggered before.- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
setModDate
public void setModDate() throws java.io.IOException, InvalidPasswordException
sets the document modification date to "now"- Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getModDate
public java.lang.String getModDate() throws java.io.IOException, InvalidPasswordException
- Returns:
- Document modification date. Implicitly invokes
read()
if has not been triggered before. - Throws:
java.io.IOException
- reflects a variety of PDF read/parse errors byread()
InvalidPasswordException
- invalid password given to the object constructor, i.e.PdfDocumentBase(byte[], String)
.
-
getPageSize
public PageSize getPageSize(int pageIndex) throws InvalidPasswordException, java.io.IOException
returns page size- Parameters:
pageIndex
- page number (starting from 1)- Returns:
- page size object
- Throws:
InvalidPasswordException
- thrown for encrypted documents in a case given password does not matchjava.io.IOException
- general document read exception
-
getPageBox
public float[] getPageBox(int pageIndex, java.lang.String boxName) throws InvalidPasswordException, java.io.IOException
returns PDF page boundaries array.- Parameters:
boxName
- "ArtBox", "BleedBox", "TrimBox" – used for printing; "MediaBox" – target paper size, i.e., finished size of printed document; "CropBox" – page visible in the viewer, i.e., what the user sees on the screen; "BBox" – the bounds of the actual content on the pagepageIndex
- page number (starting from 1)- Returns:
- array of four numbers
- Throws:
InvalidPasswordException
- thrown for encrypted documents in a case given password does not matchjava.io.IOException
- general document read exception
-
read
public void read() throws java.io.IOException, InvokeException, InvalidPasswordException
reads and parses given PDF document stream- Throws:
java.io.IOException
- read/parsing errorsInvokeException
- PDF parsing specific errorsInvalidPasswordException
- PDF encryption password do not match
-
getPageContent
public java.lang.String getPageContent(int pageIndex) throws InvalidPasswordException, java.io.IOException
- Parameters:
pageIndex
- page number (0 is the first page)- Returns:
- page content stream
- Throws:
java.io.IOException
- read/parsing errorsInvalidPasswordException
- PDF encryption password do not match
-
deletePage
public int deletePage(int pageNumber) throws InvalidPasswordException, java.io.IOException
deletes a page of a loaded document. If the document has only one page or not existent page number is given, the method call takes no effect.- Parameters:
pageNumber
- index of page to delete (first page is 1)- Returns:
- number of pages remain in the doc.
- Throws:
java.io.IOException
- read/parsing errorsInvalidPasswordException
- PDF encryption password do not match
-
computeScope
public static boolean[] computeScope(java.lang.String scope, int pageNum)
Utility method converts page scope description to a boolean array of flags. "true" = page number matches the scope, "false" = does not. Note: in scope definition the first page is "1"; it corresponds to '0" index in the returned array.- Parameters:
scope
- "null" = all pages. Allowed syntax: "1", "3-5", "2+", "last", "even", "odd", "1+,skiplast", "5+,even"pageNum
- number of pages- Returns:
- an array of scope match flags. Each array item corresponds a page.
-
-