public static void CreateDocument(int pixelwidth, int pixelheight, int ppi, int fill, int mode, string name)
        {
            System.Diagnostics.Debug.Assert(pixelwidth > 0);
            System.Diagnostics.Debug.Assert(pixelheight > 0);
            System.Diagnostics.Debug.Assert(ppi > 0);
            //PSX.CheckEnum( mode, (int) con.phClassRGBColorMode );
            PSX.CheckEnum(fill, (int)con.phEnumTransparent, (int)con.phEnumBackgroundColor, (int)con.phEnumWhite);


            // Creates a new document


            // Create Desc2
            var Desc2 = PSX.MakeNewDescriptor();

            Desc2.PutString((int)con.phKeyName, name);
            Desc2.PutClass((int)con.phKeyMode, mode);
            Desc2.PutUnitDouble((int)con.phKeyWidth, (int)con.phUnitDistance, pixelwidth);
            Desc2.PutUnitDouble((int)con.phKeyHeight, (int)con.phUnitDistance, pixelheight);
            Desc2.PutUnitDouble((int)con.phKeyResolution, (int)con.phUnitDensity, ppi);
            Desc2.PutEnumerated((int)con.phKeyFill, (int)con.phTypeFill, fill);

            // Create Desc1
            PhotoshopTypeLibrary.IActionDescriptor Desc1 = PSX.MakeNewDescriptor();
            Desc1.PutObject((int)con.phKeyNew, (int)con.phClassDocument, Desc2);


            int old_count = DocumentAPI.GetDocumentCount();

            // Play the Event Into Photoshop
            PSX.PlayEvent((int)con.phEventMake, Desc1, (int)con.phDialogSilent, PSX.PlayBehavior.checknone);

            DocumentAPI.CheckDocumentCount(old_count + 1);
        }
        public static void DuplicateDocument(string new_name, bool merge_layers)
        {
            System.Diagnostics.Debug.Assert(new_name.Length > 0);

            // Ref1
            var Ref1 = PSX.MakeNewReference();

            Ref1.PutEnumerated((int)con.phClassDocument, (int)con.phTypeOrdinal, (int)con.phEnumFirst);

            // Desc1
            var Desc1 = PSX.MakeNewDescriptor();

            Desc1.PutReference((int)con.phKeyNull, Ref1);
            Desc1.PutString((int)con.phKeyName, new_name);

            if (merge_layers)
            {
                Desc1.PutBoolean((int)con.phKeyMerged, PSX.TRUE);
            }
            else
            {
                Desc1.PutBoolean((int)con.phKeyMerged, PSX.FALSE);
            }

            int old_count = DocumentAPI.GetDocumentCount();

            // Play the event in photoshop
            PSX.PlayEvent((int)con.phEventDuplicate, Desc1, (int)con.phDialogSilent, PSX.PlayBehavior.checkresult);

            DocumentAPI.CheckDocumentCount(old_count + 1);
        }
        public static void CloseAllDocuments()
        {
            int num_docs = DocumentAPI.GetDocumentCount();

            for (int i = 0; i < num_docs; i++)
            {
                DocumentAPI.CloseDocument();
            }
            DocumentAPI.CheckDocumentCount(0);
        }
        public static void CheckDocumentCount(int expected_count)
        {
            ///
            /// <summary>
            /// Raises an exception of the document count is not what is expected
            ///	Workitem: Move some of this checking into PSX
            /// </summary>
            ///

            int actual_document_count = DocumentAPI.GetDocumentCount( );

            if (actual_document_count != expected_count)
            {
                string msg = string.Format("Expected document count of {0}, got {1} instead", actual_document_count, expected_count);
                throw (new Photoshop6OM.PhotoshoProxyError(msg));
            }
        }
        public static void CloseDocument()
        {
            // NOTE: Those closes without prompting for save

            System.Diagnostics.Debug.Assert(DocumentAPI.GetDocumentCount() > 0);

            // Desc1
            var Desc1 = PSX.MakeNewDescriptor();

            Desc1.PutEnumerated((int)con.phKeySaving, (int)con.phTypeYesNo, (int)con.phEnumNo);

            int old_count = DocumentAPI.GetDocumentCount();

            // Play the event in photoshop
            PSX.PlayEvent((int)con.phEventClose, Desc1, (int)con.phDialogSilent, PSX.PlayBehavior.checknone);

            DocumentAPI.CheckDocumentCount(old_count - 1);
        }
        public static void OpenDocument(string filename)
        {
            if (DocumentAPI.IsFileLoaded(filename))
            {
                string msg = string.Format("The file \"{0}\" is already open.", filename);
                var    e   = new Photoshop6OM.PhotoshoProxyError(msg);
                throw e;
            }

            PSX.CheckFileExists(filename);

            // Desc1
            var Desc1 = PSX.MakeNewDescriptor();

            Desc1.PutPath((int)con.phKeyNull, filename);

            int old_count = DocumentAPI.GetDocumentCount();

            // Play the event in photoshop
            PSX.PlayEvent((int)con.phEventOpen, Desc1, (int)con.phDialogSilent, PSX.PlayBehavior.checkresult);

            DocumentAPI.CheckDocumentCount(old_count + 1);
        }