如何使用iTextSharp从PDF中提取RichMediaContent

如何使用iTextSharp从PDF中提取RichMediaContent,pdf,dictionary,annotations,itextsharp,asp.net-4.0,Pdf,Dictionary,Annotations,Itextsharp,Asp.net 4.0,我需要提取视频文件,这是嵌入在pdf文件。我可以找到注释中的视频,因此无法单独保存。我需要保存此文件如何实现 例: 他提取了附件,就像我提取视频一样 这是我的密码: string FileName = AppDomain.CurrentDomain.BaseDirectory + "raven test.pdf"; PdfReader pdfreader = new PdfReader(FileName); PdfDictionary PageDictionary = pdf

我需要提取视频文件,这是嵌入在pdf文件。我可以找到注释中的视频,因此无法单独保存。我需要保存此文件如何实现

例:

他提取了附件,就像我提取视频一样

这是我的密码:

 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?
            }
        }

    }

对于这一个,您需要参考官方规范。下面是基本代码,尽管您可能需要进行更多的
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
                    }
                }
            }
        }
    }
}
PdfReader PdfReader=新的PdfReader(文件名);
PdfDictionary PageDictionary=pdfreader.GetPageN(1);
PdfArray Annots=PageDictionary.GetAsArray(PdfName.Annots);
如果((Annots==null)| |(Annots.Length==0))
回来
foreach(Annots.ArrayList中的PdfObject-oAnnot){
PdfDictionary注释字典=(PdfDictionary)PdfReader.GetPdfObject(oanno);
//查看批注是否为富媒体批注
if(AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.RICHMEDIA)){
//看看有没有内容
if(AnnotationDictionary.Contains(PdfName.RICHMEDIACONTENT)){
//获取内容字典
PdfDictionary RMC=AnnotationDictionary.GetAsDict(PdfName.RICHMEDIACONTENT);
if(RMC.Contains(PdfName.ASSETS)){
//获取assset子字典(如果存在)
PdfDictionary Assets=RMC.GetAsDict(PdfName.Assets);
//获取子数组中的名称。
PdfArray names=Assets.GetAsArray(PdfName.names);
//确保它有值
如果(names.ArrayList.Count>0){
//单个内容可以有多个资产。返回的数组格式为{name,IR,name,IR,name,IR…}
for(int i=0;i