예제 #1
0
        //private void OnBookmarkSelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        //{
        //    Bookmark newValue = (Bookmark)e.NewValue;
        //    if (newValue != null)
        //    {
        //        document.Document.Navigator.GoToBookmark(newValue);
        //        this.destinationRectangle = newValue.GetDestinationRectangle((int)(this.document.Page.Width * this.GlobalScale), (int)(this.document.Page.Height * this.GlobalScale), null);
        //    }
        //}


        /// <summary>
        /// Updates the view location.
        /// </summary>
        /// <param name="destinationInfo">The destination info.</param>
        private void UpdateViewLocation(Apitron.PDF.Rasterizer.Rectangle destinationInfo)
        {
            if (destinationInfo == null)
            {
                this.PageScroller.ScrollToTop();
                return;
            }
            double value = zoomValue;
            double scale = value;

            double horizontalScale = this.PageScroller.ViewportWidth / this.PageImage.ActualWidth;
            double verticalScale   = this.PageScroller.ViewportHeight / this.PageImage.ActualHeight;

            if (destinationInfo.Bottom != 0 && destinationInfo.Right != this.PageImage.ActualWidth)
            {
                double expectedHScale = this.PageScroller.ViewportWidth / destinationInfo.Height;
                double expectidVScale = this.PageScroller.ViewportHeight / destinationInfo.Width;
                horizontalScale = expectedHScale;
                verticalScale   = expectidVScale;

                scale = Math.Min(verticalScale, horizontalScale);
            }

            this.PageScroller.ScrollToHorizontalOffset(destinationInfo.Left * scale);
            this.PageScroller.ScrollToVerticalOffset((this.PageImage.ActualHeight - destinationInfo.Top) * scale);
        }
예제 #2
0
        private void SetImageSource(byte[] image, IList <Link> links, int width, int height)
        {
            BitmapSource source = BitmapSource.Create(width, height, 72, 72, PixelFormats.Bgr32, null, image, 4 * width);

            this.PageImage.Source = source;
            this.PageImage.Width  = width;
            this.PageImage.Height = height;

            this.PageCanvas.Width  = width;
            this.PageCanvas.Height = height;

            for (int i = 1; i < this.PageCanvas.Children.Count; i++)
            {
                Button rectangle = (Button)this.PageCanvas.Children[i];
                rectangle.Click -= this.OnLinkClick;
            }

            this.PageCanvas.Children.RemoveRange(1, this.PageCanvas.Children.Count);

            foreach (Link link in links)
            {
                Apitron.PDF.Rasterizer.Rectangle location = link.GetLocationRectangle(width, height, null);
                Button rectangle = new Button();
                rectangle.Opacity = 0;
                rectangle.Cursor  = Cursors.Hand;
                rectangle.Width   = location.Right - location.Left;
                rectangle.Height  = location.Top - location.Bottom;
                Canvas.SetLeft(rectangle, location.Left);
                Canvas.SetBottom(rectangle, location.Bottom);
                rectangle.Click      += this.OnLinkClick;
                rectangle.DataContext = link;
                this.PageCanvas.Children.Add(rectangle);
            }

            this.UpdateImageZoom();
            this.UpdateViewLocation(this.destinationRectangle);

            ////Ý tưởng bookmark 1
            //Document doc = document.Document;
            //DocumentNavigator navigator = doc == null ? null : doc.Navigator;
            //navigator.Move(2, Origin.Begin);
        }
예제 #3
0
        static void Main(string[] args)
        {
            // prepare graphics objects
            Bitmap renderedPage   = null;
            Brush  highlightBrush = new SolidBrush(Color.FromArgb(126, 255, 255, 0));

            //  store rendering settings
            Resolution        renderingResolution = new Resolution(144, 144);
            RenderingSettings renderingSettings   = new RenderingSettings();
            Page firstPage = null;

            // a list of rects to highlight
            IList <RectangleF> highlightRects = new List <RectangleF>();

            // open PDF document
            using (FileStream fs = new FileStream("../../files/test.pdf", FileMode.Open, FileAccess.Read,
                                                  FileShare.ReadWrite))
            {
                using (Document doc = new Document(fs))
                {
                    firstPage = doc.Pages[0];

                    renderedPage = firstPage.Render(renderingResolution, renderingSettings);

                    // parse links and store highlight rects
                    foreach (Link link in firstPage.Links)
                    {
                        if (link.IsUriLink)
                        {
                            Apitron.PDF.Rasterizer.Rectangle locationRect =
                                link.GetLocationRectangle(renderingResolution, renderingSettings);

                            highlightRects.Add(TransformToGDIRect(locationRect, renderedPage.Height));

                            Console.WriteLine(link.DestinationUri);
                        }
                    }
                }
            }

            // search text in the same document using regular expression matching URLs
            using (SearchIndex search = new SearchIndex(new FileStream("../../files/test.pdf", FileMode.Open, FileAccess.Read,
                                                                       FileShare.ReadWrite)))
            {
                search.Search(handlerArgs =>
                {
                    // first page only
                    if (handlerArgs.PageIndex > 0)
                    {
                        handlerArgs.CancelSearch = true;
                        return;
                    }

                    // add highlight rects by processing found items
                    foreach (SearchResultItem item in handlerArgs.ResultItems)
                    {
                        SearchResultRegion searchResultRegion = firstPage.TransformRegion(item.Region, renderingResolution,
                                                                                          renderingSettings);

                        foreach (double[] block in searchResultRegion.Blocks)
                        {
                            float xMin = float.MaxValue;
                            float yMin = float.MaxValue;
                            float xMax = float.MinValue;
                            float yMax = float.MinValue;
                            for (int i = 0; i < block.Length;)
                            {
                                xMin = (float)Math.Min(xMin, block[i]);
                                xMax = (float)Math.Max(xMax, block[i++]);

                                yMin = (float)Math.Min(yMin, block[i]);
                                yMax = (float)Math.Max(yMax, block[i++]);
                            }

                            highlightRects.Add(new RectangleF(xMin, yMin, xMax - xMin, yMax - yMin));
                        }

                        Console.WriteLine(item.Title);
                    }
                },
                              new Regex(@"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?"));
            }

            // render hightllight rects
            HighlightRects(renderedPage, highlightRects, highlightBrush);

            renderedPage.Save("renderedPage.png");
            Process.Start("renderedPage.png");

            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }