public static List <KeyValuePair <string, int> > ReadLayerNames(PdsDictionary rootObj)
        {
            var layers = new List <KeyValuePair <string, int> >();

            var ocprops = rootObj.GetDictionary("OCProperties");

            if (ocprops != null)
            {
                var ocgs = ocprops.GetArray("OCGs");
                if (ocgs != null)
                {
                    for (var index = 0; index < ocgs.GetNumObjects(); index++)
                    {
                        var ocg = ocgs.GetDictionary(index);
                        if (ocg == null)
                        {
                            continue;
                        }

                        var name = ocg.GetText("Name");
                        var id   = ocg.GetId();
                        layers.Add(new KeyValuePair <string, int>(name, id));
                    }
                }
            }
            return(layers);
        }
        //////////////////////////////////////////////////////////////////////////////////////////////////
        // MarkUntaggedObjectsAsArtifact
        // find any non-tagged objects in the page content and mark them as artifact
        //////////////////////////////////////////////////////////////////////////////////////////////////
        internal static void MarkUntaggedObjectsAsArtifact(PdfPage page)
        {
            PdfDoc doc = page.GetDoc();

            for (int i = 0; i < page.GetNumPageObjects(); i++)
            {
                PdsPageObject page_obj = page.GetPageObject(i);

                PdsContentMark content_mark = page_obj.GetContentMark();
                if (!content_mark.GetTagArtifact() && content_mark.GetTagMcid() == -1)
                {
                    PdsDictionary artifact_dict = doc.CreateDictObject(false);
                    artifact_dict.Put("Type", doc.CreateNameObject(false, "Pagination"));
                    artifact_dict.Put("Subtype", doc.CreateNameObject(false, "Footer"));
                    content_mark.AddTag("Artifact", artifact_dict, false);
                }
            }
            page.SetContent();
        }
        ///////////////////////////////////////////////////////////////////////
        // ParseElement
        ///////////////////////////////////////////////////////////////////////
        private static void ParseObject(PdsObject obj, int level)
        {
            if (level == 3)
            {
                return;             // Don't go too deep, it's just a sample.
            }
            Action <string> dump = str =>
            {
                Console.WriteLine($"{str}");
            };

            String indent = new String('-', level);

            dump(indent);

            // parse element based on type;
            PdfObjectType objType = obj.GetObjectType();

            switch (objType)
            {
            case PdfObjectType.kPdsNull:
                dump(indent + "null:" + ((PdsBoolean)obj).GetValue());
                break;

            case PdfObjectType.kPdsBoolean:
                dump(indent + "boolean:" + ((PdsBoolean)obj).GetValue());
                break;

            case PdfObjectType.kPdsNumber:
                dump(indent + "number:" + ((PdsNumber)obj).GetValue());
                break;

            case PdfObjectType.kPdsString:
                dump(indent + "string:" + ((PdsString)obj).GetText());
                break;

            case PdfObjectType.kPdsStream:
                dump(indent + "stream:" + ((PdsStream)obj).GetRawDataSize());
                ParseObject(((PdsStream)obj).GetStreamDict(), level + 1);
                break;

            case PdfObjectType.kPdsArray:
            {
                dump("array:");
                PdsArray arr = (PdsArray)obj;
                for (int i = 0; i < arr.GetNumObjects(); i++)
                {
                    dump(indent + " [" + i + "]");
                    ParseObject(arr.Get(i), level + 1);
                }
            }
            break;

            case PdfObjectType.kPdsDictionary:
            {
                dump("dictionary:");
                PdsDictionary dict = (PdsDictionary)obj;
                for (int i = 0; i < dict.GetNumKeys(); i++)
                {
                    String key = dict.GetKey(i);
                    dump(indent + " /" + key);
                    ParseObject(dict.Get(key), level + 1);
                }
            }
            break;
            }
        }
Пример #4
0
        public static void Run(
            String openPath,                            // source PDF document
            String savePath,                            // output PDF document
            String attachmentPath                       // attachment PDF
            )
        {
            Pdfix pdfix = PdfixEngine.Instance;

            PdfDoc doc = pdfix.OpenDoc(openPath, "");

            if (doc == null)
            {
                throw new Exception(pdfix.GetError());
            }

            // rect for the new annotation
            PdfRect annot_rect = new PdfRect()
            {
                left   = 300,
                right  = 328,
                bottom = 50,
                top    = 118
            };

            // create annotation dictionary and fill it
            PdsDictionary annot_dict  = doc.CreateDictObject(true);
            PdsArray      color_array = annot_dict.PutArray("C");

            color_array.PutNumber(0, 1);
            color_array.PutNumber(1, 0.33f);
            color_array.PutNumber(2, 0.25f);
            annot_dict.PutString("Contents", "AutoTag_Sample_original.pdf");
            annot_dict.PutNumber("F", 28);
            annot_dict.PutString("Name", "Paperclip");
            annot_dict.PutRect("Rect", annot_rect);
            annot_dict.PutString("Subj", "File Attachment");
            annot_dict.PutName("Subtype", "FileAttachment");
            annot_dict.PutString("T", "Mr PDFixer");
            annot_dict.PutName("Type", "Annot");

            PdsDictionary fs_dict = annot_dict.PutDict("FS");

            fs_dict.PutString("F", "AutoTag_Sample_original.pdf");
            fs_dict.PutName("Type", "Filespec");
            fs_dict.PutString("UF", "AutoTag_Sample_original.pdf");

            // open attachment doc
            var fileStm = pdfix.CreateFileStream(attachmentPath, PsFileMode.kPsReadOnly);

            if (fileStm == null)
            {
                throw new Exception(pdfix.GetError());
            }

            byte[] fileData = new byte[fileStm.GetSize()];
            fileStm.Read(0, fileData);

            // create stream object from attachment
            PdsStream filestream = doc.CreateStreamObject(true, null, fileData);

            PdsDictionary ef_dict = fs_dict.PutDict("EF");

            ef_dict.Put("F", filestream);

            // create annotation object from dictionary
            PdfAnnot annot = doc.GetAnnotFromObject(annot_dict);

            // add annotation on the first page
            var page = doc.AcquirePage(0);

            if (page == null)
            {
                throw new Exception(pdfix.GetError());
            }

            if (!page.AddAnnot(0, annot))
            {
                throw new Exception(pdfix.GetError());
            }

            page.Release();

            // save document
            if (!doc.Save(savePath, Pdfix.kSaveFull))
            {
                throw new Exception(pdfix.GetError());
            }

            doc.Close();
        }