Example #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: \"Xfinium.Pdf 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: \"Xfinium.Pdf 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: \"Xfinium.Pdf Actions Sample\"});";
            document.AfterPrintAction = jsAfterPrintAction;
        }
        private static string getTextFromLink(PdfGoToAction linkAction)
        {
            PdfPage targetPage = linkAction.View.Page;

            if (targetPage == null)
            {
                return(String.Empty);
            }

            StringBuilder result = new StringBuilder();

            const float eps = 5.0f; // small reserve for text start vertical position
            PdfCollection <PdfTextData> textFromTargetPage = targetPage.Canvas.GetTextData();

            foreach (PdfTextData textData in textFromTargetPage)
            {
                if (textData.Position.Y < targetPage.Height - linkAction.View.Top - eps)
                {
                    continue;
                }

                result.Append(textData.GetText() + " ");
            }

            return(result.ToString());
        }
Example #3
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: \"Xfinium.Pdf 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: \"Xfinium.Pdf 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: \"Xfinium.Pdf Actions Sample\"});";
            document.AfterPrintAction = jsAfterPrintAction;
        }
Example #4
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);
        }
Example #5
0
        private void button1_Click(object sender, EventArgs e)
        {
            //pdf file
            string input = "..\\..\\..\\..\\..\\..\\Data\\Sample5.pdf";

            //open a pdf document
            PdfDocument doc = new PdfDocument(input);

            //get the first page
            PdfPageBase page = doc.Pages[0];

            //set pdf destination
            PdfDestination dest = new PdfDestination(page);

            dest.Mode     = PdfDestinationMode.Location;
            dest.Location = new PointF(-40f, -40f);
            dest.Zoom     = 0.6f;

            //set action
            PdfGoToAction gotoAction = new PdfGoToAction(dest);

            doc.AfterOpenAction = gotoAction;

            string output = "SetZoomFactor.pdf";

            //save pdf document
            doc.SaveToFile(output);

            //Launching the Pdf file
            PDFDocumentViewer(output);
        }
Example #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            //pdf file
            string input = "..\\..\\..\\..\\..\\..\\Data\\Sample3.pdf";

            //open pdf document
            PdfDocument doc = new PdfDocument(input);

            //get the fourth page
            PdfPageBase page = doc.Pages[3];

            //create a destination
            PdfDestination destination = new PdfDestination(page);

            //create GoToAction instance
            PdfGoToAction action = new PdfGoToAction(destination);

            //set the action to execute when the document is opened.
            doc.AfterOpenAction = action;

            string output = "SpecifyPageToView.pdf";

            //save pdf document
            doc.SaveToFile(output);

            //Launching the Pdf file
            PDFDocumentViewer(output);
        }
        private static LinkInfo getFirstLink(PdfDocument pdf)
        {
            int i = 0;

            foreach (PdfWidget widget in pdf.GetWidgets())
            {
                PdfActionArea actionArea = widget as PdfActionArea;
                if (actionArea != null)
                {
                    PdfGoToAction linkAction = actionArea.Action as PdfGoToAction;
                    if (linkAction != null)
                    {
                        // lets ignore links which point to an absent page
                        if (linkAction.View.Page != null)
                        {
                            return(new LinkInfo(pdf, actionArea, i));
                        }
                    }
                }

                i++;
            }

            return(null);
        }
Example #8
0
        /// <summary>
        /// Creates a link within the current document using a named destination.
        /// </summary>
        /// <param name="rect">The link area in default page coordinates.</param>
        /// <param name="destinationName">The named destination's name.</param>
        public static PdfLinkAnnotation CreateDocumentLink(PdfRectangle rect, string destinationName)
        {
            PdfLinkAnnotation link = new PdfLinkAnnotation();

            link._linkType = LinkType.NamedDestination;
            link.Rectangle = rect;
            link._action   = PdfGoToAction.CreateGoToAction(destinationName);
            return(link);
        }
Example #9
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.Graphics.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.xfiniumsoft.com/";

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

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.outlines.pdf") };
            return output;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //Draw pages
            PdfPageBase lastPage = DrawPages(doc);

            //script
            String script
                = "app.alert({"
                  + "    cMsg: \"I'll lead; you must follow me.\","
                  + "    nIcon: 3,"
                  + "    cTitle: \"JavaScript Action\""
                  + "});";
            PdfJavaScriptAction action1 = new PdfJavaScriptAction(script);

            doc.AfterOpenAction = action1;

            //script
            script
                = "app.alert({"
                  + "    cMsg: \"The firt page!\","
                  + "    nIcon: 3,"
                  + "    cTitle: \"JavaScript Action\""
                  + "});";
            PdfJavaScriptAction action2 = new PdfJavaScriptAction(script);

            action1.NextAction = action2;

            PdfDestination dest = new PdfDestination(lastPage);

            dest.Zoom = 1;
            PdfGoToAction action3 = new PdfGoToAction(dest);

            action2.NextAction = action3;

            //script
            script
                = "app.alert({"
                  + "    cMsg: \"Oh sorry, it's the last page. I'm missing!\","
                  + "    nIcon: 3,"
                  + "    cTitle: \"JavaScript Action\""
                  + "});";
            PdfJavaScriptAction action4 = new PdfJavaScriptAction(script);

            action3.NextAction = action4;

            //Save pdf file.
            doc.SaveToFile("ActionChain.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("ActionChain.pdf");
        }
Example #11
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            doc.LoadFromFile(@"..\..\..\..\..\..\Data\GetZoomFactor.pdf");
            PdfGoToAction action    = doc.AfterOpenAction as PdfGoToAction;
            float         zoomvalue = action.Destination.Zoom;

            MessageBox.Show("The zoom factor of the document is " + zoomvalue * 100 + "%.");
        }
Example #12
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //Draw pages
            PdfPageBase lastPage = DrawPages(doc);

            //script
            String script
                = "app.alert({"
                + "    cMsg: \"I'll lead; you must follow me.\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
            PdfJavaScriptAction action1 = new PdfJavaScriptAction(script);
            doc.AfterOpenAction = action1;

            //script
            script
                = "app.alert({"
                + "    cMsg: \"The firt page!\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
            PdfJavaScriptAction action2 = new PdfJavaScriptAction(script);
            action1.NextAction = action2;

            PdfDestination dest = new PdfDestination(lastPage);
            dest.Zoom = 1;
            PdfGoToAction action3 = new PdfGoToAction(dest);
            action2.NextAction = action3;

            //script
            script
                = "app.alert({"
                + "    cMsg: \"Oh sorry, it's the last page. I'm missing!\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
            PdfJavaScriptAction action4 = new PdfJavaScriptAction(script);
            action3.NextAction = action4;

            //Save pdf file.
            doc.SaveToFile("ActionChain.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("ActionChain.pdf");
        }
Example #13
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].Graphics.DrawString("Go To actions:", font, blackBrush, 400, 240);

                document.Pages[i].Graphics.DrawRectangle(blackPen, 400, 260, 200, 20);
                slo.X = 500;
                slo.Y = 270;
                document.Pages[i].Graphics.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.VisualRectangle = new PdfVisualRectangle(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;
            }
        }
Example #14
0
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            using (PdfDocument pdf = new PdfDocument())
            {
                PdfPage secondPage = pdf.AddPage();
                secondPage.Canvas.DrawString(10, 300, "Go-to action target");

                PdfGoToAction action     = pdf.CreateGoToPageAction(1, 300);
                PdfActionArea annotation = pdf.Pages[0].AddActionArea(10, 50, 100, 30, action);
                annotation.BorderColor       = new PdfRgbColor(255, 0, 0);
                annotation.BorderDashPattern = new PdfDashPattern(new float[] { 3, 2 });

                pdf.Save("GoToAction.pdf");
            }

            Process.Start("GoToAction.pdf");
        }
            public LinkInfo(PdfDocument pdf, PdfActionArea actionArea, int index)
            {
                if (pdf == null)
                {
                    throw new ArgumentNullException("document");
                }

                if (actionArea == null)
                {
                    throw new ArgumentNullException("actionArea");
                }

                Action = actionArea.Action as PdfGoToAction;
                if (Action == null)
                {
                    throw new ArgumentException("Action area doesn't contain link", "actionArea");
                }

                Index            = index;
                Bounds           = actionArea.BoundingBox;
                OwnerPageNumber  = pdf.IndexOf(actionArea.Owner);
                TargetPageNumber = pdf.IndexOf(Action.View.Page);
            }
Example #16
0
        private static void BuildTestOutline(PdfDocument pdf)
        {
            PdfOutlineItem root       = pdf.OutlineRoot;
            PdfOutlineItem lastParent = null;

            PdfFont times     = pdf.AddFont(PdfBuiltInFont.TimesItalic);
            double  pageWidth = pdf.GetPage(0).Width;

            PdfPage page = pdf.GetPage(pdf.PageCount - 1);

            for (int i = 1; i < 30; i++)
            {
                if (i > 1)
                {
                    page = pdf.AddPage();
                }

                page.Canvas.Font     = times;
                page.Canvas.FontSize = 16;

                string titleFormat = string.Format("Page {0}", i);

                double textWidth = page.Canvas.GetTextWidth(titleFormat);
                page.Canvas.DrawString(new PdfPoint((pageWidth - textWidth) / 2, 100), titleFormat);

                PdfGoToAction action = pdf.CreateGoToPageAction(i - 1, 0);

                if (i == 1 || i == 10 || i == 20)
                {
                    lastParent = root.AddChild(titleFormat, action);
                }
                else
                {
                    lastParent.AddChild(titleFormat, action);
                }
            }
        }
        private static void JumpToSpecificLocationAction(PdfDocument pdf, PdfPageBase page)
        {
            //add a new page
            PdfPageBase pagetwo = pdf.Pages.Add();

            //draw text on the page
            pagetwo.Canvas.DrawString("This is Page Two.",
                                      new PdfFont(PdfFontFamily.Helvetica, 20f),
                                      new PdfSolidBrush(Color.Black),
                                      10, 10);

            //create PdfDestination instance and link to PdfGoToAction
            PdfDestination pageBottomDest = new PdfDestination(pagetwo);

            pageBottomDest.Location = new PointF(0, 5);
            pageBottomDest.Mode     = PdfDestinationMode.Location;
            pageBottomDest.Zoom     = 1f;
            PdfGoToAction action = new PdfGoToAction(pageBottomDest);

            PdfTrueTypeFont buttonFont   = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
            float           buttonWidth  = 70;
            float           buttonHeight = buttonFont.Height * 1.5f;
            PdfStringFormat format2      = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            RectangleF      buttonBounds = new RectangleF(0, 200, buttonWidth, buttonHeight);

            //create a rectangle and draw text
            page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds);
            page.Canvas.DrawString("To Last Page", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2);

            //add the annotation
            PdfActionAnnotation annotation
                = new PdfActionAnnotation(buttonBounds, action);

            annotation.Border = new PdfAnnotationBorder(0.75f);
            annotation.Color  = Color.LightGray;
            (page as PdfNewPage).Annotations.Add(annotation);
        }
Example #18
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Load file from disk.
            doc.LoadFromFile(@"..\..\..\..\..\..\Data\Sample.pdf");

            //Create a PdfDestination with specific page, location and 50% zoom factor
            PdfDestination dest = new PdfDestination(2, new PointF(0, 100), 0.5f);

            //Create GoToAction with dest
            PdfGoToAction action = new PdfGoToAction(dest);

            //Set open action
            doc.AfterOpenAction = action;

            String result = "SpecifyPageToView_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
Example #19
0
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            using (PdfDocument pdf = new PdfDocument())
            {
                PdfPage secondPage = pdf.AddPage();
                secondPage.Canvas.DrawString(10, 300, "Go-to action target");

                PdfGoToAction action     = pdf.CreateGoToPageAction(1, 300);
                PdfActionArea annotation = pdf.Pages[0].AddActionArea(10, 50, 100, 30, action);
                annotation.Border.Color       = new PdfRgbColor(255, 0, 0);
                annotation.Border.DashPattern = new PdfDashPattern(new float[] { 3, 2 });
                annotation.Border.Width       = 1;
                annotation.Border.Style       = PdfMarkerLineStyle.Dashed;

                pdf.Save("GoToAction.pdf");
            }

            Console.WriteLine($"The output is located in {Environment.CurrentDirectory}");
        }
        /// <summary>
        /// Adds navigation button.
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="buttonText"></param>
        private void NavigateToNextPage(PdfPage currentPage, string buttonText)
        {
            // Create a Button field.
            PdfButtonField submitButton = new PdfButtonField(page, buttonText);

            submitButton.Bounds  = new RectangleF(page.GetClientSize().Width - 30, page.GetClientSize().Height - x, 25, 15);
            submitButton.Font    = pdfFont;
            submitButton.ToolTip = buttonText;

            // Add a new page.
            page = document.Pages.Add();

            // Create an instance of PdfDestination.
            PdfDestination dest = new PdfDestination(page, new PointF(0, 100));

            // Create an instance of GoTo action.
            PdfGoToAction goToAction = new PdfGoToAction(page);

            // Update the destination for the action.
            goToAction.Destination = dest;

            // Set action for the field.
            submitButton.Actions.GotFocus = goToAction;
        }
Example #21
0
        protected void buttonCreatePdf_Click(object sender, EventArgs e)
        {
            // create a PDF document
            PdfDocument document = new PdfDocument();

            // set a demo serial number
            document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // create the true type fonts that can be used in document
            System.Drawing.Font ttfFont           = new System.Drawing.Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point);
            PdfFont             newTimesFont      = document.CreateFont(ttfFont);
            PdfFont             newTimesFontEmbed = document.CreateFont(ttfFont, true);

            // create page 1
            PdfPage page1 = document.AddPage();
            // create a text object to be laid out on this page
            PdfText text1 = new PdfText(10, 10, "This is the Page 1 of the document with Open action", newTimesFontEmbed);

            // layout the text
            page1.Layout(text1);

            // create page 2
            PdfPage page2 = document.AddPage();
            // create a text object to be laid out on this page
            PdfText text2 = new PdfText(10, 10, "This is the Page 2 of the document with Open action", newTimesFontEmbed);

            // layout the text
            page2.Layout(text2);

            // create page 1
            PdfPage page3 = document.AddPage();
            // create a text object to be laid out on this page
            PdfText text3 = new PdfText(10, 10, "This is the Page 3 of the document with Open action", newTimesFontEmbed);

            // layout the text
            page3.Layout(text3);

            if (radioButtonJavaScript.Checked)
            {
                // display an alert message when the document is opened

                string alertMessage   = textBoxAlertMessage.Text;
                string javaScriptCode = "app.alert({cMsg: \"" + alertMessage + "\", cTitle: \"Open Document JavaScript Action\"});";

                // create the JavaScript action to display the alert
                PdfJavaScriptAction javaScriptAction = new PdfJavaScriptAction(javaScriptCode);

                // set the document JavaScript open action
                document.SetOpenAction(javaScriptAction);
            }
            else
            {
                // go to a given page in document and set the given zoom level when the document is opened

                int pageIndex = radioButtonPage1.Checked ? 0 : (radioButtonPage2.Checked ? 1 : 2);
                int zoomLevel = int.Parse(textBoxZoomLevel.Text);

                PdfDestination openDestination = new PdfDestination(document.Pages[pageIndex], new System.Drawing.PointF(10, 10));
                openDestination.Zoom = zoomLevel;
                PdfGoToAction goToAction = new PdfGoToAction(openDestination);

                // set the document GoTo open action
                document.SetOpenAction(goToAction);
            }

            try
            {
                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

                // inform the browser about the binary data format
                HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

                // let the browser know how to open the PDF document and the file name
                HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=OpenAction.pdf; size={0}",
                                                                                            pdfBuffer.Length.ToString()));

                // write the PDF buffer to HTTP response
                HttpContext.Current.Response.BinaryWrite(pdfBuffer);

                // call End() method of HTTP response to stop ASP.NET page processing
                HttpContext.Current.Response.End();
            }
            finally
            {
                document.Close();
            }
        }
Example #22
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;

            // Create one page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

            float y = 10;

            //title
            PdfBrush brush1 = PdfBrushes.Black;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
            page.Canvas.DrawString("Part List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
            y = y + font1.MeasureString("Part List", format1).Height;
            y = y + 2;

            //table top
            PdfDestination tableTopDest = new PdfDestination(page);
            tableTopDest.Location = new PointF(0, y);
            tableTopDest.Mode = PdfDestinationMode.Location;
            tableTopDest.Zoom = 1f;

            //Draw table            
            PdfTrueTypeFont buttonFont = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
            float buttonWidth = 70;
            float buttonHeight = buttonFont.Height * 1.5f;
            float tableTop = y;
            PdfLayoutResult tableLayoutResult = DrawTable(page, y + buttonHeight + 5);

            //table bottom
            PdfDestination tableBottomDest = new PdfDestination(tableLayoutResult.Page);
            tableBottomDest.Location = new PointF(0, tableLayoutResult.Bounds.Bottom);
            tableBottomDest.Mode = PdfDestinationMode.Location;
            tableBottomDest.Zoom = 1f;

            //go to table bottom
            float x = page.Canvas.ClientSize.Width - buttonWidth;
            PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            RectangleF buttonBounds = new RectangleF(x, tableTop, buttonWidth, buttonHeight);
            page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds);
            page.Canvas.DrawString("To Bottom", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2);
            PdfGoToAction action1 = new PdfGoToAction(tableBottomDest);
            PdfActionAnnotation annotation1
                = new PdfActionAnnotation(buttonBounds, action1);
            annotation1.Border = new PdfAnnotationBorder(0.75f);
            annotation1.Color = Color.LightGray;
            (page as PdfNewPage).Annotations.Add(annotation1);

            //go to table top
            float tableBottom = tableLayoutResult.Bounds.Bottom + 5;
            buttonBounds = new RectangleF(x, tableBottom, buttonWidth, buttonHeight);
            tableLayoutResult.Page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds);
            tableLayoutResult.Page.Canvas.DrawString("To Top", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2);
            PdfGoToAction action2 = new PdfGoToAction(tableTopDest);
            PdfActionAnnotation annotation2
                = new PdfActionAnnotation(buttonBounds, action2);
            annotation2.Border = new PdfAnnotationBorder(0.75f);
            annotation2.Color = Color.LightGray;
            (tableLayoutResult.Page as PdfNewPage).Annotations.Add(annotation2);

            //goto last page
            PdfNamedAction action3 = new PdfNamedAction(PdfActionDestination.LastPage);
            doc.AfterOpenAction = action3;

            //script
            String script
                = "app.alert({"
                + "    cMsg: \"Oh no, you want to leave me.\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
            PdfJavaScriptAction action4 = new PdfJavaScriptAction(script);
            doc.BeforeCloseAction = action4;

            //Save pdf file.
            doc.SaveToFile("Action.pdf");
            doc.Close();

            //Launching the Pdf file.
            PDFDocumentViewer("Action.pdf");
        }
Example #23
0
        public ActionResult CreatePdf(FormCollection collection)
        {
            // create a PDF document
            PdfDocument document = new PdfDocument();

            // set a demo serial number
            document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // create the true type fonts that can be used in document
            System.Drawing.Font ttfFont           = new System.Drawing.Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point);
            PdfFont             newTimesFont      = document.CreateFont(ttfFont);
            PdfFont             newTimesFontEmbed = document.CreateFont(ttfFont, true);

            // create page 1
            PdfPage page1 = document.AddPage();
            // create a text object to be laid out on this page
            PdfText text1 = new PdfText(10, 10, "This is the Page 1 of the document with Open action", newTimesFontEmbed);

            // layout the text
            page1.Layout(text1);

            // create page 2
            PdfPage page2 = document.AddPage();
            // create a text object to be laid out on this page
            PdfText text2 = new PdfText(10, 10, "This is the Page 2 of the document with Open action", newTimesFontEmbed);

            // layout the text
            page2.Layout(text2);

            // create page 1
            PdfPage page3 = document.AddPage();
            // create a text object to be laid out on this page
            PdfText text3 = new PdfText(10, 10, "This is the Page 3 of the document with Open action", newTimesFontEmbed);

            // layout the text
            page3.Layout(text3);

            if (collection["ActionType"] == "radioButtonJavaScript")
            {
                // display an alert message when the document is opened

                string alertMessage   = collection["textBoxAlertMessage"];
                string javaScriptCode = "app.alert({cMsg: \"" + alertMessage + "\", cTitle: \"Open Document JavaScript Action\"});";

                // create the JavaScript action to display the alert
                PdfJavaScriptAction javaScriptAction = new PdfJavaScriptAction(javaScriptCode);

                // set the document JavaScript open action
                document.SetOpenAction(javaScriptAction);
            }
            else
            {
                // go to a given page in document and set the given zoom level when the document is opened

                int pageIndex = collection["PageNumber"] == "radioButtonPage1" ? 0 : (collection["PageNumber"] == "radioButtonPage2" ? 1 : 2);
                int zoomLevel = int.Parse(collection["textBoxZoomLevel"]);

                PdfDestination openDestination = new PdfDestination(document.Pages[pageIndex], new System.Drawing.PointF(10, 10));
                openDestination.Zoom = zoomLevel;
                PdfGoToAction goToAction = new PdfGoToAction(openDestination);

                // set the document GoTo open action
                document.SetOpenAction(goToAction);
            }

            try
            {
                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

                FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
                fileResult.FileDownloadName = "OpenAction.pdf";

                return(fileResult);
            }
            finally
            {
                document.Close();
            }
        }
Example #24
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.Graphics.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.xfiniumsoft.com/";

            PdfOutlineItem webOutline = new PdfOutlineItem();

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

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "xfinium.pdf.sample.outlines.pdf") };
            return(output);
        }
Example #25
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a pdf document
            PdfDocument doc = new PdfDocument();

            //Set margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins       margin   = new PdfMargins();

            margin.Top    = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left   = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right  = margin.Left;

            //Create one page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

            float y = 10;

            //Title
            PdfBrush        brush1  = PdfBrushes.Black;
            PdfTrueTypeFont font1   = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));
            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);

            page.Canvas.DrawString("Part List", font1, brush1, page.Canvas.ClientSize.Width / 2, y, format1);
            y = y + font1.MeasureString("Part List", format1).Height;
            y = y + 2;

            //Table top
            PdfDestination tableTopDest = new PdfDestination(page);

            tableTopDest.Location = new PointF(0, y);
            tableTopDest.Mode     = PdfDestinationMode.Location;
            tableTopDest.Zoom     = 1f;

            //Draw table
            PdfTrueTypeFont buttonFont        = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold));
            float           buttonWidth       = 70;
            float           buttonHeight      = buttonFont.Height * 1.5f;
            float           tableTop          = y;
            PdfLayoutResult tableLayoutResult = DrawTable(page, y + buttonHeight + 5);

            //Table bottom
            PdfDestination tableBottomDest = new PdfDestination(tableLayoutResult.Page);

            tableBottomDest.Location = new PointF(0, tableLayoutResult.Bounds.Bottom);
            tableBottomDest.Mode     = PdfDestinationMode.Location;
            tableBottomDest.Zoom     = 1f;

            //Go to table bottom
            float           x            = page.Canvas.ClientSize.Width - buttonWidth;
            PdfStringFormat format2      = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            RectangleF      buttonBounds = new RectangleF(x, tableTop, buttonWidth, buttonHeight);

            page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds);
            page.Canvas.DrawString("To Bottom", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2);
            PdfGoToAction       action1 = new PdfGoToAction(tableBottomDest);
            PdfActionAnnotation annotation1
                = new PdfActionAnnotation(buttonBounds, action1);

            annotation1.Border = new PdfAnnotationBorder(0.75f);
            annotation1.Color  = Color.LightGray;
            (page as PdfNewPage).Annotations.Add(annotation1);

            //Go to table top
            float tableBottom = tableLayoutResult.Bounds.Bottom + 5;

            buttonBounds = new RectangleF(x, tableBottom, buttonWidth, buttonHeight);
            tableLayoutResult.Page.Canvas.DrawRectangle(PdfBrushes.DarkGray, buttonBounds);
            tableLayoutResult.Page.Canvas.DrawString("To Top", buttonFont, PdfBrushes.CadetBlue, buttonBounds, format2);
            PdfGoToAction       action2 = new PdfGoToAction(tableTopDest);
            PdfActionAnnotation annotation2
                = new PdfActionAnnotation(buttonBounds, action2);

            annotation2.Border = new PdfAnnotationBorder(0.75f);
            annotation2.Color  = Color.LightGray;
            (tableLayoutResult.Page as PdfNewPage).Annotations.Add(annotation2);

            //Go to last page
            PdfNamedAction action3 = new PdfNamedAction(PdfActionDestination.LastPage);

            doc.AfterOpenAction = action3;

            //Script
            String script
                = "app.alert({"
                  + "    cMsg: \"Oh no, you want to leave me.\","
                  + "    nIcon: 3,"
                  + "    cTitle: \"JavaScript Action\""
                  + "});";
            PdfJavaScriptAction action4 = new PdfJavaScriptAction(script);

            doc.BeforeCloseAction = action4;

            //Save pdf file
            doc.SaveToFile("Action.pdf");
            doc.Close();

            //Launch the Pdf file
            PDFDocumentViewer("Action.pdf");
        }
Example #26
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].Graphics.DrawString("Go To actions:", font, blackBrush, 400, 240);

                document.Pages[i].Graphics.DrawRectangle(blackPen, 400, 260, 200, 20);
                slo.X = 500;
                slo.Y = 270;
                document.Pages[i].Graphics.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.VisualRectangle = new PdfVisualRectangle(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;
            }
        }