mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-09 19:29:34 +08:00
Merge branch 'master' into creator-tablecell-div-image-support
This commit is contained in:
commit
ede3df6446
@ -15,6 +15,8 @@ import (
|
||||
. "github.com/unidoc/unidoc/pdf/core"
|
||||
)
|
||||
|
||||
// PdfReader represents a PDF file reader. It is a frontend to the lower level parsing mechanism and provides
|
||||
// a higher level access to work with PDF structure and information, such as the page structure etc.
|
||||
type PdfReader struct {
|
||||
parser *PdfParser
|
||||
root PdfObject
|
||||
@ -32,6 +34,9 @@ type PdfReader struct {
|
||||
traversed map[PdfObject]bool
|
||||
}
|
||||
|
||||
// NewPdfReader returns a new PdfReader for an input io.ReadSeeker interface. Can be used to read PDF from
|
||||
// memory or file. Immediately loads and traverses the PDF structure including pages and page contents (if
|
||||
// not encrypted).
|
||||
func NewPdfReader(rs io.ReadSeeker) (*PdfReader, error) {
|
||||
pdfReader := &PdfReader{}
|
||||
pdfReader.traversed = map[PdfObject]bool{}
|
||||
@ -61,13 +66,13 @@ func NewPdfReader(rs io.ReadSeeker) (*PdfReader, error) {
|
||||
return pdfReader, nil
|
||||
}
|
||||
|
||||
// IsEncrypted returns true if the PDF file is encrypted.
|
||||
func (this *PdfReader) IsEncrypted() (bool, error) {
|
||||
return this.parser.IsEncrypted()
|
||||
}
|
||||
|
||||
// Returns a string containing some information about the encryption method used.
|
||||
// Subject to changes. May be better to return a standardized struct with information.
|
||||
// But challenging due to the many different types supported.
|
||||
// GetEncryptionMethod returns a string containing some information about the encryption method used.
|
||||
// XXX/TODO: May be better to return a standardized struct with information.
|
||||
func (this *PdfReader) GetEncryptionMethod() string {
|
||||
crypter := this.parser.GetCrypter()
|
||||
str := crypter.Filter + " - "
|
||||
@ -95,7 +100,7 @@ func (this *PdfReader) GetEncryptionMethod() string {
|
||||
return str
|
||||
}
|
||||
|
||||
// Decrypt the PDF file with a specified password. Also tries to
|
||||
// Decrypt decrypts the PDF file with a specified password. Also tries to
|
||||
// decrypt with an empty password. Returns true if successful,
|
||||
// false otherwise.
|
||||
func (this *PdfReader) Decrypt(password []byte) (bool, error) {
|
||||
@ -116,8 +121,9 @@ func (this *PdfReader) Decrypt(password []byte) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Check access rights and permissions for a specified password. If either user/owner password is specified,
|
||||
// full rights are granted, otherwise the access rights are specified by the Permissions flag.
|
||||
// CheckAccessRights checks access rights and permissions for a specified password. If either user/owner
|
||||
// password is specified, full rights are granted, otherwise the access rights are specified by the
|
||||
// Permissions flag.
|
||||
//
|
||||
// The bool flag indicates that the user can access and view the file.
|
||||
// The AccessPermissions shows what access the user has for editing etc.
|
||||
@ -218,7 +224,6 @@ func (this *PdfReader) loadStructure() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// Trace to object. Keeps a list of already visited references to avoid circular references.
|
||||
//
|
||||
// Example circular reference.
|
||||
@ -402,12 +407,12 @@ func (this *PdfReader) buildOutlineTree(obj PdfObject, parent *PdfOutlineTreeNod
|
||||
}
|
||||
}
|
||||
|
||||
// Get the outline tree.
|
||||
// GetOutlineTree returns the outline tree.
|
||||
func (this *PdfReader) GetOutlineTree() *PdfOutlineTreeNode {
|
||||
return this.outlineTree
|
||||
}
|
||||
|
||||
// Return a flattened list of tree nodes and titles.
|
||||
// GetOutlinesFlattened returns a flattened list of tree nodes and titles.
|
||||
func (this *PdfReader) GetOutlinesFlattened() ([]*PdfOutlineTreeNode, []string, error) {
|
||||
outlineNodeList := []*PdfOutlineTreeNode{}
|
||||
flattenedTitleList := []string{}
|
||||
@ -442,6 +447,7 @@ func (this *PdfReader) GetOutlinesFlattened() ([]*PdfOutlineTreeNode, []string,
|
||||
return outlineNodeList, flattenedTitleList, nil
|
||||
}
|
||||
|
||||
// loadForms loads the AcroForm.
|
||||
func (this *PdfReader) loadForms() (*PdfAcroForm, error) {
|
||||
if this.parser.GetCrypter() != nil && !this.parser.IsAuthenticated() {
|
||||
return nil, fmt.Errorf("File need to be decrypted first")
|
||||
@ -588,7 +594,7 @@ func (this *PdfReader) buildPageList(node *PdfIndirectObject, parent *PdfIndirec
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get the number of pages in the document.
|
||||
// GetNumPages returns the number of pages in the document.
|
||||
func (this *PdfReader) GetNumPages() (int, error) {
|
||||
if this.parser.GetCrypter() != nil && !this.parser.IsAuthenticated() {
|
||||
return 0, fmt.Errorf("File need to be decrypted first")
|
||||
@ -694,7 +700,7 @@ func (this *PdfReader) traverseObjectData(o PdfObject) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get a page by the page number. Indirect object with type /Page.
|
||||
// GetPageAsIndirectObject returns an indirect object containing the page dictionary for a specified page number.
|
||||
func (this *PdfReader) GetPageAsIndirectObject(pageNumber int) (PdfObject, error) {
|
||||
if this.parser.GetCrypter() != nil && !this.parser.IsAuthenticated() {
|
||||
return nil, fmt.Errorf("File needs to be decrypted first")
|
||||
@ -715,8 +721,7 @@ func (this *PdfReader) GetPageAsIndirectObject(pageNumber int) (PdfObject, error
|
||||
return page, nil
|
||||
}
|
||||
|
||||
// Get a page by the page number.
|
||||
// Returns the PdfPage entry.
|
||||
// GetPage returns the PdfPage model for the specified page number.
|
||||
func (this *PdfReader) GetPage(pageNumber int) (*PdfPage, error) {
|
||||
if this.parser.GetCrypter() != nil && !this.parser.IsAuthenticated() {
|
||||
return nil, fmt.Errorf("File needs to be decrypted first")
|
||||
@ -733,7 +738,7 @@ func (this *PdfReader) GetPage(pageNumber int) (*PdfPage, error) {
|
||||
return page, nil
|
||||
}
|
||||
|
||||
// Get optional content properties
|
||||
// GetOCProperties returns the optional content properties PdfObject.
|
||||
func (this *PdfReader) GetOCProperties() (PdfObject, error) {
|
||||
dict := this.catalog
|
||||
obj := dict.Get("OCProperties")
|
||||
@ -755,7 +760,8 @@ func (this *PdfReader) GetOCProperties() (PdfObject, error) {
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
// Inspect the object types, subtypes and content in the PDF file.
|
||||
// Inspect inspects the object types, subtypes and content in the PDF file returning a map of
|
||||
// object type to number of instances of each.
|
||||
func (this *PdfReader) Inspect() (map[string]int, error) {
|
||||
return this.parser.Inspect()
|
||||
}
|
||||
@ -769,12 +775,13 @@ func (r *PdfReader) GetObjectNums() []int {
|
||||
return r.parser.GetObjectNums()
|
||||
}
|
||||
|
||||
// Get specific object number.
|
||||
// GetIndirectObjectByNumber retrieves and returns a specific PdfObject by object number.
|
||||
func (this *PdfReader) GetIndirectObjectByNumber(number int) (PdfObject, error) {
|
||||
obj, err := this.parser.LookupByNumber(number)
|
||||
return obj, err
|
||||
}
|
||||
|
||||
// GetTrailer returns the PDF's trailer dictionary.
|
||||
func (this *PdfReader) GetTrailer() (*PdfObjectDictionary, error) {
|
||||
trailerDict := this.parser.GetTrailer()
|
||||
if trailerDict == nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user