D3X
D3X

Reputation: 547

How can I put anchor for email in PDF using itextpdf

I have to place some data in a table in a PDF, which has email information, it is required to put an anchor in the generated PDF, such that on click of that email, outlook window with pre-filled subject is opened and email can be sent directly by adding message to it.

Referring to online examples, I had put content to a Paragraph and added an Anchor to it, but unfortunately, it didn't work out, please find the snipped of the code.


    table.addCell(getLCell(1, labelMap.get("email"), 1, 8));
    Paragraph para=new Paragraph();
    para.add(new Phrase(email));
    Anchor anchor = new Anchor("mailto:"+email+"?subject=Reference Number:1234");
    anchor.setReference("mailto:"+email+"?subject=Reference Number:1234");
    para.add(anchor);
    table.addCell(this.getVCell(3, para, 1, 4));


private PdfPCell getLCell(int cspan, String name, int... d) {
    PdfPCell cell = new PdfPCell(new Phrase(name, normal_bold));
    cell.setRowspan(1);
    cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
    cell.setColspan(cspan);
    for (int i : d) {
        cell.disableBorderSide(i);
    }
    return cell;
}



private PdfPCell getVCell(int cspan, Paragraph name, int... d) {
    PdfPCell cell = new PdfPCell(new Phrase(name.getContent(), normal));
    cell.setRowspan(1);
    cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
    cell.setColspan(cspan);
    for (int i : d) {
        cell.disableBorderSide(i);
    }
    return cell;
}

Thanks in Advance!!!

Upvotes: 1

Views: 1972

Answers (1)

Jordi Castilla
Jordi Castilla

Reputation: 26961

Something like this must work...

Anchor anchor = new Anchor("sendMail");
anchor.setReference("mailto:"+email+"?subject=ReferenceNumber:1234");
para.add(anchor);

EDIT all depends of the final result you need. But this is working for me:

private static Font bigFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);

Paragraph para= new Paragraph("YOUR CONTENT", bigfont);

Anchor anchor = new Anchor("sendMail");
anchor.setReference("mailto:"+email+"?subject=ReferenceNumber:1234");
para.add(anchor);

Upvotes: 2

Related Questions