示例#1
0
        /// <summary>
        /// Creates a new <see cref="RasterImage"/> from the specified file.
        /// </summary>
        /// <param name="fileName">The path to the file containing the image.</param>
        /// <param name="pageNumber">The number of the page in the file from which the image should be created, starting at 0. Only useful for multi-page formats, such as PDF.</param>
        /// <param name="scale">The scale factor at which to render the image.</param>
        /// <param name="alpha">A boolean value indicating whether transparency (alpha) data from the image should be preserved or not.</param>
        /// <param name="interpolate">A boolean value indicating whether the image should be interpolated when it is resized or not.</param>
        public RasterImageFile(string fileName, int pageNumber = 0, double scale = 1, bool alpha = true, bool interpolate = true)
        {
            using (MuPDFContext context = new MuPDFContext())
            {
                using (MuPDFDocument document = new MuPDFDocument(context, fileName))
                {
                    RoundedRectangle roundedBounds = document.Pages[pageNumber].Bounds.Round(scale);

                    this.Width  = roundedBounds.Width;
                    this.Height = roundedBounds.Height;

                    this.HasAlpha    = alpha;
                    this.Interpolate = interpolate;

                    this.Id = Guid.NewGuid().ToString();

                    int imageSize = document.GetRenderedSize(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB);

                    this.ImageDataAddress = Marshal.AllocHGlobal(imageSize);
                    this.DataHolder       = new DisposableIntPtr(this.ImageDataAddress);
                    GC.AddMemoryPressure(imageSize);

                    document.Render(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB, this.ImageDataAddress);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Invoked by the "Open file" button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void OpenFileClicked(object sender, RoutedEventArgs e)
        {
            //Show a dialog with the supported file types.
            OpenFileDialog dialog = new OpenFileDialog()
            {
                AllowMultiple = false,
                Title         = "Open document...",
            };

            string[] result = await dialog.ShowAsync(this);

            if (result.Length == 1)
            {
                //First of all we need the PDFRenderer to stop doing anything with the Document.
                this.FindControl <PDFRenderer>("MuPDFRenderer").ReleaseResources();

                //Now we can dispose the document.
                Document?.Dispose();

                //Create a new document and initialise the PDFRenderer with it.
                Document = new MuPDFDocument(Context, result[0]);

                MaxPageNumber = Document.Pages.Count;
                this.FindControl <PDFRenderer>("MuPDFRenderer").Initialize(Document, ocrLanguage: GetCurrentLanguage(this.FindControl <ComboBox>("OCRLanguageBox").SelectedIndex));
                this.FindControl <Image>("PageAreaImage").Source = GenerateThumbnail();

                //Set up the FileWatcher to keep track of any changes to the file.
                Watcher.EnableRaisingEvents = false;
                Watcher.Path   = Path.GetDirectoryName(result[0]);
                Watcher.Filter = Path.GetFileName(result[0]);
                Watcher.EnableRaisingEvents = true;
            }
        }
示例#3
0
        static void Main()
        {
            //Initialise the MuPDF context. This is needed to open or create documents.
            using MuPDFContext ctx = new MuPDFContext();

            //Open a PDF document
            using MuPDFDocument doc1 = new MuPDFDocument(ctx, "Document1.pdf");

            //Save the page as a PNG image with transparency, at a 1x zoom level (1pt = 1px).
            doc1.SaveImage(0, 1, PixelFormats.RGBA, "Raster1.png", RasterOutputFileTypes.PNG);

            //Open an Open XPS document
            using MuPDFDocument doc2 = new MuPDFDocument(ctx, "Document2.oxps");

            //Save only part of the page as a PNG image with transparency, at a 2x zoom level (1pt = 2px).
            doc2.SaveImage(0, new Rectangle(87, 360, 517, 790), 2, PixelFormats.RGBA, "Raster2.png", RasterOutputFileTypes.PNG);

            //Merge the two documents into a single document.
            MuPDFDocument.CreateDocument(ctx, "Merged.pdf", DocumentOutputFileTypes.PDF, true,

                                         //We take the full page from the first document
                                         (doc1.Pages[0], doc1.Pages[0].Bounds, 1),

                                         //We only take a region of the page from the second document
                                         (doc2.Pages[0], new Rectangle(87, 360, 517, 790), 1)
                                         );
        }
示例#4
0
        /// <summary>
        /// Creates a new <see cref="RasterImage"/> from the specified stream.
        /// </summary>
        /// <param name="imageStream">The stream containing the image data.</param>
        /// <param name="fileType">The type of the image contained in the stream.</param>
        /// <param name="pageNumber">The number of the page in the file from which the image should be created, starting at 0. Only useful for multi-page formats, such as PDF.</param>
        /// <param name="scale">The scale factor at which to render the image.</param>
        /// <param name="alpha">A boolean value indicating whether transparency (alpha) data from the image should be preserved or not.</param>
        /// <param name="interpolate">A boolean value indicating whether the image should be interpolated when it is resized or not.</param>
        public RasterImageStream(Stream imageStream, InputFileTypes fileType, int pageNumber = 0, double scale = 1, bool alpha = true, bool interpolate = true)
        {
            using (MuPDFContext context = new MuPDFContext())
            {
                IntPtr originalImageAddress;
                long   originalImageLength;

                IDisposable toBeDisposed = null;
                GCHandle    handleToFree;

                if (imageStream is MemoryStream ms)
                {
                    int origin = (int)ms.Seek(0, SeekOrigin.Begin);
                    originalImageLength = ms.Length;

                    handleToFree         = GCHandle.Alloc(ms.GetBuffer(), GCHandleType.Pinned);
                    originalImageAddress = handleToFree.AddrOfPinnedObject();
                }
                else
                {
                    MemoryStream mem = new MemoryStream((int)imageStream.Length);
                    imageStream.CopyTo(mem);

                    toBeDisposed = mem;

                    int origin = (int)mem.Seek(0, SeekOrigin.Begin);
                    originalImageLength = mem.Length;

                    handleToFree         = GCHandle.Alloc(mem.GetBuffer(), GCHandleType.Pinned);
                    originalImageAddress = handleToFree.AddrOfPinnedObject();
                }

                using (MuPDFDocument document = new MuPDFDocument(context, originalImageAddress, originalImageLength, fileType))
                {
                    RoundedRectangle roundedBounds = document.Pages[pageNumber].Bounds.Round(scale);

                    this.Width  = roundedBounds.Width;
                    this.Height = roundedBounds.Height;

                    this.HasAlpha    = alpha;
                    this.Interpolate = interpolate;

                    this.Id = Guid.NewGuid().ToString();

                    int imageSize = document.GetRenderedSize(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB);

                    this.ImageDataAddress = Marshal.AllocHGlobal(imageSize);
                    this.DataHolder       = new DisposableIntPtr(this.ImageDataAddress);
                    GC.AddMemoryPressure(imageSize);

                    document.Render(pageNumber, scale, alpha ? MuPDFCore.PixelFormats.RGBA : MuPDFCore.PixelFormats.RGB, this.ImageDataAddress);
                }

                handleToFree.Free();
                toBeDisposed?.Dispose();
            }
        }
示例#5
0
        /// <summary>
        /// Invoked when the window is opened.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WindowOpened(object sender, EventArgs e)
        {
            //Render the initial PDF and initialise the PDFRenderer with it.
            MemoryStream ms = RenderInitialPDF();

            Context  = new MuPDFContext();
            Document = new MuPDFDocument(Context, ref ms, InputFileTypes.PDF);

            MaxPageNumber = 1;
            this.FindControl <PDFRenderer>("MuPDFRenderer").Initialize(Document, ocrLanguage: GetCurrentLanguage(this.FindControl <ComboBox>("OCRLanguageBox").SelectedIndex));
            this.FindControl <Image>("PageAreaImage").Source = GenerateThumbnail();

            //Start the UI updater thread.
            UIUpdater();
        }
示例#6
0
        /// <summary>
        /// Render the page to a PNG stream.
        /// </summary>
        /// <param name="page">The <see cref="Page"/> to render.</param>
        /// <param name="stream">The stream to which the PNG data will be written.</param>
        /// <param name="scale">The scale to be used when rasterising the page. This will determine the width and height of the image file.</param>
        public static void SaveAsPNG(this Page page, Stream stream, double scale = 1)
        {
            Document doc = new Document();

            doc.Pages.Add(page);

            MemoryStream ms = new MemoryStream();

            doc.SaveAsPDF(ms);

            using (MuPDFContext context = new MuPDFContext())
            {
                using (MuPDFDocument muDoc = new MuPDFDocument(context, ref ms, InputFileTypes.PDF))
                {
                    muDoc.WriteImage(0, scale, MuPDFCore.PixelFormats.RGBA, stream, RasterOutputFileTypes.PNG);
                }
            }
        }
示例#7
0
        /// <summary>
        /// Invoked when the file that has been opened changes.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileChanged(object sender, FileSystemEventArgs e)
        {
            Dispatcher.UIThread.InvokeAsync(() =>
            {
                //Keep track of the current DisplayArea.
                Rect displayArea = this.FindControl <PDFRenderer>("MuPDFRenderer").DisplayArea;

                //Close the current document and reopen it.
                this.FindControl <PDFRenderer>("MuPDFRenderer").ReleaseResources();
                Document?.Dispose();
                Document      = new MuPDFDocument(Context, e.FullPath);
                MaxPageNumber = Document.Pages.Count;
                this.FindControl <PDFRenderer>("MuPDFRenderer").Initialize(Document, ocrLanguage: GetCurrentLanguage(this.FindControl <ComboBox>("OCRLanguageBox").SelectedIndex));
                this.FindControl <Image>("PageAreaImage").Source = GenerateThumbnail();

                //Restore the DisplayArea.
                this.FindControl <PDFRenderer>("MuPDFRenderer").SetDisplayAreaNow(displayArea);
            });
        }
示例#8
0
        static void Main()
        {
            //Initialise the MuPDF context. This is needed to open or create documents.
            using MuPDFContext ctx = new MuPDFContext();

            //Open a PDF document
            using MuPDFDocument doc1 = new MuPDFDocument(ctx, "Document1.pdf");

            //Save the page as a PNG image with transparency, at a 1x zoom level (1pt = 1px).
            doc1.SaveImage(0, 1, PixelFormats.RGBA, "Raster1.png", RasterOutputFileTypes.PNG);

            //Open an Open XPS document
            using MuPDFDocument doc2 = new MuPDFDocument(ctx, "Document2.oxps");

            //Save only part of the page as a PNG image with transparency, at a 2x zoom level (1pt = 2px).
            doc2.SaveImage(0, new Rectangle(87, 360, 517, 790), 2, PixelFormats.RGBA, "Raster2.png", RasterOutputFileTypes.PNG);

            //Merge the two documents into a single document.
            MuPDFDocument.CreateDocument(ctx, "Merged.pdf", DocumentOutputFileTypes.PDF, true,

                                         //We take the full page from the first document
                                         (doc1.Pages[0], doc1.Pages[0].Bounds, 1),

                                         //We only take a region of the page from the second document
                                         (doc2.Pages[0], new Rectangle(87, 360, 517, 790), 1)
                                         );

            //Open the rasterised page so that we can try using the OCR.
            using MuPDFDocument doc3 = new MuPDFDocument(ctx, "Raster1.png");

            //Get a structured text representation of the page using OCR.
            MuPDFStructuredTextPage page = doc3.GetStructuredTextPage(0, new TesseractLanguage(TesseractLanguage.Fast.Eng));

            //Print all the text lines.
            foreach (MuPDFStructuredTextBlock blk in page)
            {
                foreach (MuPDFStructuredTextLine line in blk)
                {
                    System.Console.WriteLine(line.Text);
                }
            }
        }
示例#9
0
        /// <summary>
        /// Parses an image URI into a page. This is intended to replace the default image URI interpreter in <c>VectSharp.SVG.Parser.ParseImageURI</c>. To do this, use something like:
        /// <code>VectSharp.SVG.Parser.ParseImageURI = VectSharp.MuPDFUtils.ImageURIParser.Parser(VectSharp.SVG.Parser.ParseSVGURI);</code>
        /// </summary>
        /// <param name="parseSVG">A function to parse an SVG image uri into a page. You should pass <c>VectSharp.SVG.Parser.ParseSVGURI</c> as this argument.</param>
        /// <returns>A function to parse an image URI into a page.</returns>
        public static Func <string, bool, Page> Parser(Func <string, bool, Page> parseSVG)
        {
            return((string uri, bool interpolate) =>
            {
                if (uri.StartsWith("data:"))
                {
                    string mimeType = uri.Substring(uri.IndexOf(":") + 1, uri.IndexOf(";") - uri.IndexOf(":") - 1);

                    string type = uri.Substring(uri.IndexOf(";") + 1, uri.IndexOf(",") - uri.IndexOf(";") - 1);

                    if (mimeType != "image/svg+xml")
                    {
                        int offset = uri.IndexOf(",") + 1;

                        byte[] parsed;

                        bool isVector = false;

                        InputFileTypes fileType;

                        switch (mimeType)
                        {
                        case "image/png":
                            fileType = InputFileTypes.PNG;
                            break;

                        case "image/jpeg":
                        case "image/jpg":
                            fileType = InputFileTypes.JPEG;
                            break;

                        case "image/gif":
                            fileType = InputFileTypes.GIF;
                            break;

                        case "image/bmp":
                            fileType = InputFileTypes.BMP;
                            break;

                        case "image/tiff":
                        case "image/tif":
                            fileType = InputFileTypes.TIFF;
                            break;

                        case "application/oxps":
                        case "application/vnd.ms-xpsdocument":
                            fileType = InputFileTypes.XPS;
                            isVector = true;
                            break;

                        case "application/x-cbz":
                            fileType = InputFileTypes.CBZ;
                            break;

                        case "application/epub+zip":
                            fileType = InputFileTypes.EPUB;
                            isVector = true;
                            break;

                        case "text/fb2+xml":
                            fileType = InputFileTypes.FB2;
                            break;

                        case "image/x-portable-anymap":
                            fileType = InputFileTypes.PNM;
                            break;

                        case "image/x-portable-arbitrarymap":
                            fileType = InputFileTypes.PAM;
                            break;

                        case "application/pdf":
                            fileType = InputFileTypes.PDF;
                            isVector = true;
                            break;

                        default:
                            fileType = InputFileTypes.PDF;
                            break;
                        }

                        string substring = uri.Substring(offset);


                        switch (type)
                        {
                        case "base64":
                            parsed = Convert.FromBase64String(uri.Substring(offset));
                            break;

                        case "":
                            parsed = (from el in System.Web.HttpUtility.UrlDecode(uri.Substring(offset)) select(byte) el).ToArray();
                            break;

                        default:
                            throw new InvalidDataException("Unknown data stream type!");
                        }

                        if (!isVector)
                        {
                            GCHandle handle = GCHandle.Alloc(parsed, GCHandleType.Pinned);

                            RasterImageStream img = new RasterImageStream(handle.AddrOfPinnedObject(), parsed.Length, fileType, interpolate: interpolate);

                            handle.Free();

                            Page pag = new Page(img.Width, img.Height);

                            pag.Graphics.DrawRasterImage(0, 0, img);

                            return pag;
                        }
                        else
                        {
                            string tempFile = Path.GetTempFileName();

                            using (MuPDFContext context = new MuPDFContext())
                            {
                                using (MuPDFDocument document = new MuPDFDocument(context, parsed, fileType))
                                {
                                    MuPDFDocument.CreateDocument(context, tempFile, DocumentOutputFileTypes.SVG, true, document.Pages[0]);
                                }
                            }

                            string tbr = "data:image/svg+xml;," + System.Web.HttpUtility.UrlEncode(File.ReadAllText(tempFile));

                            File.Delete(tempFile);

                            return parseSVG(tbr, interpolate);
                        }
                    }
                    else
                    {
                        return parseSVG(uri, interpolate);
                    }
                }

                return null;
            });
        }