locked
How add anchor to existing PDf using Itextsharp? RRS feed

  • Question

  • User194909979 posted

    Can someone please tell  me how to add anchor to an existing pdf? i have search the web, but all I found was adding anchors to new pdf.

    Thanks for you help.

    Tuesday, December 18, 2012 12:56 PM

Answers

  • User151468730 posted

    Hello capiono!

    To create page jumps, please refer to the following link - you may find it useful.

    http://en.support.wordpress.com/splitting-content/page-jumps/

    Best of Luck!

    With Kind Regards,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 20, 2012 11:54 PM

All replies

  • User1648350404 posted

    Have a look at following:

    http://www.mikesdotnetting.com/Article/84/iTextSharp-Links-and-Bookmarks

    Wednesday, December 19, 2012 6:18 AM
  • User194909979 posted

    Yes, i tried this,  still not working. Actually this is what I'm trying to achieve: I need to add an anchor to an existing pdf in order to return the user to the top of the page when clicked

     public void Bookmark() {
                PdfReader reader = new PdfReader(new RandomAccessFileOrArray(_fileName), null);
                DeleteFile(); //delete old pdf (_filename)
    
                _fileName = Setting.Temp + Guid.NewGuid() + ".pdf";
    
                Document doc = new Document(PageSize.A4, 0, 0, 50, 50);
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(_fileName, FileMode.Create));
    
                iTextSharp.text.Font link = FontFactory.GetFont("Arial", 12, iTextSharp.text.Font.UNDERLINE, BaseColor.BLUE);
    
                doc.Open();
    
                PdfContentByte pdfContentByte = writer.DirectContent;
                Anchor topAnchor = null;
                PdfImportedPage page = null;
                for (int i = 1; i < reader.NumberOfPages; i++) {
                    if (i == 1) {
                        topAnchor = new Anchor();
                        topAnchor.Name = "TopAnchor";
                        page = writer.GetImportedPage(reader, i);
                        doc.NewPage();
                        pdfContentByte.AddTemplate(page, 0, 0);
                        doc.Add(topAnchor);
                    } else {
                        page = writer.GetImportedPage(reader, i);
                        doc.NewPage();
                        pdfContentByte.AddTemplate(page, 0, 0);
                        
                        Anchor anchor = new Anchor("Back to Top", link);
                        anchor.Reference = "#TopAnchor";
                        doc.Add(anchor);
                    }
                    
                }
    
    
                doc.Close();
            }
    

    Wednesday, December 19, 2012 7:35 AM
  • User151468730 posted

    Hello capiono!

    To create page jumps, please refer to the following link - you may find it useful.

    http://en.support.wordpress.com/splitting-content/page-jumps/

    Best of Luck!

    With Kind Regards,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 20, 2012 11:54 PM