Ejemplo n.º 1
0
        public DjvuPageInfo(DjvuDocumentInfo docInfo, int pageNumber)
        {
            if (docInfo == null)
            {
                throw new ArgumentNullException(nameof(docInfo));
            }

            if ((_Info = docInfo.GetPageInfo(pageNumber)) == null)
            {
                throw new ArgumentException(
                          $"{nameof(DjvuDocumentInfo)} object {nameof(docInfo)} is not fully initialized.",
                          nameof(docInfo));
            }

            if (pageNumber < 0 || pageNumber >= docInfo.PageCount)
            {
                throw new ArgumentOutOfRangeException(nameof(pageNumber));
            }

            _DocumentInfo = docInfo;
            Number        = pageNumber;

            Page = NativeMethods.GetDjvuDocumentPage(_DocumentInfo.Document, pageNumber);

            DjvuJobStatus status       = DjvuJobStatus.NotStarted;
            List <object> messagesList = new List <object>();

            while (true)
            {
                status = NativeMethods.GetDjvuJobStatus(Page);
                if ((int)status >= 2)
                {
                    break;
                }
                else
                {
                    messagesList.AddRange(DjvuDocumentInfo.ProcessMessage(docInfo.Context, true));
                }
            }

            if (status == DjvuJobStatus.Failed)
            {
                throw new ApplicationException($"Failed to parse document page: {pageNumber}");
            }
            else if (status == DjvuJobStatus.Stopped)
            {
                throw new ApplicationException(
                          $"Parsing of DjVu document page interrupted by user. Page number: {pageNumber}");
            }

            if (_Info != null)
            {
                Width    = _Info.Width;
                Height   = _Info.Height;
                Dpi      = _Info.Dpi;
                Version  = _Info.Version;
                Rotation = _Info.Rotation;
            }

            PageType = NativeMethods.GetDjvuPageType(Page);
        }
Ejemplo n.º 2
0
        public static DjvuDocumentInfo CreateDjvuDocumentInfo(string filePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentException(nameof(filePath));
            }

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException($"File was not found - path: {filePath}");
            }

            Process proc        = Process.GetCurrentProcess();
            string  fileName    = System.IO.Path.GetFileNameWithoutExtension(filePath);
            string  programName = $"{proc.ProcessName}:{proc.Id:0000#}:{fileName}:{DateTime.UtcNow}";

            IntPtr context  = IntPtr.Zero;
            IntPtr document = IntPtr.Zero;

            try
            {
                context = NativeMethods.CreateDjvuContext(programName);
                if (context == IntPtr.Zero)
                {
                    throw new ApplicationException("Failed to create native ddjvu_context object");
                }

                document = NativeMethods.LoadDjvuDocument(context, filePath, 1);
                if (document == IntPtr.Zero)
                {
                    throw new ApplicationException($"Failed to open native djvu_document: {filePath}");
                }

                DjvuJobStatus status = DjvuJobStatus.NotStarted;
                while (true)
                {
                    status = NativeMethods.GetDjvuJobStatus(document);
                    if ((int)status >= 2)
                    {
                        break;
                    }
                    else
                    {
                        ProcessMessage(context, true);
                    }
                }

                if (status == DjvuJobStatus.OK)
                {
                    return(new DjvuDocumentInfo(context, document, filePath));
                }
                else if (status == DjvuJobStatus.Failed)
                {
                    throw new ApplicationException($"Failed to parse document: {filePath}");
                }
                else if (status == DjvuJobStatus.Stopped)
                {
                    throw new ApplicationException(
                              $"Parsing of DjVu document interrupted by user. Document path: {filePath}");
                }

                throw new ApplicationException($"Unknown error: \"{status}\" while processing document: {filePath}");
            }
            catch (Exception error)
            {
                if (document != IntPtr.Zero)
                {
                    NativeMethods.ReleaseDjvuDocument(document);
                    document = IntPtr.Zero;
                }

                if (context != IntPtr.Zero)
                {
                    NativeMethods.ReleaseDjvuContext(context);
                    context = IntPtr.Zero;
                }

                throw new AggregateException($"Failed to create DjvuDocumenyInfo from file {filePath}", error);
            }
        }