Q)
All right,
I tried that:
‘’‘Import necessary page resources’’’
resource_pages = VectorPage()
resource_pages.push_back(backgroundPage)
resource_pages.push_back(backOfStatementPage)
imported_pages = new_doc.ImportPages(copy_pages)
res_pages = new_doc.ImportPages(resource_pages)
And I get a crash:
Problem Event Name: APPCRASH
Application Name: python.exe
Application Version: 0.0.0.0
Application Timestamp: 4cf14060
Fault Module Name: PDFNetC.dll
Fault Module Version: 5.7.0.0
Fault Module Timestamp: 4df6ac02
Exception Code: c0000005
Exception Offset: 003af2da
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
It seems to not be fond of two imports of separate VectorPage resources. So I added the two background pages to the copy_pages object, and skipped past them before I iterate over it.
copy_pages.push_back(backgroundPage)
copy_pages.push_back(backOfStatementPage)
…
imported_pages = new_doc.ImportPages(copy_pages)
i = iter(imported_pages)
i.next()
i.next()
for x in i:
…
This causes a different crash:
Problem Event Name: APPCRASH
Application Name: python.exe
Application Version: 0.0.0.0
Application Timestamp: 4cf14060
Fault Module Name: gdiplus.dll
Fault Module Version: 6.1.7601.17825
Fault Module Timestamp: 4f9235ab
Exception Code: c0000005
Exception Offset: 000a0001
OS Version: 6.1.7601.2.1.0.256.48
Locale ID: 1033
Additional Information 1: 0a9e
Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
Additional Information 3: 0a9e
Additional Information 4: 0a9e372d3b4ad19135b953a78882e789
Here is the complete function, to make it clearer what is going on:
def printHp(infile, accounts, workOrder, outfile):
“”“Print the different statement groups to postscript.”""
logger.info("_______________________________________________")
logger.info(“Printing pages for HP…”)
checksOnPage = False
in_doc = PDFDoc(infile)
in_doc.InitSecurityHandler()
(groupings, names) = getGroupings(accounts)
buildFirstLast(“firstLastReport.txt”, names)
iterAccounts = iter(sorted(accounts.iteritems(), key=lambda x: x[1][0]))
output_path = “.”
‘’‘Open the background pdf and get it in memory’’’
bp_doc = PDFDoc(‘background.pdf’)
itr = bp_doc.GetPageIterator()
backgroundPage = itr.Current()
‘’‘Open the back of the pdf and get it in memory’’’
bp_doc = PDFDoc(‘backOfStatement.pdf’)
itr = bp_doc.GetPageIterator()
backOfStatementPage = itr.Current()
‘’‘Import necessary page resources’’’
for group in groupings:
logger.info(“pages {0}-{1}”.format(group[1], group[2]))
new_doc = PDFDoc()
copy_pages = VectorPage()
copy_pages.push_back(backgroundPage)
copy_pages.push_back(backOfStatementPage)
itr = in_doc.GetPageIterator(group[1])
while itr.HasNext():
if itr.Current().GetIndex() > group[2]:
break
page = itr.Current()
copy_pages.push_back(page)
itr.Next()
imported_pages = new_doc.ImportPages(copy_pages)
i = iter(imported_pages)
i.next()
i.next()
for x in i:
try:
currPage = currPages.next()
logger.debug(“Current page: {0}”.format(currPage))
if currPage in item[1][3]:
checksOnPage = True
except (StopIteration, UnboundLocalError):
item = iterAccounts.next()
currPages = iter(item[1][1])
currPage = currPages.next()
logger.debug(“Current page: {0}”.format(currPage))
if currPage in item[1][3]:
checksOnPage = True
‘’‘letterhead is used for standard pages, but check pages are printed on plain’’’
if checksOnPage:
checksOnPage = False
‘’‘push back the current page’’’
new_doc.PagePushBack(x)
‘’‘create a blank page for the back and push it’’’
blankPage = new_doc.PageCreate()
new_doc.PagePushBack(blankPage)
else:
‘’‘add the background to the current page and push it’’’
writer = ElementWriter()
reader = ElementReader()
reader.Begin(backgroundPage)
writer.Begin(x, ElementWriter.e_underlay, False)
addBackground(reader, writer)
writer.End()
reader.End()
new_doc.PagePushBack(x)
‘’‘push back the back of statement’’’
new_doc.PagePushBack(backOfStatementPage)
if checkRemainingDriveSpace(os.path.abspath(output_path)) < 4000:
output_path = ‘’
for drive in getDrives():
if checkRemainingDriveSpace(drive) > 4000:
if not os.path.exists(os.path.join(drive, ‘temp’)):
os.mkdir(os.path.join(drive, ‘temp’))
output_path = os.path.join(drive, ‘temp’)
logger.info(“Current Drive has less than 4000 megabytes of free space remaining. Switching to {0}”.format(output_path))
break
if not output_path:
logger.info(“All mapped drives have less than 4000 megabytes of space!”)
logger.info(“Quitting on creation of file: {0}”.format(output_filename))
sys.exit(1)
basename = “{0}{1}{2}_{3}”.format(workOrder, group[0], group[1], group[2])
new_doc.Save("{0}.pdf".format(os.path.join(output_path, basename)), 0)
Close the open document to free up document memory sooner than waiting for the
garbage collector
new_doc.Close()
in_doc.Close()
Progress, but still not functional.
A) First, yes, all the pages used in a single call to PDFDoc.ImportPages must be from the same document (this is in the documentation, and an exception also thrown in this case).
Secondly, and I think this is the first error you reported in the last message, the ElementReader is still using the original background page from the original document, ElementReader should be using the first page from “imported_pages” variable.
However, instead of all this you can just use the Stamper class. I’ve attached code that does what you want, importing a background page. The benefit of this is that the Stamper class not only handles all the element reading and writing for you, but it will also auto import the page resources if it’s from another document. Finally, Stamper provides a lot of handy features for customization.
If you’re using a PDFNet version without Stamper, then making the two changes mentioned should fix your issues.
PDFPageTest.py (1.69 KB)