Tuesday, July 21, 2009

VC application architecture

Win32 Application (if you don't want open console window when the application is running, you should not use console application).

Application
Windows class (MainWindows or Some kind of DialogWindows)
Thread Class(you can have several thread runing, controled by windows class);


APIENTRY wMain
Get and save some system information into globle varibles such as windows version, current module directory,cmd argv
Create MainWindow object or MainDialog object and make it showwindows
Enter Message dispatching
Exit

MainWindow or MainDialog
Save some program related parameters in the MainWindow class
create thread (if need)
Process message

Thread
do work
exit.
--------------------------------

Sunday, July 19, 2009

转换spool file 到 PDF RPGIV 源代码 - AS00 RPG编程技巧 - Passthru

转换spool file 到 PDF RPGIV 源代码 - AS00 RPG编程技巧 - Passthru: "转换spool file 到 PDF RPGIV 源代码
转换spool file 到 PDF RPGIV 源代码

******************************************************************
H
* Work files
Fcvtwork02 IF F 382 DISK
Fcvtwork01 UF A F 378 DISK
* Program parameter - report title
D paTitle S 50A
* Program parameter - spooled file information returned by API
D SplInfo DS
D saReturned 10I 0
D saAvailabl 10I 0
D saIntJobId 16A
D saSplfId 16A
D saJobName 10A
D saUser 10A
D saJobNbr 6A
D saSplFile 10A
D saSplNbr 10I 0
D saFormType 10A
D saUsrDta 10A
D saStatus 10A
D saFilAvail 10A
D saHold 10A
D saSave 10A
D siPages 10I 0
D siCurrPage 10I 0
D siFromPage 10I 0
D siToPage 10I 0
D siLastPage 10I 0
D siRestart 10I 0
D siCopies 10I 0
D siCopyRem 10I 0
D siLPI 10I 0
D siCPI 10I 0
D siOutPty 2A
D saOutq 10A
D saOutqLib 10A
D saOpenDate 7A
D saOpenTime 6A
D saPrtFile 10A
D saPrtfLib 10A
D saPgmName 10A
D saPgmLib 10A
D saAcgCode 15A
D saPrtTxt 30A
D siRcdLen 10I 0
D siMaxRcds 10I 0
D saDevType 10A
D saPrtType 10A
D saDocName 12A
D saFlrName 64A
D saS36Proc 8A
D saFidelity 10A
D saRplUnprt 1A
D saRplChar 1A
D siPageLen 10I 0
D siPageWdth 10I 0
D siSepartrs 10I 0
D siOvrFlw 10I 0
D saDBCS 10A
D saDBCSExt 10A
D saDBCSSOSI 10A
D saDBCSRotn 10A
D saDBCSCPI 10I 0
D saGraphics 10A
D saCodePage 10A
D saFormDf 10A
D saFormDfLb 10A
D siDrawer 10I 0
D saFont 10A
D saS36SplId 6A
D siRotation 10I 0
D siJustify 10I 0
D saDuplex 10A
D saFoldRcds 10A
D saCtlChar 10A
D saAlign 10A
D saPrtQlty 10A
D saFormFeed 10A
D saVolumes 71A
D saLabels 17A
D saExchange 10A
D saCharCode 10A
D siTotRcds 10I 0
D siMultiUp 10I 0
D saFrontOvl 10A
D saFrtOvlLb 10A
D snFOOffDwn 15P 5
D snFOOffAcr 15P 5
D saBackOvl 10A
D saBckOvlLb 10A
D snBOOffDwn 15P 5
D snBOOffAcr 15P 5
D saUOM 10A
D saPagDfn 10A
D saPagDfnLb 10A
D saSpacing 10A
D snPointSiz 15P 5
D snFMOffDwn 15P 5
D snFMOffAcr 15P 5
D snBMOffDwn 15P 5
D snBMOffAcr 15P 5
D snPageLen 15P 5
D snPageWdth 15P 5
D saMethod 10A
D saAFP 1A
D saChrSet 10A
D saChrSetLb 10A
D saCdePagNm 10A
D saCdePgeLb 10A
D saCdeFnt 10A
D saCdeFntLb 10A
D saDBCSFnt 10A
D saDBCSFntL 10A
D saUserDef 10A
D saReduce 10A
D saReserv1 1A
D siOutBin 10I 0
D siCCSID 10I 0
D saUserText 100A
D saSystem 8A
D saOrigId 8A
D saCreator 10A
* Program parameter - bookmark option
D paBookmark S 7A
* Program parameter - bookmark *POS option parameters
D BMarkPos DS
D siPosCount 5I 0
D snPosLine 3P 0
D snPosChar 3P 0
D snPosLen 3P 0
* Program parameter - bookmark *KEY option parameters
D BMarkKey DS
D siKeyCount 5I 0
D siLen 5I 0
D saKeyStr 378A
D snKeyOccur 3P 0
D snKeyOff 3P 0
D snKeyLen 3P 0
* PDF 'object' array
D aiObject S 10I 0 DIM(32767)
* Start position of PDF options
D aaStart S 10A DIM(32767)
* Current object number
D wiObject S 10I 0
* Current count of bytes written
D wiChrCount S 10I 0
* Current page number
D wiPage S 10I 0
* Start position of text
D wiStart S 10I 0
* Bookmark text
D waBookmark S 378A
* Count of occurrences of the bookmark key
D wiOccurs S 5I 0
* Input spooled file data including control characters
D InputData DS
D saSkipLine 3A
D ssSkipLine 3S 0 OVERLAY(saSkipLine:1)
D saSpceLine 1A
D ssSpceLine 1S 0 OVERLAY(saSpceLine:1)
D saInput 378A
* Output PDF-format data
D OutputData DS
D saOutput 378A
* Procedure prototypes
D WritePDF PR
D iaOutput 378A CONST OPTIONS(*VARSIZE)
D AddEscape PR 378A
D iaInput 378A
D PDFHeader PR
D PDFPages PR
D PDFTrailer PR
D NewPage PR
D EndPage PR
D NumToText PR 10A
D iiNum 10I 0 CONST
D NewObject PR
* Program parameters
C *ENTRY PLIST
C PARM paTitle
C PARM SplInfo
C PARM paBookmark
C PARM BMarkPos
C PARM BMarkKey
* Output a PDF header
C CALLP PDFHeader
* Create PDF page 'objects'
C CALLP PDFPages
* Output a PDF trailer
C CALLP PDFTrailer
C RETURN
**********************************************************************
* Procedure to create a PDF 'header' *
**********************************************************************
P PDFHeader B
D PDFHeader PI
D liPage S 10I 0
D liPageObj S 10I 0
* Create catalog object
C CALLP WritePDF('%PDF-1.0')
C CALLP WritePDF('%忏嫌')
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF('<<')
C CALLP WritePDF('/Type /Catalog')
C CALLP WritePDF('/Pages 5 0 R')
C CALLP WritePDF('/Outlines 2 0 R')
C CALLP WritePDF('/PageMode /UseOutlines')
C CALLP WritePDF('>>')
C CALLP WritePDF('endobj')
* Create outlines object
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF('<<')
C CALLP WritePDF('/Type /Outlines')
C CALLP WritePDF('/Count '+%trim(NumToText(siPages)))
C CALLP WritePDF( '/First 9 0 R')
C
C CALLP WritePDF( '/Last '
C + %trim(NumToText((siPages*4)+5))
C + ' 0 R')
C CALLP WritePDF('>>')
C CALLP WritePDF('endobj')
* Create procedures object
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF('[/PDF /Text]')
C CALLP WritePDF('endobj')
* Create fonts object
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF('<<')
C CALLP WritePDF ('/Type /Font')
C CALLP WritePDF ('/Subtype /Type1')
C CALLP WritePDF ('/Name /F1')
C CALLP WritePDF ('/BaseFont /Courier')
C CALLP WritePDF ('/Encoding /WinAnsiEncoding')
C CALLP WritePDF ('>>')
C CALLP WritePDF ('endobj')
* Create pages object
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF ('<<')
C CALLP WritePDF ('/Type /Pages')
C CALLP WritePDF('/Count '+%trim(NumToText(siPages)))
* Write list of child pages
C EVAL liPage = wiObject + 1
C EVAL liPageObj = liPage
C CALLP WritePDF ( '/Kids ['
C + %trim(NumToText(liPage))
C + ' 0 R')
C DOW liPage < siPages + wiObject
C EVAL liPage = liPage + 1
C EVAL liPageObj = liPageObj + 4
C CALLP WritePDF ( ' '
C + %trim(NumToText(liPageObj))
C + ' 0 R')
C ENDDO
C CALLP WritePDF (' ]')
C CALLP WritePDF ('>>')
C CALLP WritePDF ('endobj')
P PDFHeader E
**********************************************************************
* Procedure to create PDF pages *
**********************************************************************
P PDFPages B
D liLine S 10I 0
D liLength S 5I 0
D liChar S 5I 0
D liX S 5I 0
D liY S 5I 0
* Create page object for first page
C EVAL wiPage = 0
C EVAL liX = 0
* Read spooled file data from input work file
C READ cvtwork02 InputData LR
C DOW *INLR = *OFF
* Skip to a line if specified, handling page throw if it occurs
C IF saSkipLine <> *BLANKS
C IF ssSkipLine < liLine or liLine = 0
C IF wiPage <> 0
C CALLP EndPage
C ENDIF
C CALLP NewPage
C EVAL liLine = ssSkipLine
C EVAL liY
C = (612/siPageLen) * (siPagelen-liLine)
C ELSE
C EVAL liY
C = -((612/siPageLen) * (ssSkipLine-liLine))
C EVAL liLine = ssSkipLine
C ENDIF
C ENDIF
* Space a number of lines if specified
C IF saSpceLine <> *BLANKS
C EVAL liLine = liLine + ssSpceLine
C EVAL liY
C = -((612/siPageLen) * ssSpceLine)
C ENDIF
* Set up bookmark if position option specified
C IF paBookmark = '*POS'
C IF liLine = snPosLine and waBookmark = *BLANKS
C EVAL waBookmark = %trim(%subst(saInput :
C snPosChar:
C snPosLen ))
C ENDIF
C ENDIF
* Set up bookmark if key option specified
C IF paBookmark = '*KEY'
C saKeyStr:siLenSCAN saInput:1 liChar
C IF liChar > 0
C EVAL wiOccurs = wiOccurs + 1
C IF wiOccurs = snKeyOccur
C EVAL liChar = liChar + snKeyOff
C EVAL liLength = snKeyLen
C IF liChar + liLength > siPageWdth
C EVAL liLength = siPageWdth - liChar
C ENDIF
C IF liChar < 1
C EVAL liChar = 1
C ENDIF
C IF liChar + liLength <= siPageWdth
C EVAL waBookmark = %trim(%subst(saInput :
C liChar :
C liLength ))
C ENDIF
C ENDIF
C ENDIF
C ENDIF
* Add escape character before special characters \, ( and )
C EVAL saInput = AddEscape(saInput)
* Output the line of text
C CALLP WritePDF( %trim(NumToText(liX))
C + ' '
C + %trim(NumToText(liY))
C + ' Td ('
C + %trimr(saInput)
C + ') Tj')
C READ cvtwork02 InputData LR
C ENDDO
C CALLP EndPage
P PDFPages E
**********************************************************************
* Procedure to create a PDF trailer *
**********************************************************************
P PDFTrailer B
D PDFTrailer PI
D laDateTime S 14A
D i S 10I 0
D liXRef S 10I 0
* Create information object
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF('<<')
C CALLP WritePDF( '/Creator ('
C + %trim(saPgmLib)
C + '/'
C + %trim(saPgmName)
C + ')' )
C IF %subst(saOpenDate:1:1) = '0'
C EVAL laDateTime = '19' + %subst(saOpenDate:2:6)
C + saOpenTime
C ELSE
C EVAL laDateTime = '20' + %subst(saOpenDate:2:6)
C + saOpenTime
C ENDIF
C CALLP WritePDF( '/CreationDate (D:'
C + laDateTime + ')')
C CALLP WritePDF('/Title (' + %trim(paTitle) + ')')
C CALLP WritePDF('/Producer (CVTSPLPDF)')
C CALLP WritePDF('/Keywords ()')
C CALLP WritePDF( '/Author ('
C + %trim(saJobNbr)
C + '/'
C + %trim(saUser)
C + '/'
C + %trim(saJobName)
C + ')' )
C CALLP WritePDF('>>')
C CALLP WritePDF('endobj')
* Create cross-reference
C EVAL liXref = wiChrCount - 1
C CALLP WritePDF('xref 0 '
C + %trim(NumToText(wiObject+1)) )
C CALLP WritePDF('0000000000 65535 f')
C DO wiObject i
C CALLP WritePDF(aaStart(i) + ' 00000 n')
C ENDDO
* Write trailer
C CALLP WritePDF('trailer')
C CALLP WritePDF('<<')
C CALLP WritePDF('/Size '
C + %trim(NumToText(wiObject+1)))
C CALLP WritePDF('/Root 1 0 R')
C CALLP WritePDF('/Info '
C + %trim(NumToText(wiObject))
C + ' 0 R')
C CALLP WritePDF('>>')
C CALLP WritePDF('startxref')
C CALLP WritePDF(%trim(NumToText(liXref)))
C CALLP WritePDF('%%EOF')
P PDFTrailer E
**********************************************************************
* Procedure to create a new PDF 'object' *
**********************************************************************
P NewObject B
D NewObject PI
D lsDataLen S 10S 0
D i S 10I 0
C EVAL wiObject = wiObject + 1
C EVAL i = wiObject
C EVAL lsDataLen = wiChrCount
C MOVE lsDataLen aaStart(i)
P NewObject E
**********************************************************************
* Procedure to output PDF data
**********************************************************************
P WritePDF B
D WritePDF PI
D iaOutput 378A CONST OPTIONS(*VARSIZE)
D liLength S 5I 0
* Update byte count with length of data to be written
C ' ' CHECKR iaOutput liLength
C EVAL wiChrCount= wiChrCount + liLength + 2
* Output data to work file
C EVAL saOutput = %trimr(iaOutput)
C WRITE cvtwork01 OutputData
P WritePDF E
**********************************************************************
* Procedure to convert a number to text *
**********************************************************************
P NumToText B
D NumToText PI 10A
D iiNum 10I 0 CONST
D laSign S 1A
D laInput S 10A
D laOutput S 10A
D liIn S 5I 0
D liOut S 5I 0
D liNum S 10I 0
* Set up sign if and make number positive if number is negative
C IF iiNum < 0
C EVAL laSign = '-'
C EVAL liNum = -iiNum
C ELSE
C EVAL laSign = ' '
C EVAL liNum = iiNum
C ENDIF
* Number number to work character variable
C MOVE liNum laInput
* Skip over leading zeros
C EVAL liIn = 1
C EVAL liOut = 1
C DOW liIn < %size(laInput)
C and %subst(laInput:liIn:1) = '0'
C EVAL liIn = liIn + 1
C ENDDO
* Move digits to output area
C DOW liIn <= %size(laInput)
C and liOut <= %size(laOutput)
C EVAL %subst(laOutput:liOut:1)
C = %subst(laInput :liIn :1)
C EVAL liIn = liIn + 1
C EVAL liOut = liOut + 1
C ENDDO
* Add sign
C IF laSign = '-'
C EVAL laOutput = laSign + laOutput
C ENDIF
* Return number in text format
C RETURN laOutput
P NumToText E
**********************************************************************
* Procedure to add an escape character before special characters *
**********************************************************************
P AddEscape B
D AddEscape PI 378A
D iaInput 378A
D laOutput S 378A
D laChar S 1A
D i S 5I 0
D o S 5I 0
D liLength S 5I 0
* Determine length of input data
C ' ' CHECKR iaInput liLength
* Work through input data and prefix special characters with escape
C EVAL i = 1
C EVAL o = 0
C DOW i <= liLength
C EVAL laChar = %subst(iaInput:i:1)
C IF laChar = '\' or laChar = '(' or laChar = ')'
C EVAL o = o + 1
C EVAL %subst(laOutput:o:1) = '\'
C ENDIF
C EVAL o = o + 1
C EVAL %subst(laOutput:o:1) = laChar
C EVAL i = i + 1
C ENDDO
C RETURN laOutput
P AddEscape E
**********************************************************************
* Procedure to create a new page object *
**********************************************************************
P NewPage B
D NewPage PI
* Create a page object
C EVAL wiPage = wiPage + 1
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF('<<')
C CALLP WritePDF('/Type /Page')
C CALLP WritePDF('/Parent 5 0 R')
C CALLP WritePDF( '/Resources << /Font <<'
C + ' /F1 4 0 R >>'
C + ' /ProcSet 3 0 R >>')
C CALLP WritePDF('/MediaBox [0 0 792 612]')
C CALLP WritePDF( '/Contents '
C + %trim(NumToText(wiObject+1))
C + ' 0 R')
C CALLP WritePDF('>>')
C CALLP WritePDF('endobj')
* Set up bookmark if *PAGNBR option specified
C IF paBookmark = '*PAGNBR'
C EVAL waBookmark = 'Page '
C + %trim(NumToText(wiPage))
C ELSE
C EVAL waBookmark = *BLANKS
C EVAL wiOccurs = 0
C ENDIF
* Create a stream object
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF( '<< /Length '
C + %trim(NumToText(wiObject+1))
C + ' 0 R >>')
C CALLP WritePDF('stream')
C EVAL wiStart = wiChrCount
C CALLP WritePDF('BT')
* Determine font size to use from Characters per inch setting
C SELECT
C WHEN siCPI = 50
C CALLP WritePDF('/F1 20 Tf')
C WHEN siCPI = 120
C CALLP WritePDF('/F1 9 Tf')
C WHEN siCPI = 150
C CALLP WritePDF('/F1 8 Tf')
C WHEN siCPI = 167
C CALLP WritePDF('/F1 6 Tf')
C OTHER
C CALLP WritePDF('/F1 10 Tf')
C ENDSL
P NewPage E
**********************************************************************
* Procedure to finish a page object *
**********************************************************************
P EndPage B
D EndPage PI
D liLength S 10I 0
* End text stream
C CALLP WritePDF('ET')
C EVAL liLength = wiChrCount- wiStart
C CALLP WritePDF('endstream')
C CALLP WritePDF('endobj')
* Create indirect length object for stream
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF(%trim(NumToText(liLength)))
C CALLP WritePDF('endobj')
* Create outline object
C EVAL waBookmark = AddEscape(waBookMark)
C CALLP NewObject
C CALLP WritePDF(%trim(NumToText(wiObject))+' 0 obj')
C CALLP WritePDF('<<')
C CALLP WritePDF('/Parent 2 0 R')
C CALLP WritePDF( '/Title ('
C + %trimr(waBookmark) + ')')
C IF wiPage > 1
C CALLP WritePDF( '/Prev '
C + %trim(NumToText(wiObject-4))
C + ' 0 R')
C ENDIF
C IF wiPage < siPages
C CALLP WritePDF( '/Next '
C + %trim(NumToText(wiObject+4))
C + ' 0 R')
C ENDIF
C CALLP WritePDF('/Dest ['
C + %trim(NumToText(wiObject-3))
C + ' 0 R /XYZ 0 792 0]')
C CALLP WritePDF('>>')
C CALLP WritePDF('endobj')
P EndPage E"

希望PDF之家-技术文章-PDF开发篇 -一个简单的PDF文件结构的分析

希望PDF之家-技术文章-PDF开发篇 -一个简单的PDF文件结构的分析: "一个简单的PDF文件结构的分析

发布日期:2006-4-14 作者:pdfMaker 出处:CSDN

Adobe的PDF参考告诉我们一个PDF文件可以通过下面4个方面来理解:
1. 对象, 一个PDF文档是由一个由基本数据类型组成的数据结构。
2. 文件(物理结构), 决定对象是如何存放在一个PDF文件中的, 它们是如何被访问的,如何被更新的。这个结构是独立于对象的语义的。
3. 文档结构, 说明一些基本的对象类型是如何来表现PDF文档的成分的:页,字体,批注,和另外一些内容。
4. 内容流.一个PDF文件内容流包含一系列的指令,描述页面的外观或其他图形实体的外观和文件内容。
但是当时对我来说要看懂这几行字是有很大的困难的,需要了解确切含义,必须看完后面的几十页上百页的内容并且要分析一下一个实际的PDF文件才能完全领会它的意思。
后来经过长时间的文档阅读,相关开发,并且具体地分析PDF文件后才把PDF文件的语法,文件的解析搞清楚。虽然说学习是痛并快乐着,但是对于当时我来说真的希望有一个人能够告诉我一个简单的例子,通过一个简单的例子来描述PDF的基本组成,它的解析原理和过程。因此下面我主要将以一个简单的例子来说明PDF的主要特性并给出一个简单的PDF文件的全景。
在继续阅读该文章前,我们先问自己下面的几个问题:
l 你了解至少一种文件格式吗?(例如HTML)
l 为什么要学习PDF的相关知识?
如果你对第一个问题的答案为“是”, 并且第二个问题你能给出一个非常明确的答案,那么这篇短文是适合你的。否则,如果对任何一种格式都不了解,建议先了解一下HTML,或XML,你可以从这两种语言里得到很多启发,对学习PDF的构成有很大的好处;如果你不清楚你要学习是为了什么,那么我就认为你学习没有目的性和动力,说不定你今天学了以后明天就忘得一干二净。
1.PDF格式和HTML,XML格式:
一个PDF文档从根本上来说是一个8字节序。其实PDF格式和我们已经熟知的HTML,XML等结构化的文件格式一样,包含有关键字,分隔符,数据等等。
不同的是PDF文件是按照二进制流的方式保存的,而html文件则是文本方式保存的。XML文件一般只包含数据本身,并没有把如何显示的信息放在其中,因此要显示一个XML文件还需要一个Schema文件才能显示,否则看到的将是所有的字节流;HTML包含了数据的同时也包含了一些关于如何显示的信息,但是HTML是基于文本存放的,是可读的,你打开一个HTML文件就能知道所有显示在浏览器里得文字。 另外就是HTML不能包含二进制流,它对图像文件的引用都是通过链接的,全部是外部文件的方式来实现的。
2.PDF规范的发展
PDF规范从1993年到现在,已经有过7个版本,六次版本升级,从最初的pdf1.0.6版本到现在的PDF1.6, 每次的版本升级都会加入一些新的特性,PDF参考说明书也是从最初的100多页到现在的1000多页,但是PDF文件格式的主要特性还是没有改变,可以这么理解,PDF1.6是PDF1.0的扩展集,学习了PDF1.0以后也能基本上理解PDF1.6的内容。 因此说我下面的例子是基于一个PDF1.0的最简单的一个PDF文件的分析。
PDF规范的发展升级:
1.1 1995 加入了文档加密(40字节),线索树,名字树,链接,设备独立色彩资源。
1.2 1996 表单, 半色调屏幕,和其他的一些高级色彩特性, 对中文,日文和韩文的支持
1.3 2000 数字签名, 逻辑结构, JavaScript, 嵌入式文件,Masked Images, 平滑阴影, 支持 CID字体的附加色彩。
1.4 2001 文件加密 (128 字节), 标签式 PDF, 访问控制,透明,元数据流
1.5 2003 文档加密 (公钥), JPEG 2000 压缩, 可选的内容组,附加的注解类型
1.6 2005 文档加密 (AES),增加最大文件支持,加入3D支持,额外的注解类型

3.PDF文件的基本组成:
一个PDF文件从大的方面来说分4个部分:
l 文件头,指明了该文件所遵从的PDF规范的版本号,它出现在PDF文件的第一行。
l 文件体,PDF文件的主要部分,由一系列对象组成。
l 交叉引用表,为了能对间接对象进行随机存取而设立的一个间接对象的地址索引表。
l 文件尾,声明了交叉引用表的地址,即指明了文件体的根对象(Catalog),从而能够找到PDF文件中各个对象体的位置,达到随机访问。另外还保存了PDF文件的加密等安全信息(以后详细讨论)。
如下图:

图1

4.PDF文档的逻辑结构
作为一种结构化的文件格式,一个PDF文档是由一些称为“对象”的模块组成的。并且每个对象都有数字标号,这样的话可以这些对象就可以北其他的对象所引用。这些对象不需要按照顺序出现在PDF文档里面,出现的顺序可以是任意的,比如一个PDF文件有3页,第3页可以出现在第一页以前,对象按照顺序出现唯一的好处就是能够增加文件的可读性,如果你不会用文本编辑器来阅读PDF结构,那么大可不必关心。正是因为页与页之间的不相关性,就可以对PDF文件的页码进行随机的访问。
文件尾(Trail),说明根对象的对象号,并且说明交叉引用表的位置,通过对交叉引用表的查询可以目录对象(Catalog)。这个目录对象是该PDF文档的根对象,包含PDF文档的大纲(outline)和页面组对象(pages)引用。大纲对象是指PDF文件的书签树;页面组对象(pages)包含该文件的页面数,各个页面对象(page)的对象号。
一个PDF文档有下图所示的层次关系:

图2

页面(page)对象作为PDF中最重要的对象,包含如何显示该页面的信息,例如使用的字体,包含的内容(文字,图片等),页面的大小。当然里面的子项也可以是其他对象的引用。页面中包含的信息是包含在一个称为流(stream)的对象里,这个流的长度(字节数)必须直接给出或指向另外一个对象。如下图:

图3

5. PDF的基本语法:
文件的第一行是文件头,指明了该文件所遵从的PDF规范的版本号,它出现在PDF文件的第一行。
一个对象的第一行一般有两个数字和关键字“obj”。例如:

3 0 obj
<<

/Type /Pages
/Count 1
/Kids [4 0 R]
>>

endobj

第一个数字称为对象号,来唯一标识一个对象的,第二个是产生号,是来表明它在被创建后的第几次修改,所有新创建的PDF文件的对象号应该都是0,即第一次被创建以后没有被修改过。上面的例子就说明该对象的对象号是3,而且创建后没有被修改过。
对象的内容应该是包含在<< 和>>之间的,最后以关键字endobj结束.

6. 文件Hello World的文件分析:
6.1.文件的具体分析
%PDF-1.0
文件头,说明符合PDF1.0规范

1 0 obj
<<

/Type /Catalog
/Pages 3 0 R
/Outlines 2 0 R
>>

endobj
Catalog对象(根对象)

2 0 obj
<<

/Type /Outlines
/Count 0
>>

endobj
outline对象(此处它的计数为0,说明没有书签)

3 0 obj
<<

/Type /Pages
/Count 1
/Kids [4 0 R]
>>

endobj
pages对象(页面组对象),/Type /Pages 说明自身的属性,对象的类型为页码,/Count 1说明页码数量为1,/Kids [4 0 R]说明页的对象为4, 这里要说明的是如果有多个页面,就多个页面直接连续下去,比如说/Kids [4 0 R 10 0 R], 就说明该PDF的第一页的对象号是4,第二页的对象号是10。

4 0 obj
<<

/Type /Page
/Parent 3 0 R
/Resources << /Font << /F1 7 0 R >> /ProcSet 6 0 R >>
/MediaBox [0 0 612 792]
/Contents 5 0 R
>>

endobj
页对象,/Parent 3 0 R说明其父对象的对象号为3,/Resources << /Font << /F1 7 0 R >> /ProcSet 6 0 R >>说明该页所要包含的资源,包括字体和内容的类型,/MediaBox [0 0 612 792]说明页面的显示大小(以象素为单位),/Contents 5 0 R说明页面内容对象的对象号为5。

5 0 obj
<< /Length 44 >>
stream
BT
/F1 24 Tf
100 100 Td (Hello World) Tj
ET
endstream
endobj

<< /Length 44 >>说明stream对象为字节数,从BT开始,ET结束,包括中间的行结束符。
Stream说明一个流对象的开始。
BT说明一个文字对象的开始。
/F1 24 Tf,Tf说明True font对象,字体明为F1, 大小为24个象素。
100 150 Td (Hello World) Tj,100 100 说明这一行文字放置的位置,对于Td, 我们可以这样理解,我们的当前X,Y坐标分别加上100和150就是文本的位置,因为在该例子中只有一个对象,那么它的位置就是(100,150), 如果下个对象位置信息为100, 50 Td, 那么它的位置应该就是(100+100, 150+50)也就是(200,200)。(Hello World) Tj说明文本的内容,当然,如果这里是文本的内容可以写成16进制,用<>包含。
ET说明文字对象的结束
endstream流对象的结束

6 0 obj
[/PDF /Text]
Endobj
[/PDF /Text]说明PDF的内容类型仅仅为文本,如果有图片则为[/PDF /Image]

7 0 obj
<<

/Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Helvetica
>>

endobj
Object six defines the
字体对象,不再多作解释。

所有的对象之后是下面的交叉引用表:
xref
0 8
0000000000 65535 f
0000000009 00000 n
0000000074 00000 n
0000000120 00000 n
0000000179 00000 n
0000000322 00000 n
0000000415 00000 n
0000000445 00000 n
xref说明一个交叉引用表的开始,交叉引用表的第一行0 8 说明下面各行所描述的对象号是从0开始,并且有8个对象。
0000000000 65535 f,一般每个PDF文件都是以这一行开始交叉应用表的,说明对象0的起始地址为0000000000,产生号(generation number)为65535,也是最大产生号,不可以再进行更改,而且最后对象的表示是f,表明该对象为free, 这里,大家可以看到,其实这个对象可以看作是文件头。
0000000009 00000 n就是表示对象1,也就是catalog对象了,0000000009是其偏移地址,00000为5位产生号(最大为65535),0表明该对象未被修改过, n表示该对象在使用,区别与自由对象,不可以更改。
下面的几行相信大家就可以告诉我含义了。

Trailer
<<

/Size 8
/Root 1 0 R
>>

startxref
553
%%EOF
trailer
说明文件尾trailer对象的开始。
/Size 8说明该PDF文件的对象数目。
/Root 1 0 R说明根对象的对象号为1。
Startxref
553说明交叉引用表的偏移地址,从而可以找到PDF文档中所有的对象的相对地址,进而访问对象。
%%EOF为文件结束标志。

6.2.PDF解析过程

图4

7.结束语:
到这里,我们对一个最简单的PDF文件的介绍就结束了,我想大家对PDF文件的格式和特定应该已经有所了解了。
当然,我这里介绍的是不完整的,完整的信息,请访问adobe的网站下载:
http://partners.adobe.com/public/developer/pdf/index_reference.html
下次介绍PDF的加密过程及原理。"

用C#实现生成PDF文档(原码):数据挖掘研究院( China Data Mining Research ,ChinaKDD)

用C#实现生成PDF文档(原码):数据挖掘研究院( China Data Mining Research ,ChinaKDD): "用C#实现生成PDF文档(原码)

//write by wenhui.org
using System;
using System.IO;
using System.Text;
using System.Collections;

namespace PDFGenerator
{

public class PDFGenerator
{
static float pageWidth = 594.0f;
static float pageDepth = 828.0f;
static float pageMargin = 30.0f;
static float fontSize = 20.0f;
static float leadSize = 10.0f;


static StreamWriter pPDF=new StreamWriter('E:\myPDF.pdf');

static MemoryStream mPDF= new MemoryStream();

static void ConvertToByteAndAddtoStream(string strMsg)
{
Byte[] buffer=null;
buffer=ASCIIEncoding.ASCII.GetBytes(strMsg);
mPDF.Write(buffer,0,buffer.Length);
buffer=null;
}

static string xRefFormatting(long xValue)
{
string strMsg =xValue.ToString();
int iLen=strMsg.Length;
if (iLen<10)
{
StringBuilder s=new StringBuilder();
int i=10-iLen;
s.Append(′0′,i);
strMsg=s.ToString() + strMsg;
} 同徽B2B电子商务研究中心
return strMsg;
}

static void Main(string[] args)
{
ArrayList xRefs=new ArrayList();
//Byte[] buffer=null;
float yPos =0f;
long streamStart=0;
long streamEnd=0;
long streamLen =0;
string strPDFMessage=null;
//PDF文档头信息
strPDFMessage='%PDF-1.1 ';
ConvertToByteAndAddtoStream(strPDFMessage);

xRefs.Add(mPDF.Length);
strPDFMessage='1 0 obj ';
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage='<< /Length 2 0 R >> ';
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage='stream ';
ConvertToByteAndAddtoStream(strPDFMessage);
////////PDF文档描述
streamStart=mPDF.Length;
//字体
strPDFMessage='BT /F0 ' + fontSize +' Tf ';
ConvertToByteAndAddtoStream(strPDFMessage);
//PDF文档实体高度
yPos = pageDepth - pageMargin;
strPDFMessage=pageMargin + ' ' + yPos +' Td ' ;
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage= leadSize+' TL ' ;

数据挖掘商道

ConvertToByteAndAddtoStream(strPDFMessage);

//实体内容
strPDFMessage= '(http://www.wenhui.org)Tj ' ;
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage= 'ET ';
ConvertToByteAndAddtoStream(strPDFMessage);
streamEnd=mPDF.Length;

streamLen=streamEnd-streamStart;
strPDFMessage= 'endstream endobj ';
ConvertToByteAndAddtoStream(strPDFMessage);
//PDF文档的版本信息
xRefs.Add(mPDF.Length);
strPDFMessage='2 0 obj '+ streamLen + ' endobj ';
ConvertToByteAndAddtoStream(strPDFMessage);

xRefs.Add(mPDF.Length);
strPDFMessage='3 0 obj <</Type/Page/Parent 4 0 R/Contents 1 0 R>> endobj ';
ConvertToByteAndAddtoStream(strPDFMessage);

xRefs.Add(mPDF.Length);
strPDFMessage='4 0 obj <</Type /Pages /Count 1 ';
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage='/Kids[ 3 0 R ] ';
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage='/Resources<</ProcSet[/PDF/Text]/Font<</F0 5 0 R>> >> '; 同徽B2B电子商务研究中心
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage='/MediaBox [ 0 0 '+ pageWidth + ' ' + pageDepth + ' ] >> endobj ';
ConvertToByteAndAddtoStream(strPDFMessage);

xRefs.Add(mPDF.Length);
strPDFMessage='5 0 obj <</Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>> endobj ';
ConvertToByteAndAddtoStream(strPDFMessage);

xRefs.Add(mPDF.Length);
strPDFMessage='6 0 obj <</Type/Catalog/Pages 4 0 R>> endobj ';
ConvertToByteAndAddtoStream(strPDFMessage);

streamStart=mPDF.Length;
strPDFMessage='xref 0 7 0000000000 65535 f ';
for(int i=0;i<xRefs.Count;i++)
{
strPDFMessage+=xRefFormatting((long) xRefs[i])+' 00000 n ';
}
ConvertToByteAndAddtoStream(strPDFMessage);
strPDFMessage='trailer << /Size '+ (xRefs.Count+1)+' /Root 6 0 R >> ';
ConvertToByteAndAddtoStream(strPDFMessage);

strPDFMessage='startxref ' + streamStart+' %%EOF ';
ConvertToByteAndAddtoStream(strPDFMessage);

同徽B2B电子商务研究中心

mPDF.WriteTo(pPDF.BaseStream);

mPDF.Close();
pPDF.Close();
}
}"

Friday, July 17, 2009

VC express 编译错误问题

如不用MFC 则所有project中都设置不使用MFC,而是标准windows库,删除头文件afx....h之流的东西,否则编译时会报错,查找相当不易。