By using itextsharp c#, I have one PDF file. In that PDF file, we have 10 pages. In 1st page of PDF file, i want a hyperlink at bottom to navigate a particular page of PDF file. For example, In 1st page of PDF, i want a hyperlink at the bottom to go to 5th page. NOTE:- it not newly crated file. i want to apply already created file.
-
Did you try search? [Click](http://stackoverflow.com/q/13448853/1997232), [click](http://stackoverflow.com/q/31107489/1997232).. – Sinatr Jul 06 '15 at 08:22
2 Answers
It was very easy to find this page. You have to use Anchor
and give it a name. Then you reference it by using #name
:
Anchor click = new Anchor("Click to go to Target");
click.Reference = "#target";
Paragraph p1 = new Paragraph();
p1.Add(click);
doc.Add(p1);
Paragraph p2 = new Paragraph();
p2.Add(new Chunk("\n\n\n\n\n\n\n\n"));
doc.Add(p2);
Anchor target = new Anchor("This is the Target");
target.Name = "target";
Paragraph p3 = new Paragraph();
p3.Add(target);
doc.Add(p3);
In your case: put such anchors on every page, make references to all of them on the first page.
Btw, did you know what
Often when you open a PDF file, your PDF Viewer application displays a tree-view of the structure of the document, with each branch or leaf acting as a link to the corresponding chapter or section. iTextSharp provides the functionality to generate this tree-view through its Chapter and Section classes.
This said you don't need to do anything, simply use Chapter
and Section
properly and you will have what you need.

- 20,892
- 15
- 90
- 319
-
2"I didn't get proper result" is not an accurate diagnosis. Document what you want, what you tried and how the result differs from what you want. – Bruno Lowagie Jul 06 '15 at 08:42
-
actually, the above code is worked in the same page only. i want to go particular page in pdf file. – Vijay Krishna Jul 06 '15 at 09:55
-
for example, i have a 10 pages of PDF file, i want to go to 5th page. – Vijay Krishna Jul 06 '15 at 09:57
-
How do you split pages? If you are doing that by inserting page break yourself (I think `Chapter` does that too, not sure, was a while when I last used `ITextSharp`), then simply add anchors to each and then reference. If content is not split by you explicitly (e.g. plain document without page breaks), then you are in troubles, perhaps you have to some event handling and manual job, see [this](http://stackoverflow.com/q/14812955/1997232) question. – Sinatr Jul 06 '15 at 10:00
-
we have a lot of pages in PDF file. if i want to go particular page, what i am doing in the code? – Vijay Krishna Jul 06 '15 at 10:14
-
I shouldn't be trying to answer your question without demanding to show of how you doing it first. Show code. I am not able to help any further than what is already written in my answer without seeing it, sorry. – Sinatr Jul 06 '15 at 10:17
-
@Sinatr I think the OP doesn't understand your answer because you're explaining how to introduce links when creating a PDF from scratch. In your defense: the question wasn't very clear and the OP should have provided some code. This being said, I think my answer is what the OP needs (but again: the question isn't clear). – Bruno Lowagie Jul 06 '15 at 10:38
-
actually what i am told, 1st assume, we have 1 PDF file. its already created. just assume the PDF file having 10 pages. if i want to go some particular page in that 10 pages of PDF file. for example, i want to 5th page of that 10 pages of PDF file. what i am doing by using itextsharp c#? – Vijay Krishna Jul 06 '15 at 10:58
-
-
@VijayKrishna What have you tried? I give you an example to navigate from page 1 to page 10 and you complain that this doesn't answer your question because you want to navigate to page 5. Is it that hard to replace `10` by `5` in a code sample? – Bruno Lowagie Jul 06 '15 at 11:07
-
i don't have any code for that. i am asking code or reference for that task. because i am a fresher. please send the solution for that. how to navigate 1st page to 5th page. NOTE:- i have code for newly created file to navigate 5th page. but problem is i want to create navigation to go 1st page to 5th page in 1st page of existing PDF file. – Vijay Krishna Jul 06 '15 at 11:28
-
2@Sinatr This seems to be a lost case. Have you seen the comments the OP wrote on my answer: he thinks that the names of the *variables* `a1` and `a10` refer to page numbers. Tsssk... – Bruno Lowagie Jul 06 '15 at 11:39
-
that is executed, did not apply for the PDF file. please tell me another solution – Vijay Krishna Jul 07 '15 at 05:55
Please take a look at the AddNavigation example. This is the Java version of what you need to do:
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
PdfDestination d1 = new PdfDestination(PdfDestination.FIT);
Rectangle rect = new Rectangle(0, 806, 595, 842);
PdfAnnotation vijay = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_INVERT, 10, d1);
stamper.addAnnotation(vijay, 1);
PdfDestination d2 = new PdfDestination(PdfDestination.FIT);
PdfAnnotation krishna = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_PUSH, 1, d2);
stamper.addAnnotation(krishna, 10);
stamper.close();
}
You want to navigate from page 1 to page 5 using iTextSharp. This means we have to make small adjustments:
First you need to create a PdfDestination
:
PdfDestination d = new PdfDestination(PdfDestination.FIT);
The C# code is identical to the Java code.
Then you need to create a Rectangle
. This is the area where people will be able to click:
Rectangle rect = new Rectangle(0, 806, 595, 842);
The C# code is identical to the Java code, but be aware that I am using harc-coded coordinates here. I have an existing document with A4 pages that are created in such a way that the lower-left corner of the page has the coordinate (0, 0)
. My coordinates correspond with the half-inch upper margin of the page.
Now we create a link annotation:
PdfAnnotation a = PdfAnnotation.CreateLink(stamper.Writer,
rect, PdfAnnotation.HIGHLIGHT_PUSH, 5, d);
Do you notice the slight differences between C# and Java? The method CreateLink()
starts with an upper case in C# and getWriter()
doesn't exist in PdfStamper
, you need the Writer
property. In this case, I jump to page 5.
Finally, we add the annotation to the stamper:
stamper.AddAnnotation(a, 1);
Note that once again, I changed the lower case (Java) into an upper case (C#).
This creates a link from page 1 to page 5.
The full example creates a link from page 1 to page 10 and a link from page 10 to page 1. You can test it here: primes_links.pdf

- 75,994
- 9
- 109
- 165
-
-
My example shows you how to navigate from page 1 to place 10. Replace 10 by 5 and you'll navigate to page 5. Did you try this? I'll update the C# code in my answer. – Bruno Lowagie Jul 06 '15 at 11:05
-
ok. thank u. finally one doubt. a10 and a1? i think these are page numbers. – Vijay Krishna Jul 06 '15 at 11:31
-
`a10` and `a1` are *variable names*! You can choose whatever you want. Do you want me to replace `a10` with `vijay` and `a1` with `krishna`? The code will keep working. – Bruno Lowagie Jul 06 '15 at 11:34
-
Done! Now the *variable names* refer to you personally @VijayKrishna. Now you can no longer confuse a *variable name* with a page number. Please try the example and if it works, accept the answer. – Bruno Lowagie Jul 06 '15 at 11:36
-
Moreover: in the C# example, I used `a`. No page number there, is there? *Have you tried the example?* – Bruno Lowagie Jul 06 '15 at 11:37
-
i was try that, the program is success fully. no error. but didn't apply to my pdf file. – Vijay Krishna Jul 06 '15 at 13:38
-
Maybe you copied the example literally and maybe your pages have different dimensions (a different origin and/or a different width / height). In any case: I have proof that my example works. I see no reason to change my answer. If it doesn't work for you, you have to explain why it doesn't work. For instance: did you take a look at the PDF with [iText RUPS](http://itextpdf.com/product/itext_rups)? Do you see the annotations? – Bruno Lowagie Jul 06 '15 at 13:41
-
the code successfully executed. but didn't apply for my pdf. because its have images. – Vijay Krishna Jul 06 '15 at 13:46
-
*Because its have images* is not a valid reason. You can perfectly add link annotations to a PDF containing images. I am pretty sure that you are using the wrong coordinates. Do you know the size of the pages in your original PDF? – Bruno Lowagie Jul 06 '15 at 13:48