简体   繁体   中英

Add a hyperlink to paragraph using itextsharp

I have a paragraph that I want to attach a link with, by doing the below it displays another text on the bottom instead of the link being attached to the existing text.

   Paragraph portfolioText = new Paragraph("View our Portfolio", new Font(Font.FontFamily.HELVETICA, 15, Font.NORMAL, iTextSharp.text.BaseColor.WHITE));
           portfolioText.Alignment = Element.ALIGN_CENTER;
  portfolioText.SetLeading(12.1f, 12.1f);
            portfolioText.IndentationLeft = 90;


            Anchor portAnch = new Anchor(portfolioText);
            portAnch.Reference = "http://portfolio.xxxxx.com/";
doc.Add(portfolioText);
doc.Add(portAnch);

[![enter image description here][1]][1]

UPDATE:

I tried with chunk instead like:

Chunk portText = new Chunk("View Portfolio");
            portText.SetAnchor(new Uri("http://portfolio.xxxxx.com/"));
            Paragraph p = new Paragraph();
            p.Add(portText);
            doc.Add(p);

and it worked but how do I apply all the font style/size and position just like the previous paragraph?

UPD 2

I tried to give it the styles like this but then I don't even see it on the page

 Chunk portText = new Chunk("View Portfolio");
            portText.SetAnchor(new Uri("http://portfolio.xxxx.com/"));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Font = new Font(Font.FontFamily.HELVETICA, 15, Font.NORMAL, iTextSharp.text.BaseColor.WHITE);
            p.SetLeading(12.1f, 12.1f);
            p.IndentationLeft = 90;
            p.Add(portText);    

Your question is straight from chapter 6 of the "building blocks" tutorial.

https://developers.itextpdf.com/content/itext-7-building-blocks/chapter-6-creating-actions-destinations-and-bookmarks

example with named actions:

Paragraph p = new Paragraph()
.add("Go to last page")
.setAction(PdfAction.createNamed(PdfName.LastPage));
document.add(p);

p = new Paragraph()
.add("Go to first page")
.setAction(PdfAction.createNamed(PdfName.FirstPage));
document.add(p);

example with GoTo action:

new Paragraph()
    .addTabStops(tabstops)
    .add(entry.getKey())
    .add(new Tab())
    .add(String.valueOf(entry.getValue()))
    .setAction(PdfAction.createGoTo(
            PdfExplicitDestination.createFit(entry.getValue())));

Where entry is a an entry from Map<String, Integer

The working iText7 code for your usecase is

PdfDocument pdfDocument = new PdfDocument(new PdfWriter(PATH_TO_OUTPUT_FILE));
Document layoutDocument = new Document(pdfDocument);

Paragraph portfolioText = new Paragraph("View our Portfolio");
portfolioText.setFont(PdfFontFactory.createFont());
portfolioText.setFontColor(Color.ORANGE);
portfolioText.setFixedLeading(12.1f);
portfolioText.setFirstLineIndent(90f);
portfolioText.setAction(PdfAction.createURI("http://google.com/"));

layoutDocument.add(portfolioText);
layoutDocument.close();

Your example code above has all the individual working parts to do what you want, just not put together the correct way. Here's a simple working example (using iTextSharp 5.5.12 like you are):

// [1] create a Chunk with font and colors you want
var anchor = new Chunk("View our Portfolio")
{
    Font = new Font(
        Font.FontFamily.HELVETICA, 25,
        Font.NORMAL,
        BaseColor.BLUE
    )
};

// [2] set the anchor URL
anchor.SetAnchor("http://portfolio.xxxxx.com/");

// [3] create a Paragraph with alignment, indentation, etc
Paragraph p = new Paragraph()
{
    Alignment = Element.ALIGN_CENTER,
    IndentationLeft = 90
};
p.SetLeading(12.1f, 12.1f);

// [4] add chunk to Paragraph
p.Add(anchor);

// [5] add Paragraph to Document
document.Add(p);

Result PDF:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM