2015-02-06 28 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