2011-12-29 20 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