Operate PDF-iTextSharp-use links and bookmarks in Asp. Net

Source: Internet
Author: User

 

Link

The Anchor object of iTextSharp is very similar to the Anchor object in HTML. They allow creating links to external documents or internal documents. However, unlike the link in HTML, the link in iTextSharp does not have any style by default. For this reason, I suggest that for the link, it should be underlined and the blue font should be used to tell the user that the text is linked:

 

String path = Server. MapPath ("PDFs ");

 

Document doc = new Document ();

Try

 

{

 

Using writer. GetInstance (doc, new FileStream (path + "/Anchors.pdf", FileMode. Create ));

 

Doc. Open ();

 

Font link = FontFactory. GetFont ("Arial", 12, Font. UNDERLINE, new Color (0, 0,255 ));

 

Anchor anchor = new Anchor ("www.mikesdotnetting.com", link );

 

Anchor. Reference = "http://www.mikesdotnetting.com ";

 

Doc. Add (anchor );

 

}

 

Catch (incluentexception dex)

 

{

 

Response. Write (dex. Message );

 

}

 

Catch (IOException ioex)

 

{

 

Response. Write (ioex. Message );

 

}

 

Finally

 

{

 

Doc. Close ();

 

}

The code above creates an external link. After clicking the link, the browser opens and directs to the specified link.

The internal link is implemented in HTML by adding the NAME attribute for the <a> tag. This method is also used for iTextSharp:

 

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 "));

 

Doc. Add (p2 );

 

 

 

Anchor target = new Anchor ("This is the Target ");

 

Target. Name = "target ";

 

Paragraph p3 = new Paragraph ();

 

P3.Add (target );

 

Doc. Add (p3 );

The first paragraph contains an anchor pointing to "# target" just like <a> in HTML. The second paragraph contains multiple line breaks, the third paragraph contains an anchor and is set to the same Name as the first paragraph reference. the result is that when you Click "Click to go to target", PDF will direct to This anchor no matter what you read, and now "This is target" will appear at the top of the PDF.

An alternative solution of the anchor is to use the SetLocalGoto () and SetLocalDestination () Methods of the Chunk class:

 

 

Paragraph p4 = new Paragraph ();

 

P4.Add (new Chunk ("Click "));

 

P4.Add (new Chunk ("here", link). SetLocalGoto ("GOTO "));

 

P4.Add (new Chunk ("to find local goto "));

 

P4.Add (new Chunk ("\ n "));

 

 

 

Paragraph p5 = new Paragraph ();

 

P5.Add (new Chunk ("Local Goto Destination"). SetLocalDestination ("GOTO "));

 

 

 

Doc. Add (p4 );

 

Doc. Add (p5 );

 

 

 

 

 

 

 

 

 

 

The font used in the first paragraph tells the user that this is a hyperlink. Chunk. SetLocalGoto () accepts a String-type parameter, which is the name of the anchor. Next, several line breaks are added. The SetLocalDestination () method is used to set the name of the current location, which corresponds to the specified location name in the previously defined SetLocalGoto () method. When a PDF file is generated, "Here" is underlined and blue. After you click it, the PDF file is directed to "Local Goto Destination" at the top.

 

 

 

 

 

Bookmarks

 

Most of the time when you open a PDF file, your PDF browser will display a digital document structure, and each branch or leaf node will be linked to a chapter or section, iTextSharp uses the Chapter and Section objects to generate tree navigation.

 

The largest object of the bookmarkdoner is Chapter. Each Chapter object has a new page, and the Section object must be added to the Chapter object or other Section objects:

 

Chapter chapter1 = new Chapter (new Paragraph ("This is Chapter 1"), 1 );

 

Section section1 = chapter1.AddSection (20f, "Section 1.1", 2 );

 

Section section2 = chapter1.AddSection (20f, "Section 1.2", 2 );

 

Section subsection1 = section2.AddSection (20f, "Subsection 1.2.1", 3 );

 

Section subsection2 = section2.AddSection (20f, "Subsection 1.2.2", 3 );

 

Section subsubsection = subsection2.AddSection (20f, "Sub Subsection 1.2.2.1", 4 );

 

Chapter chapter2 = new Chapter (new Paragraph ("This is Chapter 2"), 1 );

 

Section section3 = chapter2.AddSection ("Section 2.1", 2 );

 

Section subsection3 = section3.AddSection ("Subsection 2.1.1", 3 );

 

Section section4 = chapter2.AddSection ("Section 2.2", 2 );

 

Chapter1.BookmarkTitle = "Changed Title ";

 

Chapter1.BookmarkOpen = true;

 

Chapter2.BookmarkOpen = false;

 

Doc. Add (chapter1 );

 

Doc. Add (chapter2 );

 

 

 

 

 

The above picture clearly explains the previous code. At the beginning of the Code, a Chapter object is created and a Paragraph object is input as the parameter. The second parameter indicates the Chapter of the current Chapter. Set this parameter to 1 and add a Section object to the current chapter. The three parameters passed in for the Section constructor are: Float parameters represent indentation on the left, the second String parameter represents the node name displayed on the bookmarks and pages. The last parameter is the indentation level of the current node. in this example, Section 1.1 is the second layer of the tree, and SubSection1 is added to Section1.1, so SubSection1 is the third layer of the tree. After understanding the above explanation, the rest of the code looks much more direct.

The final code (chapter1.BookmarkTitle = "Changed Title";) can be used to change the name of the tree navigation by setting the BookmarkTitle attribute, the code above sets BookmarkOpen of Chapter1 to True and BookmarkOpen of Chapter2 to False. finally, add all the chapters to the Document.

 

Chapter and Section consume an astonishing amount of memory, so be cautious when using them. If you need to create a PDF document such as a manual, it is best to schedule it when your Web server is idle.

 

 

-----

Original article: iTextSharp-Links and Bookmarks translated by CareySon

 

<Script> </script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.