2011-12-29 125 views
2

我需要在没有使用Acrobat SDK(需要安装完整的Acrobat专业版)的情况下为pdf文档添加书签。 我正在使用iTextSharp,但它是为Java制作的,并且移植到.net它不完整。 你知道一个免费的替代品或文件来做到这一点吗?为了在c#中处理PDF文档,最好的免费替代Acrobat SDK。

+0

你需要一个“免费的啤酒”解决方案吗? – Bobrovsky 2011-12-30 17:56:07

+1

我的意见是ItextSharp是最好的免费替代acrobat sdk – Alessandro 2012-01-04 11:43:46

+0

的问题是:iTextSharp只对开源项目免费。 – Bobrovsky 2012-01-04 17:24:12

回答

2

你声称“iTextSharp的,但在Java和.NET到它不完整移植取得了”相当。主要区别是listed here

使用iTextsharp添加书签很简单。请参阅PdfOutlinePdfDestination的API。下面是一个简单的例子,让你开始:

using (Document document = new Document()) { 
    PdfWriter writer = PdfWriter.GetInstance(
    document, Response.OutputStream 
); 
    document.Open(); 
    PdfOutline root = writer.RootOutline; 
    string section = "Section {0}"; 
    string paragraph = "Paragraph {0}"; 
    for (int i = 0; i < 10;) { 
    PdfOutline sectionBookmark = new PdfOutline(
     root, 
     new PdfDestination(
     PdfDestination.FITH, writer.GetVerticalPosition(true) 
    ), 
     string.Format(section, ++i) 
    ); 
    document.Add(new Paragraph(string.Format(section, i))); 
    for (int j = 0; j < 4;) { 
     PdfOutline subSectionBookmark = new PdfOutline(
     sectionBookmark, 
     new PdfDestination(
      PdfDestination.FITH, writer.GetVerticalPosition(true) 
     ), 
     string.Format(paragraph, ++j) 
    ); 
     document.Add(new Paragraph(string.Format(paragraph, j))); 
    } 
    document.NewPage(); 
    } 
} 

上面的例子在5.1.3的Web环境中测试。如果您的开发环境不同,请将上面的Response.OutputStream替换为您所选的Stream

+0

Thx!我会接受你的答案,但是因为我需要为现有文档添加书签,并且书签需要在里面有一个javascript,所以我的puropose与你的代码有点不同。我的问题是,什么是相当于PDFstamper.setOutline方法存在于Java中,而不是在C#中? – Alessandro 2011-12-29 13:16:26

+0

这是C#=>'stamper.Outlines = outlines;'中的属性。无论何时在C#中找不到某些东西,都要查找一个从Java方法名称中删除了'set'的属性。对不起,现在我读了你的评论,我应该意识到你正在使用现有的PDF。你有[这本书](http://itextpdf.com/book/)?即使你不这样做,也可以看一下[Chapter 7 examples](http://kuujinbo.info/iTextInAction2Ed/index.aspx),也许从[this]开始(http://kuujinbo.info/iTextInAction2Ed /index.aspx?ch=Chapter07&ex=BookmarkedTimeTable)。它应该有所帮助。 – kuujinbo 2011-12-29 14:13:43