【发布时间】:2023-03-17 14:22:01
【问题描述】:
我正在尝试创建一个包含图像的 PDF 并调整 PDF 的大小并添加一个链接。我这样做是为了可以将链接嵌入到我的图像中以在多个项目中使用。我正在使用 PDFsharp。我使用图像上的链接可以正常工作,但是当我调整 PDF 页面的大小时,我的链接将不再有效。
private static void createPDF()
{
string image = "C:\\download.png";
string filename = "C:\\Test.pdf";
PdfDocument doc = new PdfDocument();
PdfPage page = doc.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
AddImage(gfx, page, image, 0, 0);
doc.Save(filename);
}
private static void AddImage(XGraphics gfx, PdfPage page, string imagePath, int xPosition, int yPosition)
{
if (!File.Exists(imagePath))
{
throw new FileNotFoundException(String.Format("Could not find image {0}.", imagePath));
}
XImage xImage = XImage.FromFile(imagePath);
page.Width = xImage.PixelWidth;
page.Height = xImage.PixelHeight;
gfx.DrawImage(xImage, xPosition, yPosition, xImage.PixelWidth, xImage.PixelHeight);
XRect rec = gfx.Transformer.WorldToDefaultPage(new XRect(new XPoint(xPosition, yPosition), new XSize(page.Width, page.Height)));
PdfRectangle rect = new PdfRectangle(rec);
page.AddWebLink(rect, "http://www.google.com");
}
【问题讨论】:
标签: c# image pdf hyperlink pdfsharp