分享
 
 
 

给你一份PDF文档,知道它有多少页

王朝other·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

PDF Page Count Algorithm

December 2004

It's not often that you can't find coding examples on the internet to do basic programming tasks, so I was surprised when I couldn't find code to determine the page count of PDF documents.

I've had to return to my old hacking ways (ie 'hacking' in its original sense). I visited my favorite hacking website - www.wotsit.org, downloaded the PDF document specifications and got coding.

It didn't take too long to construct a reasonably efficient algorithm which does what I want. I've thrown a few PDFs at it over the last couple of days so I hope I've squashed most of the bugs.

Update 22-Dec-2005:

Yes, I did find a couple of minor bugs which have now been corrected.

Code snippet ...

type

PPdfObj = ^TPdfObj;

TPdfObj = record

number,

offset: integer;

end;

function GetPdfPageCount(const filename: string): integer;

var

ms: TMemoryStream;

k, cnt, pagesNum, rootNum: integer;

p, p2: pchar;

PdfObj: PPdfObj;

PdfObjList: TList;

//Summary of steps taken to parse PDF file for page count :-

//1. Locate 'startxref' at end of file

//2. get 'xref' offset and go to xref table

//3. fill my pdfObj List with object numbers and offsets

//4. handle subsections within xref table.

//5. read 'trailer' section at end of each xref

//6. store 'Root' object number if found in 'trailer'

//7. if 'Prev' xref found in 'trailer' - loop back to step 2

//8. locate Root in my full pdfObj List

//9. locate 'Pages' object from Root

//10. get Count from Pages.

function GetNumber(out num: integer): boolean;

var

tmpStr: string;

begin

tmpStr := '';

while p^ < #33 do inc(p); //skip leading CR,LF & SPC

while (p^ in ['0'..'9']) do

begin

tmpStr := tmpStr + p^;

inc(p);

end;

result := tmpStr <> '';

if not result then exit;

num := strtoint(tmpStr);

end;

function IsString(const str: string): boolean;

var

len: integer;

begin

len := length(str);

result := CompareMem( p, pchar(str), len);

inc(p, len);

end;

function FindStrInDict(const str: string): boolean;

begin

//PDF 'dictionaries' (assoc. arrays) terminate with '>>'

result := false;

while not result do

begin

while (p^ <> '>') and (p^ <> str[1]) do inc(p);

if (p^ = '>') then

begin

inc(p);

if (p^ = '>') then exit else continue;

end;

result := IsString(str);

end;

end;

begin

//on error return -1 as page count

result := -1;

try

ms := TMemoryStream.Create;

PdfObjList := TList.Create;

screen.Cursor := crHourGlass;

application.ProcessMessages;

try

ms.LoadFromFile(filename);

//find 'startxref' ignoring '%%EOF'

p := pchar(ms.Memory) + ms.Size -5;

//21-Jun-05: bugfix

//sometimes rubbish is appended to the pdf so

//look deeper for 'startxref'

p2 := pchar(ms.Memory);

repeat

while (p > p2) and (p^ <> 'f') do dec(p);

if (p = p2) then exit;

if StrLComp( (p-8), 'startxref', 9) = 0 then

break;

dec(p);

until false;

inc(p);

rootNum := -1; //ie flag not yet found

//xref offset ==> k

if not GetNumber(k) then exit;

p := pchar(ms.Memory) + k +4;

while true do //top of loop //////////////////////////////

begin

//get base object number ==> k

if not GetNumber(k) then exit;

//get object count ==> cnt

if not GetNumber(cnt) then exit;

while not (p^ in ['0'..'9']) do inc(p); //skip CR, LF

p2 := p;

//add all objects in section to list ...

for cnt := 0 to cnt-1 do

begin

new(PdfObj);

PdfObjList.Add(PdfObj);

PdfObj.number := k + cnt;

if not GetNumber(PdfObj.offset) then exit;

inc(p2,20);

p := p2;

end;

//check for and process further subsections ...

if p^ in ['0'..'9'] then continue;

// parse 'trailer dictionary' ...

if not IsString('trailer') then exit;

p2 := p;

// get Root (aka /Catalog) ...

if (rootNum = -1) and FindStrInDict('/Root') then

if not GetNumber(rootNum) then exit;

p := p2;

if not FindStrInDict('/Prev') then

break; //no more xrefs

//next xref offset ==> k

if not GetNumber(k) then exit;

p := pchar(ms.Memory) + k +4;

end; //bottom of loop /////////////////////////////////////

//Make sure we've got Root the object number ...

if rootNum < 0 then exit;

//Find Root object in list and go to its offset ...

k := 0;

while k < PdfObjList.Count do

if PPdfObj(PdfObjList[k]).number = rootNum then

break else

inc(k);

if k = PdfObjList.Count then exit;

p := pchar(ms.Memory) + PPdfObj(PdfObjList[k]).offset;

//double check that this is the Root object ...

if not GetNumber(k) or (k <> rootNum) then exit;

if not FindStrInDict('/Pages') then exit;

//get Pages object number ==> pagesNum

if not GetNumber(pagesNum) then exit;

k := 0;

while k < PdfObjList.Count do

if PPdfObj(PdfObjList[k]).number = pagesNum then

break else

inc(k);

if k = PdfObjList.Count then exit;

//Pages object found in list, now go to offset ...

p := pchar(ms.Memory) + PPdfObj(PdfObjList[k]).offset;

//make sure it's the Pages object ...

if not GetNumber(k) or (k <> pagesNum) then exit;

if not FindStrInDict('/Count') then exit;

if not GetNumber(cnt) then exit;

//21-Jun-05: bugfix

//occasionally the 'count' value is an indirect object

if GetNumber(k) and IsString(' R') then

begin

//this is an indirect object to the count value,

//so find the obj ...

k := 0;

while k < PdfObjList.Count do

if PPdfObj(PdfObjList[k]).number = cnt then

break else inc(k);

if k = PdfObjList.Count then exit;

p := pchar(ms.Memory) + PPdfObj(PdfObjList[k]).offset;

if not GetNumber(k) or //skip the object num

not GetNumber(k) or //skip the generation num

not IsString(' obj') or

not GetNumber(cnt) then exit;

end;

result := cnt;

finally

screen.Cursor := crDefault;

for k := 0 to PdfObjList.Count -1 do

dispose(PPdfObj(PdfObjList[k]));

PdfObjList.Free;

ms.Free;

end;

except

//nb: errors are flagged by returning -1

end;

end;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有