我找到了解决方案,但这并不容易:
假设我们有 3 个未定义长度的部分
<cfsavecontent variable="section1">
<p>This is section 1</p>
</cfsavecontent>
<cfsavecontent variable="section2">
<p>This is section 2</p>
</cfsavecontent>
<cfsavecontent variable="section3">
<p>This is section 3</p>
</cfsavecontent>
<cfset sectionList = 'section1,section2,section3'>
<cfset bookmarkList = "">
<cfset content = "">
<cfset currentPage = 1>
<cfloop list="#sectionList#" index="i">
<cfdocument name="infoPdf" format="PDF" bookmark="false">
<cfdocumentsection>
<cfoutput>#content#</cfoutput>
<h2>FakeHeader</h2>
</cfdocumentsection>
</cfdocument>
获取页面信息以了解内容将在哪个页面
<cfpdf action="getinfo" name="pdfInfo" source="infoPdf">
<cfset currentPage = pdfInfo.TotalPages>
<cfset bookmarkList = listAppend(bookmarkList, currentPage)>
<cfset content &= VARIABLES[i]>
<cfdocument name="myPdf" format="PDF" bookmark="false">
<cfdocumentsection>
<cfoutput>#content#</cfoutput>
</cfdocumentsection>
</cfdocument>
</cfloop>
<cfset fileWrite(ExpandPath("test.pdf"),myPdf)>
为 DDX 操作定义 bookmark.xml
<cfxml variable="bookmarks">
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Bookmarks xmlns="http://ns.adobe.com/pdf/bookmarks" version="1.0">
<cfoutput>
<cfloop from="1" to="#listlen(sectionList)#" index="i">
<Bookmark>
<Title>#ListGetAt(sectionList,i)#</Title>
<Dest>
<Fit PageNum="#ListGetAt(bookmarkList,i)-1#"/>
</Dest>
</Bookmark>
</cfloop>
</cfoutput>
</Bookmarks>
</cfxml>
<cfset fileWrite(ExpandPath("/bookmarks.xml"),bookmarks)>
DDX 文件
<cfsavecontent variable="myDDX">
<DDX xmlns="http://ns.adobe.com/DDX/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd">
<PDF result="Out1">
<PDF source="test"/>
<Bookmarks source="inputxml"/>
</PDF>
<PDF result="Out">
<TableOfContents includeInTOC="false" bookmarkTitle="Table of Contents" maxBookmarkLevel="infinite">
<TableOfContentsEntryPattern applicableLevel="all">
<StyledText>
<p font-family="Arial" font-size="11pt">
<_BookmarkTitle/>
<Space/>
<Space/>
<leader leader-pattern="dotted"/>
<Space/>
<Space/>
<_BookmarkPageCitation/>
</p>
</StyledText>
</TableOfContentsEntryPattern>
</TableOfContents>
<PDF source="Out1"/>
</PDF>
</DDX>
</cfsavecontent>
DDX处理
<cfset inputStruct = StructNew()>
<cfset inputStruct.test = 'test.pdf'>
<cfset inputStruct.inputxml = "bookmarks.xml"/>
<cfset outputStruct = StructNew()>
<cfset outputStruct.Out = "CombinedDocument.pdf">
<cfpdf action="processddx" ddxfile="#myddx#" inputfiles="#inputStruct#" outputfiles="#outputStruct#" name="ddxVar">
<cfpdf action="read" source="CombinedDocument.pdf" name="resultPdf">
<cfprocessingdirective suppressWhitespace="true">
<cfcontent type="application/pdf" reset="true" variable="#tobinary(resultPdf)#"/>
</cfprocessingdirective>