I create PDF document ant I have go problem with links to chapter of text. I use code of Bruno Lowagie from here, but it's Java and I have got some difficulties.
I'm doing like this:
class TOCEvents
public class TOCEvents : PdfPageEventHelper
{
//protected System.Collections.Generic.List<TitleTOC> toc = new System.Collections.Generic.List<TitleTOC>();
protected Dictionary<string, int> toc = new Dictionary<string, int>(5);
public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text)
{
toc.Add(text, writer.PageNumber);
}
public Dictionary<string, int> GetTOC()
{
return toc;
}
}
main
for (int i = 0; i < 10; i++)
{
String title = "This is title " + i;
Chunk c = new Chunk(title, f14);
c.SetGenericTag(title);
doc.Add(new Paragraph(c));
for (int j = 0; j < 50; j++)
{
doc.Add(new Paragraph("Line " + j + " of title " + i));
}
}
doc.NewPage();
doc.Add(new Paragraph("Table of Contents", f24));
Chunk dottedLine = new Chunk(new iTextSharp.text.pdf.draw.DottedLineSeparator());
Dictionary<string, int> entries = ev.GetTOC();
Paragraph p;
foreach (KeyValuePair<string, int> entry in entries)
{
Chunk chunk = new Chunk(entry.Key);
chunk.SetAction(PdfAction.GotoLocalPage(entry.Key, false));
p = new Paragraph(chunk);
p.Add(dottedLine);
chunk = new Chunk(entry.Value.ToString());
chunk.SetAction(PdfAction.GotoLocalPage(entry.Key, false));
p.Add(chunk);
doc.Add(p);
}
I have got problems whith this:
foreach (KeyValuePair<string, int> entry in entries)
{
Chunk chunk = new Chunk(entry.Key);
chunk.SetAction(PdfAction.GotoLocalPage(entry.Key, false));
p = new Paragraph(chunk);
p.Add(dottedLine);
chunk = new Chunk(entry.Value.ToString());
chunk.SetAction(PdfAction.GotoLocalPage(entry.Key, false));
p.Add(chunk);
doc.Add(p);
}
What am I doing wrong? I can't set links to chapters of text. I think, that i'm using wrong Dictionary<string, int>
. Where did i mistakes?
Thank you.