2015-02-06 22 views
0

我試圖用c#中的itextsharp在pdf文件中創建相對鏈接。我已經使用這個例子(翻譯成C#):http://itextpdf.com/sandbox/annotations/RemoteGoToPageitext action中的相對路徑

但是,當我打開生成的PDF並單擊鏈接沒有任何反應。但是,如果我更改爲絕對路徑的鏈接。謝謝。

我的代碼:

class Program 
{ 
    public const string DEST = "abc2.pdf"; 
    public const string SRC = "xyz2.pdf"; 

    static void Main(string[] args) 
    { 
     Program app = new Program(); 

     app.createPdf(DEST); 
     app.createPdf2(SRC); 
    } 

    public void createPdf(string dest) 
    { 
     Document document1 = new Document(); 
     PdfWriter.GetInstance(document1, new FileStream(dest, FileMode.Create)); 

     document1.Open(); 
     document1.Add(new Paragraph("Page 1")); 
     document1.NewPage(); 
     document1.Add(new Paragraph("Page 2")); 
     document1.NewPage(); 
     document1.Add(new Paragraph("Page 3")); 
     document1.NewPage(); 
     document1.Add(new Paragraph("Page 4")); 
     document1.NewPage(); 
     document1.Add(new Paragraph("Page 5")); 
     document1.NewPage(); 
     document1.Add(new Paragraph("Page 6")); 
     document1.NewPage(); 
     document1.Add(new Paragraph("Page 7")); 
     document1.Close(); 
    } 

    public void createPdf2(string src) 
    { 
     Document document2 = new Document(); 
     PdfWriter.GetInstance(document2, new FileStream(src, FileMode.Create));    
     document2.Open(); 
     Chunk chunk = new Chunk("Link"); 
     var link = "abc2.pdf";     //this don't work 
     //var link = "c:/temp/abc2.pdf"; //this work 
     chunk.SetAction(new PdfAction(link, 6)); 
     document2.Add(chunk); 
     document2.Close(); 
    } 
} 
+0

請看看我對以下問題的回答:[使用相對路徑進行PDF生成時使用iText的錨定方法](http:// stack overflow.com/questions/27063677/use-of-relative-path-for-anchor-method-using-itext-for-pdf-generation)。這個問題幾乎是你的重複;-) – 2015-02-06 08:22:59

回答

0

你可以嘗試用./(dot斜槓)或/(斜槓)的文件,按文件所在位置之前,像這樣

var link = "./abc2.pdf"; 

var link "/abc2.pdf"; 
+0

據我所知,僅添加一個斜槓(沒有點)不應該工作,因爲它會指向文件系統的根目錄。 – 2015-02-06 12:33:12