使用itextsharp如何在PDF上包含链接?

问题描述 投票:0回答:1

我似乎无法找到如何在使用 iTextSharp 时包含链接的答案。这里只是 PDF 中的一些普通行:

            PdfPTable pdftable4 = new PdfPTable(1);
            pdftable4.HorizontalAlignment = 1;
            PdfPCell cell2 = new PdfPCell(new Phrase("Tax and shipping calculated at time of shipment. If shipping is quoted, rates are subject to change based on time of shipment. Quote is valid for 30 days.", pdffont_h));
            cell2.Colspan = 10;
            cell2.DisableBorderSide(2);
            pdftable4.AddCell(cell2);

            PdfPTable pdftable5 = new PdfPTable(1);
            pdftable5.HorizontalAlignment = 1;
            PdfPCell cell5 = new PdfPCell(new Phrase("Unless explicitly stated in quote description, manufacturing quality and accuracy is based on the expectations defined in ", pdffont_h));
            cell5.Colspan = 10;
            cell5.DisableBorderSide(2);
            pdftable5.AddCell(cell5);

但现在我正在尝试添加 HTML 链接:我尝试了一些方法:

            var c = new Chunk("Fathommfg");
            c.SetAnchor("https://www.google.com");
            PdfPTable pdftable6 = new PdfTable(1);
            PdfPCell cell6 = new PdfPCell(new Paragraph("https://www.google.com"));
            cell6.AddElement(c);
            pdftable6.AddCell(cell6);

            document.Add(pdftable6);

这个“SetAnchor”代码似乎不起作用。如何正确地做到这一点?在我的谷歌搜索中,我看到了一些使用不同类型 iText 的示例,但帮助并未使用我当前使用的相同 API 或代码。这是 Bruno Lowagie 的 iTextSharp。

c# asp.net itext
1个回答
0
投票

您只需使用:

            PdfPTable pdftable6 = new PdfPTable(1);
            pdftable6.HorizontalAlignment = 1;
            Paragraph p = new Paragraph("https://www.google.com");
            p.Font.Color = BaseColor.BLUE;
            PdfPCell cell6 = new PdfPCell(p);
            cell6.Colspan = 10;
            cell6.DisableBorderSide(2);
            pdftable6.AddCell(cell6);

PDF 会自动将 URL 识别为链接。正如我上面所做的那样,我也人为地将其着色为“蓝色”。

© www.soinside.com 2019 - 2024. All rights reserved.