1
我正在嘗試創建一個帶有圖片的PDF並調整PDF大小並添加一個鏈接。我正在這樣做,所以我可以在我的圖像中嵌入一個鏈接以用於多個項目。我正在使用PDFsharp。我使用圖片上的鏈接可以正常工作,但是當我調整PDF頁面的大小時,我的鏈接將不再起作用。PDFsharp添加鏈接不起作用
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");
}
這似乎可行,但我恐怕它不會總是工作。在調用'XGraphics.FromPdfPage(page)'之前設置頁面的寬度和高度,事情應該是正確的。 –