public static LEADDocument LoadFromCache(string documentId)
        {
            LoadFromCacheOptions options = new LoadFromCacheOptions()
            {
                Cache = CacheObject, DocumentId = documentId
            };
            LEADDocument document = DocumentFactory.LoadFromCache(options);

            return(document);
        }
        public static async Task <string> ShareDocument(LEADDocument document, string documentId, string sharedFileTitle)
        {
            string retTempFilePath = string.Empty;
            string sharedfilePath  = string.Empty;

            LoadFromCacheOptions options = new LoadFromCacheOptions()
            {
                Cache = CacheObject, DocumentId = documentId
            };
            LEADDocument sourceDocument = (document != null) ? document : DocumentFactory.LoadFromCache(options);

            if (sourceDocument != null)
            {
                sharedfilePath = sourceDocument.GetDocumentFileName();
                if (string.IsNullOrWhiteSpace(sharedfilePath))
                {
                    List <string> sourceDocumentFiles = GetDocumentSourceFiles(sourceDocument);
                    if (sourceDocumentFiles.Count > 1)
                    {
                        sharedfilePath = Path.Combine(ApplicationDirectory, "Temp", (!string.IsNullOrWhiteSpace(sharedFileTitle) ? sharedFileTitle : sourceDocument.Name) + ".zip");
                        if (!Directory.Exists(Path.GetDirectoryName(sharedfilePath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(sharedfilePath));
                        }

                        retTempFilePath = sharedfilePath;
                        CompressFiles(sourceDocumentFiles, sharedfilePath);
                    }
                }
                else if (!string.IsNullOrWhiteSpace(sharedFileTitle) && sharedFileTitle.CompareTo(Path.GetFileNameWithoutExtension(sharedfilePath)) != 0)
                {
                    retTempFilePath = Path.Combine(ApplicationDirectory, "Temp", sharedFileTitle + Path.GetExtension(sharedfilePath));
                    if (!Directory.Exists(Path.GetDirectoryName(retTempFilePath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(retTempFilePath));
                    }
                    File.Copy(sharedfilePath, retTempFilePath, true);
                    sharedfilePath = retTempFilePath;
                }

                await Device.InvokeOnMainThreadAsync(async() =>
                {
                    await Share.RequestAsync(new ShareFileRequest()
                    {
                        Title = (!string.IsNullOrWhiteSpace(sharedFileTitle)) ? sharedFileTitle : Path.GetFileName(sharedfilePath),
                        File  = new ShareFile(sharedfilePath),
                        // for iOS iPad running iOS 13.4 or later we need to set the PresentationSourceBounds otherwise the share dialog won't show up.
                        PresentationSourceBounds = DeviceInfo.Platform == DevicePlatform.iOS && DeviceInfo.Idiom == DeviceIdiom.Tablet ? new System.Drawing.Rectangle(0, 20, 0, 0) : System.Drawing.Rectangle.Empty
                    });
                });
            }

            // only return temporarily created file so the user can delete it later, otherwise return null if we are using the original document file path.
            return(retTempFilePath);
        }
        public static bool DeleteFromCache(string documentId)
        {
            bool result = true;

            try
            {
                List <string>        allImbeddedDocumentsIds = new List <string>();
                LoadFromCacheOptions options = new LoadFromCacheOptions()
                {
                    Cache = CacheObject, DocumentId = documentId
                };
                LEADDocument document = DocumentFactory.LoadFromCache(options);
                if (document != null)
                {
                    allImbeddedDocumentsIds.Add(documentId);
                    if (document.Documents != null)
                    {
                        foreach (var embeddedDocument in document.Documents)
                        {
                            allImbeddedDocumentsIds.Add(embeddedDocument.DocumentId);
                        }
                    }
                }

                foreach (var docId in allImbeddedDocumentsIds)
                {
                    options.DocumentId = docId;
                    DocumentFactory.DeleteFromCache(options);

                    try
                    {
                        string cachedDocumentFolderPath = Path.Combine(CacheDirectory, docId);
                        if (Directory.Exists(cachedDocumentFolderPath))
                        {
                            Directory.Delete(cachedDocumentFolderPath, true);
                        }
                    }
                    catch (Exception ex1)
                    {
                        Console.WriteLine(ex1.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                result = false;
            }

            return(result);
        }
Esempio n. 4
0
        private static int Run(string preferencesFileName)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Messager.Caption = "Document Converter Demo";

            // Initialize Trace
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

            Console.WriteLine("LEADTOOLS " + Messager.Caption);

            DocumentConverterPreferences preferences;
            ConvertRedactionOptions      convertRedactionOptions = null;

            if (!string.IsNullOrEmpty(preferencesFileName))
            {
                // Load it from the file specified by the user
                preferences = DocumentConverterPreferences.Load(preferencesFileName);
                preferences.IsSilentMode = true;
            }
            else
            {
                // Load the preferences file
                DocumentConverterPreferences.DemoName    = "Document Converter Demo";
                DocumentConverterPreferences.XmlFileName = "DocumentConverterDemo";
                preferences = DocumentConverterPreferences.Load();
                preferences.IsSilentMode = false;
            }

            if (!Support.SetLicense(preferences.IsSilentMode))
            {
                if (preferences.IsSilentMode)
                {
                    throw new Exception("Your license file is missing, invalid or expired.");
                }
                return(-1);
            }

            // Create the rendering engine
            try
            {
                if (preferences.AnnRenderingEngine == null)
                {
                    preferences.AnnRenderingEngine           = new AnnWinFormsRenderingEngine();
                    preferences.AnnRenderingEngine.Resources = Tools.LoadResources();
                }
            }
            catch { }

            if (!preferences.IsSilentMode)
            {
                // Show the OCR engine selection dialog to startup the OCR engine
                Trace.WriteLine("Starting OCR engine");
                var engineType = preferences.OCREngineType;
                using (var dlg = new OcrEngineSelectDialog(DocumentConverterPreferences.DemoName, engineType.ToString(), true))
                {
                    dlg.AllowNoOcr        = true;
                    dlg.AllowNoOcrMessage = "The demo runs without OCR functionality but you will not be able to parse text from non-document files such as TIFF or Raster PDF. Click 'Cancel' to start this demo without an OCR engine.";
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        preferences.OcrEngineInstance = dlg.OcrEngine;
                        preferences.OCREngineType     = dlg.OcrEngine.EngineType;
                        Trace.WriteLine(string.Format("OCR engine {0} started", preferences.OCREngineType));
                    }
                }
            }
            else
            {
                // Initialize the default OCR engine
                preferences.OcrEngineInstance = InitOcrEngine(preferences);
            }

            // Initialize the RasterCodecs instance
            var rasterCodecs = new RasterCodecs();

            rasterCodecs.Options             = DocumentFactory.RasterCodecsTemplate.Options.Clone();
            preferences.RasterCodecsInstance = rasterCodecs;

            if (!string.IsNullOrEmpty(preferences.RasterCodecsOptionsPath))
            {
                preferences.RasterCodecsInstance.LoadOptions(preferences.RasterCodecsOptionsPath);
            }

            // Initialize the DocumentWriter instance
            preferences.DocumentWriterInstance = new DocumentWriter();
            if (!string.IsNullOrEmpty(preferences.DocumentWriterOptionsPath))
            {
                preferences.DocumentWriterInstance.LoadOptions(preferences.DocumentWriterOptionsPath);
            }

            // Cache to use
            ObjectCache cache = null;

            // Initialize the cache
            if (!string.IsNullOrEmpty(preferences.CacheDirectory))
            {
                var fileCache = new FileCache();
                fileCache.CacheDirectory          = preferences.CacheDirectory;
                fileCache.DataSerializationMode   = preferences.CacheDataSerializationMode;
                fileCache.PolicySerializationMode = preferences.CachePolicySerializationMode;
                cache = fileCache;
            }

            // Do conversions
            var more = true;

            while (more)
            {
                Console.WriteLine("Obtaining conversion options");

                if (!preferences.IsSilentMode)
                {
                    // Collect the options
                    using (var dlg = new DocumentConverterDialog())
                    {
                        dlg.Preferences      = preferences.Clone();
                        dlg.RedactionOptions = convertRedactionOptions;
                        if (dlg.ShowDialog() == DialogResult.OK)
                        {
                            preferences             = dlg.Preferences.Clone();
                            convertRedactionOptions = dlg.RedactionOptions;
                        }
                        else
                        {
                            more = false;
                        }
                    }
                }

                if (more)
                {
                    try
                    {
                        // Save the preferences
                        if (!preferences.IsSilentMode)
                        {
                            preferences.Save();
                        }

                        // Run the conversion
                        if (preferences.DocumentId != null)
                        {
                            var loadFromCacheOptions = new LoadFromCacheOptions
                            {
                                Cache      = cache,
                                DocumentId = preferences.DocumentId,
                                UserToken  = preferences.DocumentUserToken
                            };
                            using (var document = DocumentFactory.LoadFromCache(loadFromCacheOptions))
                            {
                                if (document == null)
                                {
                                    throw new Exception(string.Format("Could not load document with ID '{0}' from the cache", preferences.DocumentId));
                                }

                                preferences.Run(cache, document, null, convertRedactionOptions);
                            }
                        }
                        else
                        {
                            preferences.Run(null, null, null, convertRedactionOptions);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!preferences.IsSilentMode)
                        {
                            Messager.ShowError(null, ex.Message);
                        }
                        else
                        {
                            preferences.ErrorMessage = ex.Message;
                        }
                    }
                }

                if (more)
                {
                    if (!preferences.IsSilentMode)
                    {
                        // Ask if user wants to convert another document
                        more = (Messager.ShowQuestion(null, "Convert more?", MessageBoxButtons.YesNo) == DialogResult.Yes);
                    }
                    else
                    {
                        more = false;
                    }
                }
            }

            if (preferences.OcrEngineInstance != null)
            {
                preferences.OcrEngineInstance.Dispose();
            }

            if (preferences.RasterCodecsInstance != null)
            {
                preferences.RasterCodecsInstance.Dispose();
            }

            if (preferencesFileName != null)
            {
                preferences.Save(preferencesFileName);
            }

            if (preferences.ErrorMessage != null)
            {
                return(1);
            }

            return(0);
        }
        public static async Task <string> ShareDocuments(List <string> documentsIds)
        {
            string        retTempFilePath          = string.Empty;
            List <string> documentsUnderlyingFiles = new List <string>();

            foreach (var documentId in documentsIds)
            {
                LoadFromCacheOptions options = new LoadFromCacheOptions()
                {
                    Cache = CacheObject, DocumentId = documentId
                };
                LEADDocument sourceDocument = DocumentFactory.LoadFromCache(options);
                if (sourceDocument != null)
                {
                    string sharedfilePath = sourceDocument.GetDocumentFileName();
                    if (string.IsNullOrWhiteSpace(sharedfilePath))
                    {
                        foreach (var subDocument in sourceDocument.Documents)
                        {
                            sharedfilePath = subDocument.GetDocumentFileName();
                            if (!string.IsNullOrWhiteSpace(sharedfilePath))
                            {
                                documentsUnderlyingFiles.Add(sharedfilePath);
                            }
                        }
                    }
                    else
                    {
                        documentsUnderlyingFiles.Add(sharedfilePath);
                    }
                }
            }

            if (documentsUnderlyingFiles.Count == 0)
            {
                return(retTempFilePath);
            }
            else if (documentsUnderlyingFiles.Count == 1)
            {
                retTempFilePath = documentsUnderlyingFiles[0];
            }
            else
            {
                retTempFilePath = Path.Combine(ApplicationDirectory, "Temp", "Documents.zip");
                if (!Directory.Exists(Path.GetDirectoryName(retTempFilePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(retTempFilePath));
                }

                CompressFiles(documentsUnderlyingFiles, retTempFilePath);
            }

            await Device.InvokeOnMainThreadAsync(async() =>
            {
                await Share.RequestAsync(new ShareFileRequest()
                {
                    Title = Path.GetFileName(retTempFilePath),
                    File  = new ShareFile(retTempFilePath),
                    // for iOS iPad running iOS 13.4 or later we need to set the PresentationSourceBounds otherwise the share dialog won't show up.
                    PresentationSourceBounds = DeviceInfo.Platform == DevicePlatform.iOS && DeviceInfo.Idiom == DeviceIdiom.Tablet ? new System.Drawing.Rectangle(0, 20, 0, 0) : System.Drawing.Rectangle.Empty
                });
            });

            // only return temporarily created file so the user can delete it later, otherwise return null if we are using the original document file path.
            return((documentsUnderlyingFiles.Count > 1) ? retTempFilePath : string.Empty);
        }