Embedding HTML File in PDF Using iTextSharp in C#

2025-03-27T04:25:30.63+00:00

Hello,

I am using iTextSharp in C# to add a hyperlink to a PDF document. Currently, I am linking to a local HTML file using the following code:

Chunk gpoChunk = new Chunk($"GPO Name: {gpo.GpoName} ", headerFont);

//// Create Hyperlink

Anchor link = new Anchor("Click here for more details", linkFont);

link.Reference = gpo.HtmlReport;

Phrase phrase = new Phrase();

phrase.Add(gpoChunk);

phrase.Add(link);

gpoCell.AddElement(phrase);

gpoTable.AddCell(gpoCell);

document.Add(gpoTable);

However, when I move the PDF to a different location, the hyperlink to the local HTML file no longer works. I want to embed the HTML file directly into the PDF so that the link remains functional regardless of where the PDF is moved.

Could you please guide me on how to embed the HTML file within the PDF using iTextSharp?

Thank you!

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2025-03-27T09:19:32.77+00:00

    Hi @Thakur, Apurva [EMR/SYSS/PSS/PUNE] , Welcome to Microsoft Q&A,

    If you need to embed a file in a PDF, it will exist as an attachment, and you will need a PDF viewer to view it.

    If your PDF is not used locally, you can also put the linked file on the server and directly link the target address on the PDF.

    Regarding the problem that you cannot open the link after moving the PDF, I did not have this problem. I used the following code:

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System;
    using System.IO;
    
    namespace xxx
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                // PDF output path
                string outputPath = "GpoReport.pdf";
    
                // Create PDF file stream
                using (FileStream stream = new FileStream(outputPath, FileMode.Create))
                {
                    // Create PDF document
                    Document document = new Document();
                    PdfWriter.GetInstance(document, stream);
                    document.Open();
    
                    // Define fonts
                    Font headerFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12, BaseColor.BLACK);
                    Font linkFont = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.UNDERLINE, BaseColor.BLUE);
    
                    // Simulate GPO data
                    var gpo = new { GpoName = "Sample GPO", HtmlReport = @"file:///C:/Users/Desktop/report.html" };
    
                    // Create text
                    Chunk gpoChunk = new Chunk($"GPO Name: {gpo.GpoName} ", headerFont);
    
                    // Create a hyperlink (pointing to a local HTML file)
                    Anchor link = new Anchor("Click here for more details", linkFont);
                    link.Reference = gpo.HtmlReport; // Local HTML file path
    
                    // Combine text and hyperlinks
                    Phrase phrase = new Phrase();
                    phrase.Add(gpoChunk);
                    phrase.Add(link);
    
                    // Create a table
                    PdfPTable gpoTable = new PdfPTable(1);
                    PdfPCell gpoCell = new PdfPCell();
                    gpoCell.AddElement(phrase);
                    gpoTable.AddCell(gpoCell);
    
                    // Add table to PDF
                    document.Add(gpoTable);
    
                    // Close document
                    document.Close();
                }
    
                Console.WriteLine("PDF generated successfully!");
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.