2011-12-20 41 views
3

我想知道我們是否可以突出顯示文本(顏色)的使用itextsharp已經創建PDF現有的PDF嗎?高亮文本(顏色)使用iTextSharp的使用C#

我看到像創建一個新的PDF的例子,而這樣做,我們可以應用顏色。我正在尋找我可以從PDF中獲取大量文本並應用顏色並保存的地方。

這裏是我想根據業務規則來完成,閱讀PDF文件,分析文本和突出顯示文本的東西。

任何第三方DLL的建議也適用,作爲第一步,我期待中opensource iTextsharp library

+0

你有沒有嘗試谷歌搜索ItextSharp從我在過去一週看到的PDF問題有很多例子。 – MethodMan 2011-12-20 22:20:24

+0

是的,我們有實例創建PDF格式的,在那裏,因爲我在尋找中提取文本,並強調和重新保存.. – user966043 2011-12-21 01:27:26

+0

確定現在我明白了..我將不得不考慮上了一會兒..讓我做一些挖掘 – MethodMan 2011-12-21 13:48:01

回答

5

是的你可以突出顯示文本,但你將不得不爲它工作。就規格而言,看起來像突出顯示的是PDF文本標記註釋。這部分很容易。困難的部分是找出應用註釋的座標。

下面是使用現有的PdfStamper稱爲stamper創建的一大亮點簡單的代碼:

PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad); 

一旦你有,你可以使用設置顏色的亮點:

highlight.Color = BaseColor.YELLOW; 

然後將其添加到stamper使用:

stamper.AddAnnotation(highlight,1); 

從技術上講,rect參數實際上並未被使用(據我所知),而是被參數quad覆蓋。所述quad參數是X,Y COORDS基本上表示一個矩形(技術上四邊形)的角部的陣列。規範說,他們從左下角開始逆時針轉動,但實際上他們似乎從左下角到右下角到左上角到右上角。計算四是痛苦所以不是它只是更容易從它創建一個矩形,並創建四:

iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f); 
float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; 

那麼,你如何讓現有的文本在首位的矩形?對於您需要看TextExtractionStrategyPdfTextExtractor。有很多要進入,所以我打算從pointing you at this post開始,它有一些鏈接進一步的帖子。

下面是一個完整的C#2010 WinForms應用程序,針對iTextSharp 5.1.1.2,它展示了一個簡單PDF的創建以及使用硬編碼座標高亮部分文本。如果您需要幫助計算這些座標,請從上面的鏈接開始,然後提出任何問題!

using System; 
using System.ComponentModel; 
using System.Data; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      //Create a simple test file 
      string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf"); 

      using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) 
      { 
       using (Document doc = new Document(PageSize.LETTER)) 
       { 
        using (PdfWriter w = PdfWriter.GetInstance(doc, fs)) 
        { 
         doc.Open(); 
         doc.Add(new Paragraph("This is a test")); 
         doc.Close(); 
        } 
       } 
      } 

      //Create a new file from our test file with highlighting 
      string highLightFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Highlighted.pdf"); 

      //Bind a reader and stamper to our test PDF 
      PdfReader reader = new PdfReader(outputFile); 

      using (FileStream fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None)) 
      { 
       using (PdfStamper stamper = new PdfStamper(reader, fs)) 
       { 
        //Create a rectangle for the highlight. NOTE: Technically this isn't used but it helps with the quadpoint calculation 
        iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(60.6755f, 749.172f, 94.0195f, 735.3f); 
        //Create an array of quad points based on that rectangle. NOTE: The order below doesn't appear to match the actual spec but is what Acrobat produces 
        float[] quad = { rect.Left, rect.Bottom, rect.Right, rect.Bottom, rect.Left, rect.Top, rect.Right, rect.Top }; 

        //Create our hightlight 
        PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad); 

        //Set the color 
        highlight.Color = BaseColor.YELLOW; 

        //Add the annotation 
        stamper.AddAnnotation(highlight,1); 
       } 
      } 

      this.Close(); 
     } 
    } 
} 
+0

克里斯,非常感謝您在這個問題上的時間。 – user966043 2011-12-22 21:47:18

+0

克里斯,這是我正在嘗試完成,http:// stackoverflow。com/questions/2375674/itextsharp-how-to-get-the-position-of-word-on-a-page/4866110#4866110,我必須得到每個單詞,我可能不得不突出顯示一些基於規則的單詞和重新保存它。我在你的另一篇文章中看到,如果你分享了一些關於實現「TextExtractionStrategy」的信息,那麼你建議子類http://stackoverflow.com/questions/7513209/using-locationtextextractionstrategy-in-itextsharp-for-text-coordinate,對我來說,因爲我現在可能不會在pdf中解析字詞的座標。 – user966043 2011-12-22 22:03:21

相關問題