All,
I'm adding a new element to each page in a pdf, similar to the element builder/edit samples. Then I'm rearranging the pdf pages, as in sample 6 of the pdfpagetest.py sample. However, the text elements that I've added do not get moved with their associated pages. So if I add "this is page n" to each page, then reverse the page order, the text would still say page one is first. Even though I've moved page one to the end of the document.
Is there something I'm missing here? Relevant functions below.
def movePages(infile, accounts):
"""Rearrange pages into statement order."""
logger.info("_______________________________________________")
logger.info("Rearranging pdf file...")
new_doc = PDFDoc()
doc = PDFDoc(infile)
doc.InitSecurityHandler()
copy_pages = VectorPage()
itr = doc.GetPageIterator()
while itr.HasNext():
copy_pages.push_back(itr.Current())
itr.Next()
imported_pages = new_doc.ImportPages(copy_pages)
for key, value in sorted(accounts.iteritems(), key=lambda x: x[1][0]):
if value[2].find('*************') != -1:
logger.warning('Found bad statement, not printing.')
else:
for pageNo in value[1]:
new_doc.PagePushBack(imported_pages[int(pageNo)-1])
doc.Close()
new_doc.Save(infile, SDFDoc.e_remove_unused)
new_doc.Close()
def addBarcode(inputFile, outfile, accounts):
"""Add barcode image and text to each page"""
logger.info("_______________________________________________")
logger.info("Stamping combined pdf file...")
workOrder = ""
doc = inputFile
itr = doc.GetPageIterator()
eb = ElementBuilder()
writer = ElementWriter()
for item,currStatement in zip(sorted(accounts.iteritems(), key=lambda x: x[1][1][0]), count(1)):
totPages = len(item[1][1])
workOrder = item[0]
for pos,currPage in enumerate(item[1][1]):
page = itr.Current()
writer.Begin(page)
barcode = buildBarcode(workOrder, currStatement, pos, totPages)
encoder = DataMatrixEncoder(barcode)
encoder.save("tmp.png")
img = Image.Create(doc.GetSDFDoc(), "tmp.png")
if pos == 0:
# position datamatrix .91 from left edge, 2.36 from top
element = eb.CreateImage(img, 66, 603, 20, 20)
writer.WritePlacedElement(element)
if totPages > 8:
# position datamatrix .5 from left edge, 2.0 from bottom
element = eb.CreateImage(img, 36, 165, 20, 20)
writer.WritePlacedElement(element)
else:
# position datamatrix .5 from right edge, .7 from bottom
element = eb.CreateImage(img, 555, 55, 20, 20)
writer.WritePlacedElement(element)
# Begin writing a block of text
element = eb.CreateTextBegin(Font.Create(doc.GetSDFDoc(), Font.e_times_roman), 6)
writer.WriteElement(element)
element = eb.CreateTextRun(barcode)
# sets text to be .5 from left edge, and .2 from bottom edge
element.SetTextMatrix(1, 0, 0, 1, 36, 15)
element.GetGState().SetLeading(1) # Set the spacing between lines
writer.WriteElement(element)
writer.WriteElement(eb.CreateTextEnd())
writer.End()
eb.Reset()
itr.Next()
doc.Save(outfile, SDFDoc.e_remove_unused)
doc.Close()
logger.info("Done. Result saved in " + outfile +"...")
Spencer Rathbun
IT/Programming
L & D Mail Masters, Inc.
110 Security Parkway
New Albany, IN 47150
Phone: 812.981.7161
Fax: 812.981.7169
srathbun@ldmailmasters.com