Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a new PDF document and add a page to it
            PdfDocument doc  = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            //Create a PDF Launch Action
            PdfLaunchAction launchAction = new PdfLaunchAction("..\\..\\..\\..\\..\\..\\Data\\text.txt");

            //Create a PDF Action Annotation with the PDF Launch Action
            string          text = "Click here to open file";
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
            RectangleF      rect = new RectangleF(50, 50, 230, 15);

            page.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rect);
            PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction);

            //Add the PDF Action Annotation to page
            (page as PdfNewPage).Annotations.Add(annotation);


            String result = "AddPdfLaunchAction_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
Пример #2
0
        /// <summary>
        /// GoToE action
        /// </summary>
        /// <param name="pdf"></param>
        private static void EmbeddedGoToAction(PdfDocument pdf, PdfPageBase page)
        {
            //add a attachment
            PdfAttachment attachment = new PdfAttachment(@"..\..\..\..\..\..\Data\GoToAction.pdf");

            pdf.Attachments.Add(attachment);

            string          text   = "Test embedded go-to action! Click this will open the attached PDF in a new window.";
            PdfTrueTypeFont font   = new PdfTrueTypeFont(new Font("Arial", 13f));
            float           width  = 490f;
            float           height = font.Height * 2.2f;
            RectangleF      rect   = new RectangleF(0, 100, width, height);

            page.Canvas.DrawString(text, font, PdfBrushes.Black, rect);

            //create a PdfDestination with specific page, location and 200% zoom factor
            PdfDestination dest = new PdfDestination(1, new PointF(0, 842), 2f);

            //create GoToE action with dest
            PdfEmbeddedGoToAction action     = new PdfEmbeddedGoToAction(attachment.FileName, dest, true);
            PdfActionAnnotation   annotation = new PdfActionAnnotation(rect, action);

            //add the annotation
            (page as PdfNewPage).Annotations.Add(annotation);
        }
Пример #3
0
        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);
        }
Пример #4
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");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //pdf file
            string input = "..\\..\\..\\..\\..\\..\\Data\\AddTableOfContent.pdf";

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

            doc.LoadFromFile(input);
            //get the page count
            int pageCount = doc.Pages.Count;

            //insert a blank page into the pdf document
            PdfPageBase tocPage = doc.Pages.Insert(0);

            //set title
            string          title           = "Table Of Contents";
            PdfTrueTypeFont titleFont       = new PdfTrueTypeFont(new Font("Arial", 20, FontStyle.Bold));
            PdfStringFormat centerAlignment = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            PointF          location        = new PointF(tocPage.Canvas.ClientSize.Width / 2, titleFont.MeasureString(title).Height);

            tocPage.Canvas.DrawString(title, titleFont, PdfBrushes.CornflowerBlue, location, centerAlignment);

            //draw TOC text
            PdfTrueTypeFont titlesFont = new PdfTrueTypeFont(new Font("Arial", 14));

            String[] titles = new String[pageCount];
            for (int i = 0; i < titles.Length; i++)
            {
                titles[i] = string.Format("This is page{0}", i + 1);
            }
            float y = titleFont.MeasureString(title).Height + 10;
            float x = 0;

            for (int i = 1; i <= pageCount; i++)
            {
                string text      = titles[i - 1];
                SizeF  titleSize = titlesFont.MeasureString(text);

                PdfPageBase navigatedPage = doc.Pages[i];

                string pageNumText     = (i + 1).ToString();
                SizeF  pageNumTextSize = titlesFont.MeasureString(pageNumText);
                tocPage.Canvas.DrawString(text, titlesFont, PdfBrushes.CadetBlue, 0, y);
                float dotLocation     = titleSize.Width + 2 + x;
                float pageNumlocation = tocPage.Canvas.ClientSize.Width - pageNumTextSize.Width;
                for (float j = dotLocation; j < pageNumlocation; j++)
                {
                    if (dotLocation >= pageNumlocation)
                    {
                        break;
                    }
                    tocPage.Canvas.DrawString(".", titlesFont, PdfBrushes.Gray, dotLocation, y);
                    dotLocation += 3;
                }
                tocPage.Canvas.DrawString(pageNumText, titlesFont, PdfBrushes.CadetBlue, pageNumlocation, y);

                //add TOC action
                location = new PointF(0, y);
                RectangleF          titleBounds = new RectangleF(location, new SizeF(tocPage.Canvas.ClientSize.Width, titleSize.Height));
                PdfDestination      Dest        = new PdfDestination(navigatedPage, new PointF(-doc.PageSettings.Margins.Top, -doc.PageSettings.Margins.Left));
                PdfActionAnnotation action      = new PdfActionAnnotation(titleBounds, new PdfGoToAction(Dest));
                action.Border = new PdfAnnotationBorder(0);
                (tocPage as PdfNewPage).Annotations.Add(action);
                y += titleSize.Height + 10;
            }

            string output = "AddTableOfContent.pdf";

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

            //Launching the Pdf file
            PDFDocumentViewer(output);
        }
Пример #6
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");
        }