Exemplo n.º 1
0
        private float AddFileLinkAnnotation(PdfPageBase page, float y)
        {
            PdfTrueTypeFont font   = new PdfTrueTypeFont(new Font("Arial", 12f));
            PdfStringFormat format = new PdfStringFormat();

            format.MeasureTrailingSpaces = true;
            String prompt = "Launch File: ";
            SizeF  size   = font.MeasureString(prompt);

            page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y);
            float x = font.MeasureString(prompt, format).Width;

            String label = @"Launch Notepad.exe";

            size = font.MeasureString(label);
            RectangleF bounds = new RectangleF(x, y, size.Width, size.Height);

            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y);
            PdfFileLinkAnnotation annotation = new PdfFileLinkAnnotation(bounds, @"C:\Windows\Notepad.exe");

            annotation.Color = Color.Blue;
            (page as PdfNewPage).Annotations.Add(annotation);
            y = bounds.Bottom;

            return(y);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //Create a new object of PdfDocument.
            PdfDocument doc = new PdfDocument();

            //Add a page to it.
            PdfPageBase page = doc.Pages.Add();

            //Declare two parameters that will be passed to the constructor of PdfFileLinkAnnotation class.
            RectangleF rect     = new RectangleF(0, 40, 250, 35);
            string     filePath = @"..\..\..\..\..\..\Data\Template_Pdf_3.pdf";

            //Create a file link annotation based on the two parameters and add the annotation to the new page.
            PdfFileLinkAnnotation link = new PdfFileLinkAnnotation(rect, filePath);

            page.AnnotationsWidget.Add(link);

            //Create a free text annotation based on the same RectangleF, specifying the content.
            PdfFreeTextAnnotation text = new PdfFreeTextAnnotation(rect);

            text.Text = "Click here! This is a link annotation.";
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 15);

            text.Font = font;
            page.AnnotationsWidget.Add(text);

            String result = "CreatePdfLinkAnnotation_out.pdf";

            //Save the document
            doc.SaveToFile(result);
            //Launch the Pdf file
            PDFDocumentViewer(result);
        }
        private static void AddFileLinkAnnotation(PdfPageBase page, float y)
        {
            //Define a font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f));

            //Set the string format
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);

            //Text string
            String prompt = "Launch a File: ";

            //Draw text string on page canvas
            page.Canvas.DrawString(prompt, font, PdfBrushes.DodgerBlue, 0, y);

            //Use MeasureString to get the width of string
            float x = font.MeasureString(prompt, format).Width;

            //String of file name
            String label = "Sample.pdf";

            //Use MeasureString to get the SizeF of string
            SizeF size = font.MeasureString(label);

            //Create a rectangle
            RectangleF bounds = new RectangleF(x, y, size.Width, size.Height);

            //Draw label string
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, x, y);

            //Create PdfFileLinkAnnotation on the rectangle and link file "Sample.pdf"
            PdfFileLinkAnnotation annotation = new PdfFileLinkAnnotation(bounds, @"..\..\..\..\..\..\Data\Sample.pdf");

            //Set color for annotation
            annotation.Color = Color.Blue;

            //Add annotation to the page
            (page as PdfNewPage).Annotations.Add(annotation);
        }