{
    "mode": "perldoc",
    "parameter": "PDF::API2::Basic::PDF::File",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/PDF%3A%3AAPI2%3A%3ABasic%3A%3APDF%3A%3AFile/json",
    "generated": "2026-06-10T20:52:25Z",
    "synopsis": "$p = PDF::API2::Basic::PDF::File->open(\"filename.pdf\", 1);\n$p->newobj($objref);\n$p->freeobj($objref);\n$p->appendfile;\n$p->closefile;\n$p->release;       # IMPORTANT!",
    "sections": {
        "NAME": {
            "content": "PDF::API2::Basic::PDF::File - Low-level PDF file access\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "$p = PDF::API2::Basic::PDF::File->open(\"filename.pdf\", 1);\n$p->newobj($objref);\n$p->freeobj($objref);\n$p->appendfile;\n$p->closefile;\n$p->release;       # IMPORTANT!\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This class keeps track of the directory aspects of a PDF file. There are two parts to the\ndirectory: the main directory object which is the parent to all other objects and a chain of\ncross-reference tables and corresponding trailer dictionaries starting with the main directory\nobject.\n",
            "subsections": []
        },
        "INSTANCE VARIABLES": {
            "content": "Within this class hierarchy, rather than making everything visible via methods, which would be a\nlot of work, there are various instance variables which are accessible via associative array\nreferencing. To distinguish instance variables from content variables (which may come from the\nPDF content itself), each such variable will start with a space.\n\nVariables which do not start with a space directly reflect elements in a PDF dictionary. In the\ncase of a PDF::API2::Basic::PDF::File, the elements reflect those in the trailer dictionary.\n\nSince some variables are not designed for class users to access, variables are marked in the\ndocumentation with (R) to indicate that such an entry should only be used as read-only\ninformation. (P) indicates that the information is private and not designed for user use at all,\nbut is included in the documentation for completeness and to ensure that nobody else tries to\nuse it.\n\nnewroot\nThis variable allows the user to create a new root entry to occur in the trailer dictionary\nwhich is output when the file is written or appended. If you wish to over-ride the root\nelement in the dictionary you have, use this entry to indicate that without losing the\ncurrent Root entry. Notice that newroot should point to a PDF level object and not just to a\ndictionary which does not have object status.\n\nINFILE (R)\nContains the filehandle used to read this information into this PDF directory. Is an IO\nobject.\n\nfname (R)\nThis is the filename which is reflected by INFILE, or the original IO object passed in.\n\nupdate (R)\nThis indicates that the read file has been opened for update and that at some point,\n$p->appendfile() can be called to update the file with the changes that have been made to\nthe memory representation.\n\nmaxobj (R)\nContains the first usable object number above any that have already appeared in the file so\nfar.\n\noutlist (P)\nThis is a list of Objind which are to be output when the next appendfile or outfile occurs.\n\nfirstfree (P)\nContains the first free object in the free object list. Free objects are removed from the\nfront of the list and added to the end.\n\nlastfree (P)\nContains the last free object in the free list. It may be the same as the firstfree if there\nis only one free object.\n\nobjcache (P)\nAll objects are held in the cache to ensure that a system only has one occurrence of each\nobject. In effect, the objind class acts as a container type class to hold the PDF object\nstructure and it would be unfortunate if there were two identical place-holders floating\naround a system.\n\nepos (P)\nThe end location of the read-file.\n\nEach trailer dictionary contains a number of private instance variables which hold the chain\ntogether.\n\nloc (P)\nContains the location of the start of the cross-reference table preceding the trailer.\n\nxref (P)\nContains an anonymous array of each cross-reference table entry.\n\nprev (P)\nA reference to the previous table. Note this differs from the Prev entry which is in PDF\nwhich contains the location of the previous cross-reference table.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "PDF::API2::Basic::PDF::File->new\nCreates a new, empty file object which can act as the host to other PDF objects. Since there is\nno file associated with this object, it is assumed that the object is created in readiness for\ncreating a new PDF file.\n\n$p = PDF::API2::Basic::PDF::File->open($filename, $update)\nOpens the file and reads all the trailers and cross reference tables to build a complete\ndirectory of objects.\n\n$update specifies whether this file is being opened for updating and editing, or simply to be\nread.\n\n$filename may be an IO object\n\n$p->version($version)\nGets/sets the PDF version (e.g. 1.4)\n\n$version = $p->headerversion($version)\nGets/sets the PDF version stored in the file header.\n\n$version = $p->trailerversion($version)\nGets/sets the PDF version stored in the document catalog.\n\n$prevversion = $p->requireversion($version)\nEnsures that the PDF version is at least $version.\n\n$p->release()\nReleases ALL of the memory used by the PDF document and all of its component objects. After\ncalling this method, do NOT expect to have anything left in the \"PDF::API2::Basic::PDF::File\"\nobject (so if you need to save, be sure to do it before calling this method).\n\nNOTE, that it is important that you call this method on any \"PDF::API2::Basic::PDF::File\" object\nwhen you wish to destruct it and free up its memory. Internally, PDF files have an enormous\nnumber of cross-references and this causes circular references within the internal data\nstructures. Calling '\"release()\"' forces a brute-force cleanup of the data structures, freeing\nup all of the memory. Once you've called this method, though, don't expect to be able to do\nanything else with the \"PDF::API2::Basic::PDF::File\" object; it'll have no internal state\nwhatsoever.\n\n$p->appendfile()\nAppends the objects for output to the read file and then appends the appropriate table.\n\n$p->outfile($fname)\nWrites a PDF file to a file of the given filename based on the current list of objects to be\noutput. It creates the trailer dictionary based on information in $self.\n\n$fname may be an IO object;\n\n$p->createfile($fname)\nCreates a new output file (no check is made of an existing open file) of the given filename or\nIO object. Note, make sure that $p->{' version'} is set correctly before calling this function.\n\n$p->clonefile($fname)\nCreates a copy of the input file at the specified filename and sets it as the output file for\nfuture writes. A file handle may be passed instead of a filename.\n\n$p->closefile\nCloses up the open file for output by outputting the trailer etc.\n\n($value, $str) = $p->readval($str, %opts)\nReads a PDF value from the current position in the file. If $str is too short then read some\nmore from the current location in the file until the whole object is read. This is a recursive\ncall which may slurp in a whole big stream (unprocessed).\n\nReturns the recursive data structure read and also the current $str that has been read from the\nfile.\n\n$ref = $p->readobj($objind, %opts)\nGiven an indirect object reference, locate it and read the object returning the read in object.\n\n$ref = $p->readobjnum($num, $gen, %opts)\nReturns a fully read object of given number and generation in this file\n\n$objind = $p->newobj($obj)\nCreates a new, free object reference based on free space in the cross reference chain. If\nnothing free then thinks up a new number. If $obj then turns that object into this new object\nrather than returning a new object.\n\n$p->outobj($objind)\nIndicates that the given object reference should appear in the output xref table whether with\ndata or freed.\n\n$p->freeobj($objind)\nMarks an object reference for output as being freed.\n\n$p->removeobj($objind)\nRemoves the object from all places where we might remember it\n\n$p->shipout(@objects)\nShips the given objects (or all objects for output if @objects is empty) to the currently open\noutput file (assuming there is one). Freed objects are not shipped, and once an object is\nshipped it is switched such that this file becomes its source and it will not be shipped again\nunless outobj is called again. Notice that a shipped out object can be re-output or even freed,\nbut that it will not cause the data already output to be changed.\n\n$p->copy($outpdf, \\&filter)\nIterates over every object in the file reading the object, calling filter with the object and\noutputting the result. if filter is not defined, then just copies input to output.\n\nPRIVATE METHODS & FUNCTIONS\nThe following methods and functions are considered private to this class. This does not mean you\ncannot use them if you have a need, just that they aren't really designed for users of this\nclass.\n\n$offset = $p->locateobj($num, $gen)\nReturns a file offset to the object asked for by following the chain of cross reference tables\nuntil it finds the one you want.\n\nupdate($fh, $str, $instream)\nKeeps reading $fh for more data to ensure that $str has at least a line full for \"readval\" to\nwork on. At this point we also take the opportunity to ignore comments.\n\n$objind = $p->testobj($num, $gen)\nTests the cache to see whether an object reference (which may or may not have been getobj()ed)\nhas been cached. Returns it if it has.\n\n$p->addobj($objind)\nAdds the given object to the internal object cache.\n\n$tdict = $p->readxrtr($xpos)\nRecursive function which reads each of the cross-reference and trailer tables in turn until\nthere are no more.\n\nReturns a dictionary corresponding to the trailer chain. Each trailer also includes the\ncorresponding cross-reference table.\n\nThe structure of the xref private element in a trailer dictionary is of an anonymous hash of\ncross reference elements by object number. Each element consists of an array of 3 elements\ncorresponding to the three elements read in [location, generation number, free or used]. See the\nPDF specification for details.\n\n$p->outtrailer($tdict)\nOutputs the body and trailer for a PDF file by outputting all the objects in the ' outlist' and\nthen outputting a xref table for those objects and any freed ones. It then outputs the trailing\ndictionary and the trailer code.\n\nPDF::API2::Basic::PDF::File->new\nCreates a very empty PDF file object (used by new and open)\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Martin Hosken MartinHosken@sil.org\n\nCopyright Martin Hosken 1999 and onwards\n\nNo warranty or expression of effectiveness, least of all regarding anyone's safety, is implied\nin this software or documentation.\n",
            "subsections": []
        }
    },
    "summary": "PDF::API2::Basic::PDF::File - Low-level PDF file access",
    "flags": [],
    "examples": [],
    "see_also": []
}