Example #1
0
        /// <summary>
        /// Create a new <see cref="MuPDFDocument"/> from an array of bytes.
        /// </summary>
        /// <param name="context">The context that will own this document.</param>
        /// <param name="data">An array containing the data bytes that make up the document. This must not be altered until after the <see cref="MuPDFDocument"/> has been disposed!
        /// The address of the array will be pinned, which may cause degradation in the Garbage Collector's performance, and is thus only advised for short-lived documents. To avoid this issue, marshal the bytes to unmanaged memory and use one of the <see cref="IntPtr"/> constructors.</param>
        /// <param name="fileType">The type of the document to read.</param>
        public MuPDFDocument(MuPDFContext context, byte[] data, InputFileTypes fileType)
        {
            bool isImage = fileType == InputFileTypes.BMP || fileType == InputFileTypes.GIF || fileType == InputFileTypes.JPEG || fileType == InputFileTypes.PAM || fileType == InputFileTypes.PNG || fileType == InputFileTypes.PNM || fileType == InputFileTypes.TIFF;

            this.OwnerContext = context;

            DataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
            IntPtr dataAddress = DataHandle.Value.AddrOfPinnedObject();
            ulong  dataLength  = (ulong)data.Length;

            float xRes = 0;
            float yRes = 0;

            ExitCodes result = (ExitCodes)NativeMethods.CreateDocumentFromStream(context.NativeContext, dataAddress, dataLength, FileTypeMagics[(int)fileType], isImage ? 1 : 0, ref NativeDocument, ref NativeStream, ref PageCount, ref xRes, ref yRes);

            if (xRes > 72)
            {
                this.ImageXRes = xRes;
            }
            else
            {
                this.ImageXRes = 72;
            }

            if (yRes > 72)
            {
                this.ImageYRes = yRes;
            }
            else
            {
                this.ImageYRes = 72;
            }

            switch (result)
            {
            case ExitCodes.EXIT_SUCCESS:
                break;

            case ExitCodes.ERR_CANNOT_OPEN_STREAM:
                throw new MuPDFException("Cannot open data stream", result);

            case ExitCodes.ERR_CANNOT_OPEN_FILE:
                throw new MuPDFException("Cannot open document", result);

            case ExitCodes.ERR_CANNOT_COUNT_PAGES:
                throw new MuPDFException("Cannot count pages", result);

            default:
                throw new MuPDFException("Unknown error", result);
            }

            Pages        = new MuPDFPageCollection(context, this, PageCount);
            DisplayLists = new MuPDFDisplayList[PageCount];
        }
Example #2
0
        /// <summary>
        /// Create a new <see cref="MuPDFDocument"/> from a file.
        /// </summary>
        /// <param name="context">The context that will own this document.</param>
        /// <param name="fileName">The path to the file to open.</param>
        public MuPDFDocument(MuPDFContext context, string fileName)
        {
            bool isImage;

            string extension = Path.GetExtension(fileName).ToLowerInvariant();

            switch (extension)
            {
            case ".bmp":
            case ".dib":

            case ".gif":

            case ".jpg":
            case ".jpeg":
            case ".jpe":
            case ".jif":
            case ".jfif":
            case ".jfi":

            case ".pam":
            case ".pbm":
            case ".pgm":
            case ".ppm":
            case ".pnm":

            case ".png":

            case ".tif":
            case ".tiff":
                isImage = true;
                break;

            default:
                isImage = false;
                break;
            }


            this.OwnerContext = context;

            float xRes = 0;
            float yRes = 0;

            ExitCodes result = (ExitCodes)NativeMethods.CreateDocumentFromFile(context.NativeContext, fileName, isImage ? 1 : 0, ref NativeDocument, ref PageCount, ref xRes, ref yRes);


            if (xRes > 72)
            {
                this.ImageXRes = xRes;
            }
            else
            {
                this.ImageXRes = 72;
            }

            if (yRes > 72)
            {
                this.ImageYRes = yRes;
            }
            else
            {
                this.ImageYRes = 72;
            }

            switch (result)
            {
            case ExitCodes.EXIT_SUCCESS:
                break;

            case ExitCodes.ERR_CANNOT_OPEN_FILE:
                throw new MuPDFException("Cannot open document", result);

            case ExitCodes.ERR_CANNOT_COUNT_PAGES:
                throw new MuPDFException("Cannot count pages", result);

            default:
                throw new MuPDFException("Unknown error", result);
            }

            Pages        = new MuPDFPageCollection(context, this, PageCount);
            DisplayLists = new MuPDFDisplayList[PageCount];
        }