pdfmm 0.9.20
Public Member Functions | List of all members
mm::PdfMemDocument Class Reference

#include <PdfMemDocument.h>

Inheritance diagram for mm::PdfMemDocument:
mm::PdfDocument

Public Member Functions

 PdfMemDocument (bool empty=false)
 
 PdfMemDocument (const PdfMemDocument &rhs)
 
void Load (const std::string_view &filename, const std::string_view &password={ })
 
void LoadFromBuffer (const std::string_view &buffer, const std::string_view &password={ })
 
void LoadFromDevice (const std::shared_ptr< PdfInputDevice > &device, const std::string_view &password={ })
 
void Write (const std::string_view &filename, PdfSaveOptions opts=PdfSaveOptions::None)
 
void Write (PdfOutputDevice &device, PdfSaveOptions opts=PdfSaveOptions::None)
 
void WriteUpdate (const std::string_view &filename, PdfSaveOptions opts=PdfSaveOptions::None)
 
void WriteUpdate (PdfOutputDevice &device, PdfSaveOptions opts=PdfSaveOptions::None)
 
void SetPdfVersion (PdfVersion version)
 
PdfVersion GetPdfVersion () const override
 
void AddPdfExtension (const PdfName &ns, int64_t level)
 
bool HasPdfExtension (const PdfName &ns, int64_t level) const
 
void RemovePdfExtension (const PdfName &ns, int64_t level)
 
std::vector< PdfExtensionGetPdfExtensions () const
 
void SetEncrypted (const std::string &userPassword, const std::string &ownerPassword, PdfPermissions protection=PdfPermissions::Default, PdfEncryptAlgorithm algorithm=PdfEncryptAlgorithm::AESV2, PdfKeyLength keyLength=PdfKeyLength::L40)
 
void SetEncrypted (const PdfEncrypt &encrypt)
 
bool IsEncrypted () const
 
bool IsLinearized () const override
 
const PdfMemDocumentInsertPages (const PdfMemDocument &doc, unsigned atIndex, unsigned pageCount)
 
bool IsPrintAllowed () const override
 
bool IsEditAllowed () const override
 
bool IsCopyAllowed () const override
 
bool IsEditNotesAllowed () const override
 
bool IsFillAndSignAllowed () const override
 
bool IsAccessibilityAllowed () const override
 
bool IsDocAssemblyAllowed () const override
 
bool IsHighPrintAllowed () const override
 
void FreeObjectMemory (const PdfReference &ref, bool force=false)
 
void FreeObjectMemory (PdfObject *obj, bool force=false)
 
const PdfEncryptGetEncrypt () const
 
- Public Member Functions inherited from mm::PdfDocument
virtual ~PdfDocument ()
 
virtual PdfVersion GetPdfVersion () const =0
 
virtual bool IsLinearized () const =0
 
PdfOutlinesGetOrCreateOutlines ()
 
PdfNameTree & GetOrCreateNameTree ()
 
PdfAcroForm & GetOrCreateAcroForm (PdfAcroFormDefaulAppearance eDefaultAppearance=PdfAcroFormDefaulAppearance::BlackText12pt)
 
void EmbedSubsetFonts ()
 
const PdfDocumentAppend (const PdfDocument &doc, bool appendAll=true)
 
const PdfDocumentInsertExistingPageAt (const PdfDocument &doc, unsigned pageIndex, unsigned atIndex)
 
PdfRect FillXObjectFromDocumentPage (PdfXObject &xobj, const PdfDocument &doc, unsigned pageIndex, bool useTrimBox)
 
PdfRect FillXObjectFromExistingPage (PdfXObject &xobj, unsigned pageIndex, bool useTrimBox)
 
PdfRect FillXObjectFromPage (PdfXObject &xobj, const PdfPage &page, bool useTrimBox, unsigned difference)
 
void AttachFile (const PdfFileSpec &fileSpec)
 
PdfFileSpecGetAttachment (const PdfString &name)
 
void AddNamedDestination (const PdfDestination &dest, const PdfString &name)
 
void SetPageMode (PdfPageMode inMode)
 
PdfPageMode GetPageMode () const
 
void SetUseFullScreen ()
 
void SetPageLayout (PdfPageLayout inLayout)
 
void SetHideToolbar ()
 
void SetHideMenubar ()
 
void SetHideWindowUI ()
 
void SetFitWindow ()
 
void SetCenterWindow ()
 
void SetDisplayDocTitle ()
 
void SetPrintScaling (const PdfName &scalingType)
 
void SetBaseURI (const std::string_view &baseURI)
 
void SetLanguage (const std::string_view &language)
 
void SetBindingDirection (const PdfName &direction)
 
virtual bool IsPrintAllowed () const =0
 
virtual bool IsEditAllowed () const =0
 
virtual bool IsCopyAllowed () const =0
 
virtual bool IsEditNotesAllowed () const =0
 
virtual bool IsFillAndSignAllowed () const =0
 
virtual bool IsAccessibilityAllowed () const =0
 
virtual bool IsDocAssemblyAllowed () const =0
 
virtual bool IsHighPrintAllowed () const =0
 
PdfObjectGetCatalog ()
 
const PdfObjectGetCatalog () const
 
PdfPageTreeGetPageTree ()
 
const PdfPageTreeGetPageTree () const
 
PdfObjectGetTrailer ()
 
const PdfObjectGetTrailer () const
 
PdfInfoGetInfo ()
 
const PdfInfoGetInfo () const
 
PdfIndirectObjectListGetObjects ()
 
const PdfIndirectObjectListGetObjects () const
 
PdfObjectGetStructTreeRoot ()
 
PdfObjectGetMetadata ()
 
PdfObjectGetMarkInfo ()
 
PdfObjectGetLanguage ()
 

Additional Inherited Members

- Protected Member Functions inherited from mm::PdfDocument
 PdfDocument (bool empty=false)
 
void SetTrailer (std::unique_ptr< PdfObject > obj)
 
void Init ()
 
void FixObjectReferences (PdfObject &obj, int difference)
 
void SetViewerPreference (const PdfName &whichPref, const PdfObject &valueObj)
 
void SetViewerPreference (const PdfName &whichPref, bool inValue)
 
void Clear ()
 
PdfObjectgetCatalog ()
 

Detailed Description

PdfMemDocument is the core class for reading and manipulating PDF files and writing them back to disk.

PdfMemDocument was designed to allow easy access to the object structur of a PDF file.

PdfMemDocument should be used whenever you want to change the object structure of a PDF file.

When you are only creating PDF files, please use PdfStreamedDocument which is usually faster for creating PDFs.

See also
PdfDocument
PdfStreamedDocument
PdfParser
PdfWriter

Constructor & Destructor Documentation

◆ PdfMemDocument() [1/2]

PdfMemDocument::PdfMemDocument ( bool  empty = false)

Construct a new PdfMemDocument

◆ PdfMemDocument() [2/2]

PdfMemDocument::PdfMemDocument ( const PdfMemDocument rhs)

Construct a copy of the given document

Member Function Documentation

◆ AddPdfExtension()

void PdfMemDocument::AddPdfExtension ( const PdfName ns,
int64_t  level 
)

Add a vendor-specific extension to the current PDF version.

Parameters
nsnamespace of the extension
levellevel of the extension

◆ FreeObjectMemory() [1/2]

void PdfMemDocument::FreeObjectMemory ( const PdfReference ref,
bool  force = false 
)

Tries to free all memory allocated by the given PdfObject (variables and streams) and reads it from disk again if it is requested another time.

This will only work if load on demand is used. Other- wise any call to this method will be ignored. Load on demand is currently always enabled when using PdfMemDocument. If the object is dirty if will not be free'd.

Parameters
reffree all memory allocated by the object with this reference.
forceif true the object will be free'd even if IsDirty() returns true. So you will loose any changes made to this object.

This is an overloaded member for your convenience.

◆ FreeObjectMemory() [2/2]

void PdfMemDocument::FreeObjectMemory ( PdfObject obj,
bool  force = false 
)

Tries to free all memory allocated by the given PdfObject (variables and streams) and reads it from disk again if it is requested another time.

This will only work if load on demand is used. Other- wise any call to this method will be ignored. Load on demand is currently always enabled when using PdfMemDocument. If the object is dirty if will not be free'd.

Parameters
objfree object from memory
forceif true the object will be free'd even if IsDirty() returns true. So you will loose any changes made to this object.
See also
IsDirty

◆ GetEncrypt()

const PdfEncrypt * mm::PdfMemDocument::GetEncrypt ( ) const
inline
Returns
the parsers encryption object or nullptr if the read PDF file was not encrypted

◆ GetPdfExtensions()

vector< PdfExtension > PdfMemDocument::GetPdfExtensions ( ) const

Return the list of all vendor-specific extensions to the current PDF version.

Parameters
nsnamespace of the extension
levellevel of the extension

◆ GetPdfVersion()

PdfVersion mm::PdfMemDocument::GetPdfVersion ( ) const
inlineoverridevirtual

Get the PDF version of the document

Returns
PdfVersion version of the pdf document

Implements mm::PdfDocument.

◆ HasPdfExtension()

bool PdfMemDocument::HasPdfExtension ( const PdfName ns,
int64_t  level 
) const

Checks whether the documents is tagged to imlpement a vendor-specific extension to the current PDF version.

Parameters
nsnamespace of the extension
levellevel of the extension

◆ InsertPages()

const PdfMemDocument & PdfMemDocument::InsertPages ( const PdfMemDocument doc,
unsigned  atIndex,
unsigned  pageCount 
)

Copies one or more pages from another PdfMemDocument to this document

Parameters
docthe document to append
atIndexthe first page number to copy (0-based)
pageCountthe number of pages to copy
Returns
this document

◆ IsAccessibilityAllowed()

bool PdfMemDocument::IsAccessibilityAllowed ( ) const
overridevirtual

Checks if it is allowed to extract text and graphics to support users with disabilities. Every PDF-consuming application has to adhere to this value!

Returns
true if you are allowed to extract text and graphics to support users with disabilities
See also
PdfEncrypt to set own document permissions.

Implements mm::PdfDocument.

◆ IsCopyAllowed()

bool PdfMemDocument::IsCopyAllowed ( ) const
overridevirtual

Checks if text and graphics extraction is allowed. Every PDF-consuming application has to adhere to this value!

Returns
true if you are allowed to extract text and graphics from this document
See also
PdfEncrypt to set own document permissions.

Implements mm::PdfDocument.

◆ IsDocAssemblyAllowed()

bool PdfMemDocument::IsDocAssemblyAllowed ( ) const
overridevirtual

Checks if it is allowed to insert, create, rotate, or delete pages or add bookmarks. Every PDF-consuming application has to adhere to this value!

Returns
true if you are allowed to insert, create, rotate, or delete pages or add bookmarks
See also
PdfEncrypt to set own document permissions.

Implements mm::PdfDocument.

◆ IsEditAllowed()

bool PdfMemDocument::IsEditAllowed ( ) const
overridevirtual

Checks if modifying this document (besides annotations, form fields or substituting pages) is allowed. Every PDF-consuming application has to adhere to this value!

Returns
true if you are allowed to modify this document
See also
PdfEncrypt to set own document permissions.

Implements mm::PdfDocument.

◆ IsEditNotesAllowed()

bool PdfMemDocument::IsEditNotesAllowed ( ) const
overridevirtual

Checks if it is allowed to add or modify annotations or form fields. Every PDF-consuming application has to adhere to this value!

Returns
true if you are allowed to add or modify annotations or form fields
See also
PdfEncrypt to set own document permissions.

Implements mm::PdfDocument.

◆ IsEncrypted()

bool mm::PdfMemDocument::IsEncrypted ( ) const
inline
Returns
true if this PdfMemDocument creates an encrypted PDF file

◆ IsFillAndSignAllowed()

bool PdfMemDocument::IsFillAndSignAllowed ( ) const
overridevirtual

Checks if it is allowed to fill in existing form or signature fields. Every PDF-consuming application has to adhere to this value!

Returns
true if you are allowed to fill in existing form or signature fields
See also
PdfEncrypt to set own document permissions.

Implements mm::PdfDocument.

◆ IsHighPrintAllowed()

bool PdfMemDocument::IsHighPrintAllowed ( ) const
overridevirtual

Checks if it is allowed to print a high quality version of this document Every PDF-consuming application has to adhere to this value!

Returns
true if you are allowed to print a high quality version of this document
See also
PdfEncrypt to set own document permissions.

Implements mm::PdfDocument.

◆ IsLinearized()

bool mm::PdfMemDocument::IsLinearized ( ) const
inlineoverridevirtual

Returns whether this PDF document is linearized, aka web-optimized

Returns
true if the PDF document is linearized

Implements mm::PdfDocument.

◆ IsPrintAllowed()

bool PdfMemDocument::IsPrintAllowed ( ) const
overridevirtual

Checks if printing this document is allowed. Every PDF-consuming application has to adhere to this value!

Returns
true if you are allowed to print this document
See also
PdfEncrypt to set own document permissions.

Implements mm::PdfDocument.

◆ Load()

void PdfMemDocument::Load ( const std::string_view &  filename,
const std::string_view &  password = { } 
)

Load a PdfMemDocument from a file

Parameters
filenamefilename of the file which is going to be parsed/opened

When the bForUpdate is set to true, the filename is copied for later use by WriteUpdate.

See also
WriteUpdate, LoadFromBuffer, LoadFromDevice

◆ LoadFromBuffer()

void PdfMemDocument::LoadFromBuffer ( const std::string_view &  buffer,
const std::string_view &  password = { } 
)

Load a PdfMemDocument from a buffer in memory

Parameters
buffera memory area containing the PDF data
See also
WriteUpdate, Load, LoadFromDevice

◆ LoadFromDevice()

void PdfMemDocument::LoadFromDevice ( const std::shared_ptr< PdfInputDevice > &  device,
const std::string_view &  password = { } 
)

Load a PdfMemDocument from a PdfRefCountedInputDevice

Parameters
devicethe input device containing the PDF
See also
WriteUpdate, Load, LoadFromBuffer

◆ RemovePdfExtension()

void PdfMemDocument::RemovePdfExtension ( const PdfName ns,
int64_t  level 
)

Remove a vendor-specific extension to the current PDF version.

Parameters
nsnamespace of the extension
levellevel of the extension

◆ SetEncrypted() [1/2]

void PdfMemDocument::SetEncrypted ( const PdfEncrypt encrypt)

Encrypt the document during writing using a PdfEncrypt object

Parameters
encryptan encryption object that will be owned by PdfMemDocument

◆ SetEncrypted() [2/2]

void PdfMemDocument::SetEncrypted ( const std::string &  userPassword,
const std::string &  ownerPassword,
PdfPermissions  protection = PdfPermissions::Default,
PdfEncryptAlgorithm  algorithm = PdfEncryptAlgorithm::AESV2,
PdfKeyLength  keyLength = PdfKeyLength::L40 
)

Encrypt the document during writing.

Parameters
userPasswordthe user password (if empty the user does not have to enter a password to open the document)
ownerPasswordthe owner password
protectionseveral PdfPermissions values or'ed together to set the users permissions for this document
algorithmthe revision of the encryption algorithm to be used
keyLengththe length of the encryption key ranging from 40 to 256 bits (only used if algorithm >= PdfEncryptAlgorithm::RC4V2)
See also
PdfEncrypt

◆ SetPdfVersion()

void mm::PdfMemDocument::SetPdfVersion ( PdfVersion  version)
inline

Set the PDF Version of the document. Has to be called before Write() to have an effect.

Parameters
versionversion of the pdf document

◆ Write() [1/2]

void mm::PdfMemDocument::Write ( const std::string_view &  filename,
PdfSaveOptions  opts = PdfSaveOptions::None 
)

Writes the complete document to a file

Parameters
filenamefilename of the document
See also
Write, WriteUpdate

This is an overloaded member function for your convenience.

◆ Write() [2/2]

void PdfMemDocument::Write ( PdfOutputDevice device,
PdfSaveOptions  opts = PdfSaveOptions::None 
)

Writes the complete document to an output device

Parameters
devicewrite to this output device
See also
WriteUpdate

◆ WriteUpdate() [1/2]

void mm::PdfMemDocument::WriteUpdate ( const std::string_view &  filename,
PdfSaveOptions  opts = PdfSaveOptions::None 
)

Writes the document changes to a file

Parameters
filenamefilename of the document

Writes the document changes to a file as an incremental update. The document should be loaded with bForUpdate = true, otherwise an exception is thrown.

Beware when overwriting existing files. Plain file overwrite is allowed only if the document was loaded with the same filename (and the same overloaded function), otherwise the destination file cannot be the same as the source file, because the destination file is truncated first and only then the source file content is copied into it.

See also
Write, WriteUpdate

This is an overloaded member function for your convenience.

◆ WriteUpdate() [2/2]

void PdfMemDocument::WriteUpdate ( PdfOutputDevice device,
PdfSaveOptions  opts = PdfSaveOptions::None 
)

Writes the document changes to an output device

Parameters
devicewrite to this output device
bTruncatewhether to truncate the device first and fill it with the content of the source document; the default is true.

Writes the document changes to the output device as an incremental update. The document should be loaded with bForUpdate = true, otherwise an exception is thrown.

The bTruncate is used to determine whether saving to the same file or not. In case the bTruncate is true, a new source stream is opened and its whole content is copied to the device first. Otherwise the device is the same file which had been loaded and the caller is responsible to position the device at the place, where the update should be written (basically at the end of the stream).

See also
Write, WriteUpdate