Beispiel #1
0
        private static void CreateDocumentActions(PDFFixedDocument document)
        {
            // Create an action that will open the document at the last page with 200% zoom.
            PDFPageDirectDestination pageDestination = new PDFPageDirectDestination();

            pageDestination.Page = document.Pages[document.Pages.Count - 1];
            pageDestination.Zoom = 200;
            pageDestination.Top  = 0;
            pageDestination.Left = 0;
            PDFGoToAction openAction = new PDFGoToAction();

            openAction.Destination = pageDestination;
            document.OpenAction    = openAction;

            // Create an action that is executed when the document is closed.
            PDFJavaScriptAction jsCloseAction = new PDFJavaScriptAction();

            jsCloseAction.Script       = "app.alert({cMsg: \"The document will close now.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});";
            document.BeforeCloseAction = jsCloseAction;

            // Create an action that is executed before the document is printed.
            PDFJavaScriptAction jsBeforePrintAction = new PDFJavaScriptAction();

            jsBeforePrintAction.Script = "app.alert({cMsg: \"The document will be printed.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});";
            document.BeforePrintAction = jsBeforePrintAction;

            // Create an action that is executed after the document is printed.
            PDFJavaScriptAction jsAfterPrintAction = new PDFJavaScriptAction();

            jsAfterPrintAction.Script = "app.alert({cMsg: \"The document has been printed.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});";
            document.AfterPrintAction = jsAfterPrintAction;
        }
Beispiel #2
0
        private static PDFOutlineItem CreateOutline(string title, PDFPage page)
        {
            PDFPageDirectDestination pageDestination = new PDFPageDirectDestination();

            pageDestination.Page = page;
            pageDestination.Top  = 0;
            pageDestination.Left = 0;
            // Inherit current zoom
            pageDestination.Zoom = 0;

            // Create a go to action to be executed when the outline is clicked,
            // the go to action goes to specified destination.
            PDFGoToAction gotoPage = new PDFGoToAction();

            gotoPage.Destination = pageDestination;

            // Create the outline in the table of contents
            PDFOutlineItem outline = new PDFOutlineItem();

            outline.Title       = title;
            outline.VisualStyle = PDFOutlineItemVisualStyle.Italic;
            outline.Action      = gotoPage;

            return(outline);
        }
Beispiel #3
0
        private static void CreateGoToActions(PDFFixedDocument document, PDFFont font)
        {
            PDFPen   blackPen   = new PDFPen(new PDFRgbColor(0, 0, 0), 1);
            PDFBrush blackBrush = new PDFBrush();

            font.Size = 12;
            PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions();

            sao.Brush = blackBrush;
            sao.Font  = font;
            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.HorizontalAlign = PDFStringHorizontalAlign.Center;
            slo.VerticalAlign   = PDFStringVerticalAlign.Middle;

            Random rnd = new Random();

            for (int i = 0; i < document.Pages.Count; i++)
            {
                int destinationPage = rnd.Next(document.Pages.Count);

                document.Pages[i].Canvas.DrawString("Go To actions:", font, blackBrush, 400, 240);

                document.Pages[i].Canvas.DrawRectangle(blackPen, 400, 260, 200, 20);
                slo.X = 500;
                slo.Y = 270;
                document.Pages[i].Canvas.DrawString("Go To page: " + (destinationPage + 1).ToString(), sao, slo);

                // Create a link annotation on top of the widget.
                PDFLinkAnnotation link = new PDFLinkAnnotation();
                document.Pages[i].Annotations.Add(link);
                link.DisplayRectangle = new PDFDisplayRectangle(400, 260, 200, 20);

                // Create a GoTo action and attach it to the link.
                PDFPageDirectDestination pageDestination = new PDFPageDirectDestination();
                pageDestination.Page = document.Pages[destinationPage];
                pageDestination.Left = 0;
                pageDestination.Top  = 0;
                pageDestination.Zoom = 0; // Keep current zoom
                PDFGoToAction gotoPageAction = new PDFGoToAction();
                gotoPageAction.Destination = pageDestination;
                link.Action = gotoPageAction;
            }
        }
Beispiel #4
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run()
        {
            PDFFixedDocument document = new PDFFixedDocument();

            document.DisplayMode = PDFDisplayMode.UseOutlines;

            PDFStandardFont helvetica  = new PDFStandardFont(PDFStandardFontFace.Helvetica, 216);
            PDFBrush        blackBrush = new PDFBrush();

            for (int i = 0; i < 10; i++)
            {
                PDFPage page = document.Pages.Add();
                page.Canvas.DrawString((i + 1).ToString(), helvetica, blackBrush, 50, 50);
            }

            PDFOutlineItem root = new PDFOutlineItem();

            root.Title       = "Contents";
            root.VisualStyle = PDFOutlineItemVisualStyle.Bold;
            root.Color       = new PDFRgbColor(255, 0, 0);
            document.Outline.Add(root);

            for (int i = 0; i < document.Pages.Count; i++)
            {
                // Create a destination to target page.
                PDFPageDirectDestination pageDestination = new PDFPageDirectDestination();
                pageDestination.Page = document.Pages[i];
                pageDestination.Top  = 0;
                pageDestination.Left = 0;
                // Inherit current zoom
                pageDestination.Zoom = 0;

                // Create a go to action to be executed when the outline is clicked,
                // the go to action goes to specified destination.
                PDFGoToAction gotoPage = new PDFGoToAction();
                gotoPage.Destination = pageDestination;

                // Create the outline in the table of contents
                PDFOutlineItem outline = new PDFOutlineItem();
                outline.Title       = string.Format("Go to page {0}", i + 1);
                outline.VisualStyle = PDFOutlineItemVisualStyle.Italic;
                outline.Action      = gotoPage;
                root.Items.Add(outline);
            }
            root.Expanded = true;

            // Create an outline that will launch a link in the browser.
            PDFUriAction uriAction = new PDFUriAction();

            uriAction.URI = "http://www.o2sol.com/";

            PDFOutlineItem webOutline = new PDFOutlineItem();

            webOutline.Title  = "http://www.o2sol.com/";
            webOutline.Color  = new PDFRgbColor(0, 0, 255);
            webOutline.Action = uriAction;
            document.Outline.Add(webOutline);

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "outlines.pdf") };
            return(output);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            // Create the pdf document
            //PDF4NET v5: PDFDocument doc = new PDFDocument();
            PDFFixedDocument doc = new PDFFixedDocument();

            // Create the graphic objects
            //PDF4NET v5: PDFFont fontText = new PDFFont(PDFFontFace.Helvetica, 12);
            PDFStandardFont fontText = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12);

            PDFBrush blackBrush = new PDFBrush(new PDFRgbColor());

            // Create 3 pages
            //PDF4NET v5: PDFPage page = doc.AddPage();
            PDFPage page = doc.Pages.Add();

            //PDF4NET v5: page.Canvas.DrawText("Page 1", fontText, null, blackBrush, 20, 30);
            page.Canvas.DrawString("Page 1", fontText, blackBrush, 20, 30);

            //PDF4NET v5: page = doc.AddPage();
            page = doc.Pages.Add();

            //PDF4NET v5: page.Canvas.DrawText("Page 2", fontText, null, blackBrush, 20, 30);
            page.Canvas.DrawString("Page 2", fontText, blackBrush, 20, 30);

            //PDF4NET v5: page = doc.AddPage();
            page = doc.Pages.Add();

            //PDF4NET v5: page.Canvas.DrawText("Page 3", fontText, null, blackBrush, 20, 30);
            page.Canvas.DrawString("Page 3", fontText, blackBrush, 20, 30);

            /// URI Action
            // create a web action
            //PDF4NET v5: PDFURIAction uriAction = new PDFURIAction();
            PDFUriAction uriAction = new PDFUriAction();

            uriAction.URI = "http://www.o2sol.com/";

            // create a bookmark to point to the url above
            //PDF4NET v5: PDFBookmark url = new PDFBookmark();
            PDFOutlineItem url = new PDFOutlineItem();

            url.Title  = "www.o2sol.com";
            url.Color  = new PDFRgbColor();
            url.Action = uriAction;
            // add the bookmark to the document
            //PDF4NET v5: doc.Bookmarks.Add(url);
            doc.Outline.Add(url);
            /// URI Action

            /// Javascript Action
            // create a javascript action to print the document
            PDFJavaScriptAction jsAction = new PDFJavaScriptAction();

            jsAction.Script = "this.print(true);\n";

            // create a bookmark to point to execute the action
            //PDF4NET v5: PDFBookmark js = new PDFBookmark();
            PDFOutlineItem js = new PDFOutlineItem();

            js.Title  = "Print me from JavaScript";
            js.Color  = new PDFRgbColor();
            js.Action = jsAction;
            // add the bookmark to the document
            //PDF4NET v5: doc.Bookmarks.Add(url);
            doc.Outline.Add(js);
            /// Javascript Action

            /// OpenDocument action
            // Create an action to be executed when the document is opened
            // The action will open the document at the
            // second page of the document using a 800% zoom
            //PDF4NET v5: PDFPageDestination pageDestination = new PDFPageDestination();
            PDFPageDirectDestination pageDestination = new PDFPageDirectDestination();

            pageDestination.Page = doc.Pages[1];
            //PDF4NET v5: pageDestination.MagnificationMode = PDFMagnificationMode.XYZ;
            pageDestination.ZoomMode = PDFDestinationZoomMode.XYZ;

            pageDestination.Left = 0;
            pageDestination.Top  = 0;
            pageDestination.Zoom = 800;
            PDFGoToAction goToAction = new PDFGoToAction();

            goToAction.Destination = pageDestination;

            // set the action on the document
            doc.OpenAction = goToAction;
            /// OpenDocument action

            // Save the document to disk
            doc.Save("Sample_Actions.pdf");
        }