How to create Table Of Contents in iTextSharp

后端 未结 3 390
一向
一向 2020-12-10 17:37

I need to create a Table Of Contents with page numbers, but I don\'t know how. Next format:

heading1 ----------------page number  
  subHeading1-------------         


        
3条回答
  •  余生分开走
    2020-12-10 18:09

    this snipplet can it be recursive, the basic concept is:

    List celdas = new List();
    string urlSection=String.empty;    
    var estatus = new Phrase();
    estatus.Leading = 25;
    if (streams != null && streams.Any())
        primero = streams.FirstOrDefault(x => x.Id == enlace.Id);
    if (primero != null)
        urlSection = primero.UrlSection;
    
    //For the code and generate hyperlink:
    Chunk espacioTab = new Chunk(" " + enlace.Name, baseFontBig );
    
    espacioTab = Visor.Servicios.GeneracionPDF.PDFUtils.GenerarVinculo(" " + enlace.Name, urlSection, baseFontBig);
    estatus.Add(espacioTab);
    if (incluirPaginado)
    {
    
       if (primero != null)
           actualPage = primero.TotalPages;
       else
           actualPage = 0;
    
    ///This is important, generate dots like "...." to chunk end 
        estatus.Add(new Chunk(new iTextSharp.text.pdf.draw.DottedLineSeparator()));
    
        var linkPagina = new Chunk(actualPage.ToString());
        linkPagina = Visor.Servicios.GeneracionPDF.PDFUtils.GenerarVinculo(actualPage.ToString(), urlSection, baseFontBig );
        estatus.Add(linkPagina);
        resultado.paginaFinal = actualPage;
    }
    
    //This is for add to your cell or table
    PdfPCell rightCell = new PdfPCell()
    {
        Border = PdfPCell.NO_BORDER,
        Colspan = 3,
        PaddingLeft = espacioInicial.Length,
        ExtraParagraphSpace = 10,
    };
    rightCell.AddElement(estatus);
    celdas.Add(rightCell);
    

    And create a new method, this create a hyperlinkand you can invoque when you want

    /*Generar Vinculo (create hyperlink)**/
    public static Chunk GenerarVinculo(String tituloMostrar, string urlDirecion, iTextSharp.text.Font fuente)
    {
        Chunk espacioTab = new Chunk();
        try
        {
            if (String.IsNullOrEmpty(urlDirecion))
            urlDirecion = "Indice de Contenido";
    
            espacioTab = new Chunk(tituloMostrar, fuente);
            var accion = PdfAction.GotoLocalPage(urlDirecion, false);
            espacioTab.SetAction(accion);
        }                
        catch (Exception error) { }
        return espacioTab;
    }
    

    Hope helps someone

提交回复
热议问题