2012-04-24 67 views
0

我需要提取嵌入在pdf文件中的視頻文件。我可以找到註釋中的視頻,以便我不能單獨保存。我需要保存這個文件,我如何實現這一目標?如何使用iTextSharp從PDF中提取RichMediaContent

例: iTextSharp - how to open/read/extract a file attachment?

他已經提取attachement喜歡的方式,我需要提取視頻。

這裏是我的代碼:

string FileName = AppDomain.CurrentDomain.BaseDirectory + "raven test.pdf"; 
    PdfReader pdfreader = new PdfReader(FileName); 
    PdfDictionary PageDictionary = pdfreader.GetPageN(1); 
    PdfArray Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);  
    if ((Annots == null) || (Annots.Length == 0)) 
     return; 

    foreach (PdfObject oAnnot in Annots.ArrayList) 
    { 
     PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(oAnnot); 

     if (AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.RICHMEDIA)) 
     { 
      if (AnnotationDictionary.Keys.Contains(PdfName.RICHMEDIACONTENT)) 
      { 
       PdfDictionary oRICHContent = AnnotationDictionary.GetAsDict(PdfName.RICHMEDIACONTENT); // here i could see the video embeded but it is in annotation, how do i save this file? 
      } 
     } 

    } 

回答

1

爲這一個你要引用Adobe Supplement to ISO 32000, BaseVersion 1.7, ExtensionLevel 3官方規格。以下是基本代碼,儘管您可能想要投入更多null檢查。有關任何問題,請參閱評論。請注意,並非所有嵌入式電影都使用RichMedia格式,有些只是特殊的附件,所以這不會讓它們全部獲得。

PdfReader pdfreader = new PdfReader(FileName); 
PdfDictionary PageDictionary = pdfreader.GetPageN(1); 
PdfArray Annots = PageDictionary.GetAsArray(PdfName.ANNOTS); 
if ((Annots == null) || (Annots.Length == 0)) 
    return; 

foreach (PdfObject oAnnot in Annots.ArrayList) { 
    PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(oAnnot); 

    //See if the annotation is a rich media annotation 
    if (AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.RICHMEDIA)) { 
     //See if it has content 
     if (AnnotationDictionary.Contains(PdfName.RICHMEDIACONTENT)) { 
      //Get the content dictionary 
      PdfDictionary RMC = AnnotationDictionary.GetAsDict(PdfName.RICHMEDIACONTENT); 
      if (RMC.Contains(PdfName.ASSETS)) { 
       //Get the assset sub dictionary if it exists 
       PdfDictionary Assets = RMC.GetAsDict(PdfName.ASSETS); 
       //Get the names sub array. 
       PdfArray names = Assets.GetAsArray(PdfName.NAMES); 
       //Make sure it has values 
       if (names.ArrayList.Count > 0) { 
        //A single piece of content can have multiple assets. The array returned is in the form {name, IR, name, IR, name, IR...} 
        for (int i = 0; i < names.ArrayList.Count; i++) { 
         //Get the IndirectReference for the current asset 
         PdfIndirectReference ir = (PdfIndirectReference)names.ArrayList[++i]; 
         //Get the true object from the main PDF 
         PdfDictionary obj = (PdfDictionary)PdfReader.GetPdfObject(ir); 
         //Get the sub Embedded File object 
         PdfDictionary ef = obj.GetAsDict(PdfName.EF); 
         //Get the filespec sub object 
         PdfIndirectReference fir = (PdfIndirectReference)ef.Get(PdfName.F); 
         //Get the true file stream of the filespec 
         PRStream objStream = (PRStream)PdfReader.GetPdfObject(fir); 
         //Get the raw bytes for the given object 
         byte[] bytes = PdfReader.GetStreamBytes(objStream); 
         //Do something with the bytes here 
        } 
       } 
      } 
     } 
    } 
} 
+0

謝謝@Chris ... – 2012-04-25 11:25:40