主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

PDF 和 HTML 文档的部件和空位

以下示例演示如何:

  • 定义一个有空位的文档部件模板。

  • 以编程方式将文档部件插入报告中并填补空位。

  • 插入目录文档部件。

本示例使用 PDF 模板和报告。但是,您可以对 HTML 报告使用相同的过程。在整个示例中将文档类型信息替换为相应的 HTML 信息。

将模板添加到 PDF 文档部件库

在此示例中,从默认的 PDF 模板包开始。

  1. 创建默认模板包的副本。

    mlreportgen.dom.Document.createTemplate("myPDFtemplate","pdf");
  2. 解压模板包。

    unzipTemplate("myPDFtemplate.pdftx");
  3. 在当前文件夹中,打开解压的模板文件夹 myPDFtemplate。在 HTML 或文本编辑器中打开 docpart_templates.html

    dplibrary 元素定义文档部件库。dptemplate 元素定义每个文档部件模板。本文档部件库有两个文档部件模板:

    • rgChapter,定义章的部件模板

    • ReportTOC,定义目录

    <html>
    <body>
       <dplibrary>
    
    	  <dptemplate name="rgChapter">
          <h1 class="rgChapterTitle">
          <hole id="rgChapterTitlePrefix" default-style-name="rgChapterTitlePrefix" />
            <span> </span>
          <hole id="rgChapterTitleNumber" default-style-name="rgChapterTitleNumber" />
            <span>. </span>
          <hole id="rgChapterTitleText" default-style-name="rgChapterTitleText" />
          </h1>
          <hole id="rgChapterContent"/>
         </dptemplate>
        
       <dptemplate name="ReportTOC">
           <TOC number-of-levels ="3" leader-pattern="dots" />
       </dptemplate>
    
       </dplibrary>
    
    </body>
    </html>
    
  4. 创建一个名为 Author 的文档部件模板。文档部件可以包含固定文本和空位的任意组合。该文档部件模板包含固定文本 Author 和作者姓名的空位。

    <dptemplate name="Author">
          <p class="Author">
          <span>Author: </span><hole id="AuthorName" />  
          </p>
                
     </dptemplate>
    
  5. 将新的文档部件模板添加到库中。因为当您从 API 调用它时,您会按名称引用文档部件,所以您可以在库中按任何顺序放置模板。为每个文档部件模板使用唯一的名称。

     <dplibrary>
            
    	  <dptemplate name="rgChapter">
          <h1 class="rgChapterTitle">
          <hole id="rgChapterTitlePrefix" default-style-name="rgChapterTitlePrefix" />
            <span> </span>  
          <hole id="rgChapterTitleNumber" default-style-name="rgChapterTitleNumber" />
            <span>. </span>
          <hole id="rgChapterTitleText" default-style-name="rgChapterTitleText" />
          </h1>
          <hole id="rgChapterContent"/>
       </dptemplate>
       <dptemplate name="ReportTOC">
          <TOC number-of-levels ="3" leader-pattern="dots" />
      </dptemplate>
      <dptemplate name="Author">
          <p class="Author">
          <span>Author: </span><hole id="AuthorName" />  
          </p>
      </dptemplate>
    
    </dplibrary>
    
  6. 将模板重新打包为名为 myPDFtemplate2.pdftx 的新模板。

    zipTemplate("myPDFtemplate2.pdftx","myPDFtemplate");

在报告程序中使用文档部件模板

使用 mlreportgen.dom.DocumentPart 来使用文档部件模板。您需要:

  • 包含文档部件的模板包的名称。在此示例中,模板包名称为 myPDFtemplate2

  • 要调用的文档部件模板的名称以及要填充的任何空位的顺序。在此示例中,您调用:

    • 文档部件模板 rgChapter 并按前缀、编号、标题的顺序填写前三个空空位

    • ReportTOC 文档部件模板,插入目录

    • 您创建的 Author 文档部件模板并填充其一个空位

import mlreportgen.dom.*
d = Document("myDocPartEx","pdf","myPDFtemplate2");
open(d);

% Assign the rgChapter document part template to the variable dp
dp = DocumentPart(d,"rgChapter");

% Move to each hole in this document part and append content 
moveToNextHole(dp);
append(dp,"Chapter");
moveToNextHole(dp);
append(dp,"5");
moveToNextHole(dp);
append(dp,"Creating Document Part Templates");

% Append this document part to the document
append(d,dp);

% Append the document part ReportTOC to the document
append(d,DocumentPart(d,"ReportTOC"));

% You can append any allowable object between document parts or holes
append(d,Paragraph("Append any allowable object or a document part."));
append(d,Paragraph("Append a document part next:"));

% Assign the Author document part template to the variable dp2
dp2 = DocumentPart(d,"Author");

% Move to the next hole and fill it
% Append the document part to the document
moveToNextHole(dp2);
append(dp2,"Charles Brown");
append(d,dp2);

close(d);
rptview(d.OutputPath);

Author 文档部件模板包含位于空位之前的固定文本。moveToNextHole 将模板中前一个空位(或文档部件的开头)和当前空位之间的任何固定内容追加到文档中。

另请参阅

主题