2017-03-16 9 views
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"); 
    } 

答えて

0

正解は、XGraphicsオブジェクトを取得する前に、ページの幅と高さを設定する必要があることです。

だから、ほんの数行のコードを並べ替えることは実際にはやります。

0

私は質問を投稿した直後に問題を解決しました。

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)); 
     } 

     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"); 
     XImage xImage = XImage.FromFile(imagePath); 
     page.Width = xImage.PixelWidth; 
     page.Height = xImage.PixelHeight; 
     gfx.DrawImage(xImage, xPosition, yPosition, xImage.PixelWidth, xImage.PixelHeight); 
    } 

私はちょうど数行のコードをリバースしました。

+0

これはうまくいくように思われるかもしれませんが、いつもうまくいくとは限りません。 XGraphics.FromPdfPage(page);を呼び出す前にページの幅と高さを設定してください。 –

関連する問題