Winand

PDF xreftable position

Nov 8th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. """
  2. ........
  3. %xref
  4. %0 8
  5. %0000000000 65535 f
  6. %0000000010 00000 n
  7. %0000000079 00000 n
  8. %0000000179 00000 n
  9. %0000000315 00000 n
  10. %0000000394 00000 n
  11. %0000000506 00000 n
  12. %0000000602 00000 n
  13. trailer
  14. <<
  15.  /Size 8
  16.  /Root 1 0 R
  17. >>
  18. %startxref
  19. %732
  20. %%EOF
  21. """
  22.  
  23. #We want to get '732' value
  24. def getXrefTable(hfile):
  25.     hfile.seek(0, 2)
  26.     size = hfile.tell()
  27.     chunk=100 #1 iteration is enough in most cases i hope
  28.     for i in range(1, int(size/chunk)+1):
  29.         hfile.seek(-chunk*i, 2)
  30.         dat = filter(bool, [l.strip() for l in hfile.readlines()])
  31.         if len(dat) > 3: #to be sure that we've read last 3 lines
  32.             if (dat[-1]=="%%EOF") & (dat[-3]=="startxref"):
  33.                 xrefpos=int(dat[-2])
  34.                 break
  35.     return xrefpos
Advertisement
Add Comment
Please, Sign In to add comment