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.