Reputation:
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);
Upvotes: 0
Views: 8794
Reputation: 9372
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:
Upvotes: 3
Reputation: 9032
Your question is straight from chapter 6 of the "building blocks" tutorial.
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();
Upvotes: 2