简体   繁体   English

使用itextsharp将超链接添加到段落

[英]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] [![在此处输入图片描述] [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 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. 您的问题直接来自“构建模块”教程的第6章。

https://developers.itextpdf.com/content/itext-7-building-blocks/chapter-6-creating-actions-destinations-and-bookmarks 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: GoTo操作的示例:

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 其中entryMap<String, Integer的条目

The working iText7 code for your usecase is 适用于您的用例的iText7代码是

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): 这是一个简单的工作示例(像您一样使用iTextSharp 5.5.12):

// [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: 结果PDF:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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