Finally found the bug. It seems that you need to write the form element before you start recursing into the form object, else all of the sub elements get “bumped up” a level. So
elif mytype == Element.e_form: # Recursively process form XObjects
reader.FormBegin()
doctype, firstPage, statementNum = self.ProcessElements(reader, writer, statementNum)
reader.End()
should be
elif mytype == Element.e_form: # Recursively process form XObjects
writer.WriteElement(element) # write the form element, so that we don’t “bump up” the sub elements
reader.FormBegin()
doctype, firstPage, statementNum = self.ProcessElements(reader, writer, statementNum)
reader.End()
This should probably be fixed in the examples.
Spencer Rathbun
-----Original Message-----
From: PDFTron PDFNet SDK [mailto:pdfnet-sdk@googlegroups.com]
Sent: Wednesday, June 27, 2012 1:12 PM
To: ‘PDFTron PDFNet SDK’
Subject: [PDFNet] RE: SetTransform does not adjust page contents
After additional research, it seems that a followup method is masking the transforms. I traced it back to a recursive processElements loop.
def ProcessElements(self, reader, writer, currStmnt, **kwargs):
element = reader.Next() # Read page contents
firstPage = False
statementNum = currStmnt
doctype = ‘’
while element != None:
mytype = element.GetType()
gs = element.GetGState() # get the graphics state obj for the current element
multiply blend mode causes black to overlay all and white to drop behind all. WILL MIX OTHER COLORS THAT OVERLAP!
gs.SetBlendMode(GState.e_bl_multiply)
if mytype == Element.e_image:
Reset to normal mode for images, otherwise they may blank out
gs.SetBlendMode(GState.e_bl_normal)
self.processImage(element)
writer.WriteElement(element)
elif mytype == Element.e_inline_image:
Reset to normal mode for images, otherwise they may blank out
gs.SetBlendMode(GState.e_bl_normal)
self.processImage(element)
writer.WriteElement(element)
elif mytype == Element.e_text: # Process text strings…
text = element.GetTextString()
try:
match = self.headers.search(text)
if match:
doctype = match.lastgroup
if re.search(r’Employer Name’, text):
firstPage = True
statementNum = statementNum + 1
except IndexError:
pass
writer.WriteElement(element)
elif mytype == Element.e_form: # Recursively process form XObjects
reader.FormBegin()
doctype, firstPage, statementNum = self.ProcessElements(reader, writer, statementNum)
reader.End()
else:
writer.WriteElement(element)
element = reader.Next()
return doctype, firstPage, statementNum
If you comment out the recursive call then the transform works. So something about this code, pulled from the element reader example, causes information to be lost when recursively reading forms. What would I need to add to fix this?
Spencer Rathbun
-----Original Message-----
From: PDFTron PDFNet SDK [mailto:pdfnet-sdk@googlegroups.com]
Sent: Wednesday, June 27, 2012 10:54 AM
To: ‘pdfnet-sdk@googlegroups.com’
Subject: [PDFNet] SetTransform does not adjust page contents
All,
I’m following the example in the imposition test project to resize my pdf pages. The code sucessfully runs, and the output page size is 8.5x11 as expected. However, the original page content is not modified, so it does not entirely fit on the page. No matter how I adjust the SetTransform call, I can’t shift it or scale it.
def mergePages(self, infiles, outfile):
“”“Merge pages of input files, and save as output filename.”""
self.logger.info("_______________________________________________")
self.logger.info(“Merging the input pdf files…”)
new_doc = PDFDoc()
new paper size
media_box = Rect(0, 0, 612, 792)
builder = ElementBuilder()
writer = ElementWriter()
for f in infiles:
in_doc = PDFDoc(f)
in_doc.InitSecurityHandler()
copy_pages = VectorPage()
itr = in_doc.GetPageIterator()
while itr.HasNext():
copy_pages.push_back(itr.Current())
itr.Next()
imported_pages = new_doc.ImportPages(copy_pages)
i = iter(imported_pages)
for x in i:
fit content to 8.5x11 page size
new_page = new_doc.PageCreate(media_box)
writer.Begin(new_page)
element = builder.CreateForm(x)
sc_x = media_box.Width() / x.GetPageWidth()
sc_y = media_box.Height() / x.GetPageHeight()
scale = min(sc_x, sc_y)
element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0)
writer.WritePlacedElement(element)
writer.End()
#new_page.Scale(.80)
new_doc.PagePushBack(new_page)
new_doc.Save(outfile, 0)
Close the open document to free up document memory sooner than waiting for the
garbage collector
in_doc.Close()
new_doc.Close()
As you can see, I’m merging multiple pdfs together, and scaling their original content down to size. I don’t know how the input pdfs are constructed, but I thought that SetTransform was supposed to be recursive across sub elements. What possible causes are there for this behavior?
Spencer Rathbun
L & D Mail Masters, Inc.
110 Security Pkwy
New Albany, IN 47150
812.981.7161 X.171
Fax: 812.981.7169
–
You received this message because you are subscribed to the “PDFTron PDFNet SDK” group. To post to this group, send email to support@pdftron.com
To unsubscribe from this group, send email to pdfnet-sdk-unsubscribe@googlegroups.com. For more information, please visit us at http://www.pdftron.com
–
You received this message because you are subscribed to the “PDFTron PDFNet SDK” group. To post to this group, send email to support@pdftron.com
To unsubscribe from this group, send email to pdfnet-sdk-unsubscribe@googlegroups.com. For more information, please visit us at http://www.pdftron.com