2011-04-07 38 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); 

    } 

我希望幫助!

相關問題