/// <summary>
        /// It open dialog to load a PDF File
        /// </summary>
        private void OpenDocument()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() == true)
            {
                try
                {
                    var doc = new PDFDoc(openFileDialog.FileName);
                    doc.InitSecurityHandler();

                    PDFViewer.CurrentDocument = doc;
                    _undoManager = doc.GetUndoManager(); // Get document Undo Redo Manager

                    NotifyPropertyChanged(nameof(ToolsEnabled));
                }
                catch (Exception)
                {
                    throw new Exception("OpenDocument() failed");
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            // The first step in every application using PDFNet is to initialize the
            // library and set the path to common PDF resources. The library is usually
            // initialized only once, but calling Initialize() multiple times is also fine.
            PDFNet.Initialize();

            // Relative path to the folder containing test files.
            string input_path  = "../../TestFiles/";
            string output_path = "../../TestFiles/Output/";

            try
            {
                // Open the PDF document.
                using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
                    using (ElementBuilder bld = new ElementBuilder())      // Used to build new Element objects
                        using (ElementWriter writer = new ElementWriter()) // Used to write Elements to the page
                        {
                            UndoManager undo_manager = doc.GetUndoManager();

                            // Take a snapshot to which we can undo after making changes.
                            ResultSnapshot snap0 = undo_manager.TakeSnapshot();

                            DocSnapshot snap0_state = snap0.CurrentState();

                            Page page = doc.PageCreate(); // Start a new page

                            writer.Begin(page);           // Begin writing to this page

                            // ----------------------------------------------------------
                            // Add JPEG image to the file
                            Image   img     = Image.Create(doc, input_path + "peppers.jpg");
                            Element element = bld.CreateImage(img, new Matrix2D(200, 0, 0, 250, 50, 500));
                            writer.WritePlacedElement(element);

                            writer.End(); // Finish writing to the page
                            doc.PagePushFront(page);

                            // Take a snapshot after making changes, so that we can redo later (after undoing first).
                            ResultSnapshot snap1 = undo_manager.TakeSnapshot();

                            if (snap1.PreviousState().Equals(snap0_state))
                            {
                                Console.WriteLine("snap1 previous state equals snap0_state; previous state is correct");
                            }

                            DocSnapshot snap1_state = snap1.CurrentState();

                            doc.Save(output_path + "addimage.pdf", SDFDoc.SaveOptions.e_incremental);

                            if (undo_manager.CanUndo())
                            {
                                ResultSnapshot undo_snap = undo_manager.Undo();

                                doc.Save(output_path + "addimage_undone.pdf", SDFDoc.SaveOptions.e_incremental);

                                DocSnapshot undo_snap_state = undo_snap.CurrentState();

                                if (undo_snap_state.Equals(snap0_state))
                                {
                                    Console.WriteLine("undo_snap_state equals snap0_state; undo was successful");
                                }

                                if (undo_manager.CanRedo())
                                {
                                    ResultSnapshot redo_snap = undo_manager.Redo();

                                    doc.Save(output_path + "addimage_redone.pdf", SDFDoc.SaveOptions.e_incremental);

                                    if (redo_snap.PreviousState().Equals(undo_snap_state))
                                    {
                                        Console.WriteLine("redo_snap previous state equals undo_snap_state; previous state is correct");
                                    }

                                    DocSnapshot redo_snap_state = redo_snap.CurrentState();

                                    if (redo_snap_state.Equals(snap1_state))
                                    {
                                        Console.WriteLine("Snap1 and redo_snap are equal; redo was successful");
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("Problem encountered - cannot redo.");
                                }
                            }
                            else
                            {
                                Console.WriteLine("Problem encountered - cannot undo.");
                            }
                        }
            }
            catch (PDFNetException e)
            {
                Console.WriteLine(e.Message);
            }
        }
        public MainViewModel()
        {
            // Initilizes PDFNet
            PDFNet.Initialize();

            // Make sure to Terminate any processes
            Application.Current.SessionEnding += Current_SessionEnding;

            // Init all Commands
            CMDOpenDocument = new Relaycommand(OpenDocument);
            CMDNextPage     = new Relaycommand(NextPage);
            CMDPreviousPage = new Relaycommand(PreviousPage);

            // Annotations
            CMDAnottateText        = new Relaycommand(AddTextSample);
            CMDFreeTextCreate      = new Relaycommand(AddFreeTextSample);
            CMDSelectText          = new Relaycommand(SelectText);
            CMDSquareCreate        = new Relaycommand(AddSquareAnnotation);
            CMDArrowCreate         = new Relaycommand(AddArrowAnnotation);
            CMDOvalCreate          = new Relaycommand(AddOvalAnnotation);
            CMDSquigglyCreate      = new Relaycommand(AddSquigglyAnnotation);
            CMDUnderlineCreate     = new Relaycommand(AddUnderlineAnnotation);
            CMDStrikeoutCreate     = new Relaycommand(AddStrikeoutAnnotation);
            CMDTextHighlightCreate = new Relaycommand(AddHighlightAnnotation);
            CMDInkCreate           = new Relaycommand(AddInkAnnotation);

            CMDExit    = new Relaycommand(ExitApp);
            CMDZoomIn  = new Relaycommand(ZoomIn);
            CMDZoomOut = new Relaycommand(ZoomOut);
            CMDUndo    = new Relaycommand(Undo);
            CMDRedo    = new Relaycommand(Redo);

            // Checks the scale factor to determine the right resolution
            PresentationSource source      = PresentationSource.FromVisual(Application.Current.MainWindow);
            double             scaleFactor = 1;

            if (source != null)
            {
                scaleFactor = 1 / source.CompositionTarget.TransformFromDevice.M11;
            }

            // Set working doc to Viewer
            PDFViewer = new PDFViewWPF();
            PDFViewer.PixelsPerUnitWidth = scaleFactor;
            PDFViewer.SetPagePresentationMode(PDFViewWPF.PagePresentationMode.e_single_continuous);
            PDFViewer.AllowDrop = true;

            // PDF Viewer Events subscription
            PDFViewer.MouseLeftButtonDown += PDFView_MouseLeftButtonDown;
            PDFViewer.Drop += PDFViewer_Drop;

            // Enable access to the Tools available
            _toolManager = new ToolManager(PDFViewer);
            _toolManager.AnnotationAdded   += _toolManager_AnnotationAdded;
            _toolManager.AnnotationRemoved += _toolManager_AnnotationRemoved;

            // Load PDF file
            PDFDoc doc = new PDFDoc("./Resources/GettingStarted.pdf");

            doc.InitSecurityHandler();
            _undoManager = doc.GetUndoManager();
            PDFViewer.SetDoc(doc);
        }