Example #1
0
        private void OnNavigationButtonClick(object sender, RoutedEventArgs e)
        {
            Button            source    = (Button)e.Source;
            Document          doc       = document.Document;
            DocumentNavigator navigator = doc == null ? null : doc.Navigator;

            if (doc == null || navigator == null)
            {
                return;
            }
            switch ((string)source.CommandParameter)
            {
            case "Next":
                navigator.MoveForward();
                break;

            case "Prev":
                navigator.MoveBackward();
                break;

            case "First":
                navigator.Move(0, Origin.Begin);
                //navigator.Move(2, Origin.Begin); => 3rd page
                break;

            case "Last":
                navigator.Move(0, Origin.End);
                break;

            default:
                return;
            }
            this.destinationRectangle = 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 = this.ZoomSlider.Value;
            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.ZoomSlider.Value = scale;
            }

            this.PageScroller.ScrollToHorizontalOffset(destinationInfo.Left * scale);
            this.PageScroller.ScrollToVerticalOffset((this.PageImage.ActualHeight - destinationInfo.Top) * scale);
        }
        private static void Main(string[] args)
        {
            // open and load the file
            using (FileStream fs = new FileStream(@"..\..\..\Documents\testfile.pdf", FileMode.Open))
                using (Document document = new Document(fs))         // this object represents a PDF document
                {
                    // process and save pages one by one
                    for (int i = 0; i < document.Pages.Count; i++)
                    {
                        Page currentPage = document.Pages[i];

                        RenderingSettings settings = new RenderingSettings();
                        settings.BackgroundColor = (uint)Color.BlueViolet.ToArgb();

                        // we'd like to convert half of original PDF page
                        Rectangle rectangle = new Rectangle(0, currentPage.Width / 2, (int)currentPage.Width, (int)currentPage.Height);

                        // we use original page's width and height for image
                        using (Bitmap bitmap = currentPage.Render((int)currentPage.Width, (int)currentPage.Height, rectangle, settings))
                        {
                            bitmap.Save($"{i}.png", ImageFormat.Png);
                        }
                    }

                    // preview first rendered page
                    Process.Start("0.png");
                }
        }
Example #4
0
        private static void SavePageAsBitmapWithHightlightedLinks(Page page, RenderingSettings settings)
        {
            int pageWidth  = (int)page.Width;
            int pageHeight = (int)page.Height;

            // render page
            using (Bitmap bitmap = page.Render(pageWidth, pageHeight, settings))
            {
                // create graphics object that we will use for drawing of the highlighting rects
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    using (Brush highlightBrush = new SolidBrush(Color.FromArgb(0x5FFFFF00)))
                    {
                        foreach (Link link in page.Links)
                        {
                            Apitron.PDF.Rasterizer.Rectangle linkLocation = link.GetLocationRectangle(pageWidth, pageHeight, settings);

                            // PDF coordinate system has Y axis inverted in comparison to GDI, so transform the Y coordinate of the rect here
                            // because link object coordinates will be returned using PDF coordinate system.
                            g.FillRectangle(highlightBrush, new RectangleF((float)linkLocation.Left, pageHeight - (float)linkLocation.Top, (float)linkLocation.Width, (float)linkLocation.Height));
                        }
                    }
                }

                bitmap.Save("Page_with_highlighted_links.png", ImageFormat.Png);
            }
        }
        /// <summary>
        /// Click link in a page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="routedEventArgs"></param>
        private void OnLinkClick(object sender, RoutedEventArgs routedEventArgs)
        {
            Link link = (Link)((Button)sender).DataContext;

            if (link.DestinationUri != null)
            {
                string           linkStr = link.DestinationUri.ToString();
                MessageBoxResult result  = MessageBox.Show($"Are you sure you want to continue connecting \n{linkStr}", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                if (result == MessageBoxResult.Yes)
                {
                    Process.Start(new ProcessStartInfo(linkStr));
                    routedEventArgs.Handled = true;
                }
                else if (result == MessageBoxResult.No)
                {
                    //nothing
                }
            }
            else
            {
                this.document.Document.Navigator.GoToLink(link);

                this.destinationRectangle = link.GetDestinationRectangle((int)(this.document.Page.Width * this.GlobalScale), (int)(this.document.Page.Height * this.GlobalScale), null);
            }
        }
Example #6
0
        private void OnLinkClick(object sender, RoutedEventArgs routedEventArgs)
        {
            Link link = (Link)((Button)sender).DataContext;

            this.document.Document.Navigator.GoToLink(link);

            this.destinationRectangle = link.GetDestinationRectangle((int)(this.document.Page.Width * this.GlobalScale), (int)(this.document.Page.Height * this.GlobalScale), null);
        }
        private void TOCSelectionChanged(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);
            }
        }
        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);
        }
Example #9
0
 /// <summary>
 /// Transforms PDF rect to GDI rect.
 /// </summary>
 /// <param name="locationRect">Rect to transform.</param>
 /// <param name="height">The height of the page.</param>
 /// <returns>Transformed GDI rect.</returns>
 private static RectangleF TransformToGDIRect(Rectangle locationRect, double height)
 {
     return(new RectangleF((float)locationRect.Left, (float)(height - locationRect.Top),
                           (float)locationRect.Width, (float)locationRect.Height));
 }