2014-01-14 68 views
3

我正在使用ABCpdf 9.1 x64 .Net和Coldfusion来创建基于HTML内容的PDF。每个PDF文档都有一个不同的页眉和页脚,这些页面和页脚都是用一些Coldfusion代码生成的。页眉对于每页都略有不同的页面是相同的(因为它显示了页码)。这里是我的代码的主要部分:ABCpdf复制页眉和页脚

// add content 
theDoc.Get_Rect().Set_String("67 80 573 742"); 
theContentID = theDoc.AddImageHTML(pdfContent); 

while (true) { 
    if (!theDoc.Chainable(theContentID)) { 
     break; 
    } 
    theDoc.Set_Page(theDoc.AddPage()); 
    theContentID = theDoc.AddImageToChain(theContentID); 
} 

// add header & footer on each page 
for (i=1; i <= theDoc.Get_PageCount(); i++) { 
    // set page 
    theDoc.Set_PageNumber(i); 

    // HEADER 
    theDoc.Get_Rect().Set_String("67 755 573 809"); 
    theDoc.AddImageHTML(headerContent); 

    // FOOTER 
    theDoc.Get_Rect().Set_String("67 0 573 65"); 
    theDoc.AddImageHTML(replace(footerContent, "[page]", i)); 
} 

正如你所看到的,AddImageHTML()方法被调用的每一页,一次针对内容的2倍。所以,如果我有创建6页的内容,该方法被调用13次。这并不理想,因为这种方法花费了很多时间。

是否有一种更有效的方法来从HTML添加页眉和页脚?有一种方法AddImageCopy(),但它不适用于由AddImageHtml()创建的对象。

只是为了理解:那些getter和setter方法由Coldfusion创建以访问.Net属性。

回答

1
  1. 如果你的HTML相对简单,不依赖于CSS,你或许可以调整为HTML样式的文本,并使用使用AddHtml代替AddImageHtmlAddHtml应该比AddImageHtml快得多。作为一个好处,如果需要,您将能够使用引用的(不是系统安装的)字体和CMYK颜色。

  2. 因为你的头是在每一页上是相同的,也许你可以辅助Doc对象使用AddImageHtml,然后添加为每个页面上的图像。这会将标题的调用从每页一个切换到每个文件只有一个。

  3. 由于页脚在每个页面上都不相同,因此我不了解如何避免在每页上调用

+0

这种方法,把标题是一样的。谢谢! – android

0

我曾经穿过这也是我从WebSupergoo得到了答案的所有页面

doc.PageNumber = 1; 
doc.Rect.Rectangle = headerRect; //headerrect should define the rect where the header is 
doc.AddImageHtml(headerHtml); //perform addimage html once 

//repeat for other pages (clones the header. much faster than calling addImageHtml every time) 
for (int i = 1; i <= doc.PageCount; i++) 
    { 
    doc.PageNumber = i; 
     doc.AddImageDoc(doc, 1, doc.Rect); 
    }