2011-04-07 88 views
1


我想创建一个vb应用程序,它以xml作为输入来创建pdf文档。我想获得目录使用iTextSharp的的章节和部分功能的格式如下使用itextsharp和vb.net创建PDF文档的页面内容目录2005

 
heading1 ----------------page number 
    heading2---------------page number 
heading3-----------------page number

创建PDF,所有我能得到的是

 
heading1 
    heading2 
heading3 

任何人都可以帮助我获得适当条目旁边的页码..!??

感谢,
阿迪亚

回答

0

我很惊讶这个变体“Y第X页的”问题还没有上来呢。但它显然没有,所以在这里。

当您创建的“标题”文本,你也需要一个PdfTemplate添加到了TOC页面,每个页面数量将出现“DirectContent”。

在PdfPageEvent类,你需要使用OnChapterOnSection事件,以确定当前页码,并编写数到PdfTemplate该特定部分。

你可以看到类似在这个问题在C#实现与iTextSharp的东西:iTextSharp Creating a Footer Page # of #。你必须自己将它翻译成VB.net。他们在不同的单PdfTemplate直到他们知道在PDF中,在那里你会被不同的一个PdfTemplate在您的TOC的每个条目,并在填补他们的总页数,当你知道他们在什么页面...但这个概念是一样的。

2

下面是C#代码进一步解释马克斯托勒之上。

这个片段假定您遍历的xmlHeaders建设内容部分的表:

 PdfContentByte cb = writer.DirectContent; 
     MyCustomPageEventHandler pageEventHandler 

     foreach (string headerStr in xmlHeaders) 
     { 
      PdfTemplate currChapTemplate = cb.CreateTemplate(50, 50); 


      Paragraph titlePhrase = new Paragraph(); 
      titlePhrase.Add(headerStr); 
      titlePhrase.IndentationLeft = 150f; 
      pdfDoc.Add(titlePhrase); 

      float curY = writer.GetVerticalPosition(false); 
      float x = 450; 
      //here we add the template to the pdf content byte 
      cb.AddTemplate(currChapTemplate, x, curY); 

      //Now we have to send the template object to our custom eventhandler 
      //method that will store a template for each item in our TOC 
      pageEventHandler.addChapTemplateList(currChapTemplate); 
     } 

你已经建立的文档TOC protion后,下一步就是生成actualy内容对应于TOC。当您创建的实际页面的每个标题的,你需要创建一个新的变量Chapter并将其添加到文档中。这将触发你将加入到OnChapter事件处理程序的自定义代码。

最后,在自定义页面事件处理程序中,我们需要将代码添加到OnChapter方法中,并且我们需要创建一个自定义方法来存储模板列表。

int chapTemplateCounter = 0; 
    public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) 
    { 
     base.OnChapter(writer, document, paragraphPosition, title); 

     BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); 

     tableOfContentsTemplateList[chapTemplateCounter].BeginText(); 
     tableOfContentsTemplateList[chapTemplateCounter].SetFontAndSize(bfTimes, 12); 
     tableOfContentsTemplateList[chapTemplateCounter].SetTextMatrix(0, 0); 
     tableOfContentsTemplateList[chapTemplateCounter].ShowText("" + writer.PageNumber); 
     tableOfContentsTemplateList[chapTemplateCounter].EndText(); 

     chapTemplateCounter++; 
    } 

阵列的模板:

List<PdfTemplate> tableOfContentsTemplateList = new List<PdfTemplate>(); 
    public void addChapTemplateList(PdfTemplate chapTemplate) 
    { 

     tableOfContentsTemplateList.Add(chapTemplate); 

    } 

我希望帮助!