Added elements not being moved with page


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 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.""""_______________________________________________")"Rearranging pdf file...")

        new_doc = PDFDoc()
        doc = PDFDoc(infile)
        copy_pages = VectorPage()
        itr = doc.GetPageIterator()
        while itr.HasNext():

        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.')
                        for pageNo in value[1]:
        new_doc.Save(infile, SDFDoc.e_remove_unused)

def addBarcode(inputFile, outfile, accounts):
        """Add barcode image and text to each page""""_______________________________________________")"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()

                        barcode = buildBarcode(workOrder, currStatement, pos, totPages)
                        encoder = DataMatrixEncoder(barcode)
                        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)

                        if totPages > 8:
                                # position datamatrix .5 from left edge, 2.0 from bottom
                                element = eb.CreateImage(img, 36, 165, 20, 20)
                                # position datamatrix .5 from right edge, .7 from bottom
                                element = eb.CreateImage(img, 555, 55, 20, 20)

                        # Begin writing a block of text
                        element = eb.CreateTextBegin(Font.Create(doc.GetSDFDoc(), Font.e_times_roman), 6)

                        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



        doc.Save(outfile, SDFDoc.e_remove_unused)
        doc.Close()"Done. Result saved in " + outfile +"...")

Spencer Rathbun
L & D Mail Masters, Inc.
110 Security Parkway
New Albany, IN 47150
Phone: 812.981.7161
Fax: 812.981.7169