Example #1
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            PdfAcroForm form   = PdfAcroForm.GetAcroForm(pdfDoc, true);

            PdfTextFormField field = PdfFormField.CreateText(pdfDoc,
                                                             new Rectangle(36, 788, 523, 18), "myfile", "");

            // If true is passed, then the text entered in the field will represent the pathname of a file
            // whose contents are to be submitted as the value of the field.
            field.SetFileSelect(true);

            // When the mouse is released inside the annotation's area (that's what PdfName.U stands for),
            // then the focus will be set on the "mytitle" field.
            field.SetAdditionalAction(PdfName.U, PdfAction.CreateJavaScript(
                                          "this.getField('myfile').browseForFileToSubmit();"
                                          + "this.getField('mytitle').setFocus();"));
            form.AddField(field);

            PdfTextFormField title = PdfFormField.CreateText(pdfDoc,
                                                             new Rectangle(36, 752, 523, 18), "mytitle", "");

            form.AddField(title);

            pdfDoc.Close();
        }
Example #2
0
        public virtual void CreatePdf(String dest)
        {
            PdfDocument pdf      = new PdfDocument(new PdfWriter(dest));
            Document    document = new Document(pdf);
            Link        link     = new Link("here", PdfAction.CreateJavaScript("app.alert('Boo!');"));
            Paragraph   p        = new Paragraph().Add("Click ").Add(link.SetFontColor(ColorConstants.BLUE)).Add(" if you want to be scared."
                                                                                                                 );

            document.Add(p);
            document.Close();
        }
Example #3
0
        public virtual void CreateJavaScriptTest()
        {
            String      fileName = "createJavaScriptTest.pdf";
            PdfDocument document = CreateDocument(new PdfWriter(destinationFolder + fileName), false);
            String      javaScriptRotatePages = "this.setPageRotations(0,2,90)";

            document.GetPage(1).SetAdditionalAction(PdfName.O, PdfAction.CreateJavaScript(javaScriptRotatePages));
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + fileName, sourceFolder
                                                                             + "cmp_" + fileName, destinationFolder, "diff_"));
        }
 public virtual void ActionCheck11()
 {
     NUnit.Framework.Assert.That(() => {
         PdfWriter writer = new PdfWriter(new ByteArrayOutputStream());
         Stream @is       = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
         PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent("Custom", ""
                                                                                                        , "http://www.color.org", "sRGB IEC61966-2.1", @is));
         doc.GetCatalog().SetAdditionalAction(PdfName.C, PdfAction.CreateJavaScript("js"));
         doc.Close();
     }
                                 , NUnit.Framework.Throws.TypeOf <PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.A_CATALOG_DICTIONARY_SHALL_NOT_CONTAIN_AA_ENTRY));
     ;
 }
Example #5
0
 public virtual void ActionCheck13()
 {
     NUnit.Framework.Assert.That(() => {
         PdfWriter writer = new PdfWriter(new ByteArrayOutputStream());
         Stream @is       = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
         PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_2B, new PdfOutputIntent("Custom", ""
                                                                                                        , "http://www.color.org", "sRGB IEC61966-2.1", @is));
         PdfPage page = doc.AddNewPage();
         page.SetAdditionalAction(PdfName.C, PdfAction.CreateJavaScript("js"));
         doc.Close();
     }
                                 , NUnit.Framework.Throws.TypeOf <PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.PageDictionaryShallNotContainAAEntry));
     ;
 }
 public virtual void ActionCheck09()
 {
     NUnit.Framework.Assert.That(() => {
         PdfWriter writer = new PdfWriter(new ByteArrayOutputStream());
         Stream @is       = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
         PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent("Custom", ""
                                                                                                        , "http://www.color.org", "sRGB IEC61966-2.1", @is));
         PdfPage page = doc.AddNewPage();
         page.SetAdditionalAction(PdfName.C, PdfAction.CreateJavaScript("js"));
         doc.Close();
     }
                                 , NUnit.Framework.Throws.InstanceOf <PdfAConformanceException>().With.Message.EqualTo(MessageFormatUtil.Format(PdfAConformanceException._0_ACTIONS_ARE_NOT_ALLOWED, PdfName.JavaScript.GetValue())))
     ;
 }
Example #7
0
        public virtual void CreatePdf(String dest)
        {
            PdfDocument pdf      = new PdfDocument(new PdfWriter(dest));
            Document    document = new Document(pdf);
            PdfAction   action   = PdfAction.CreateJavaScript("app.alert('Boo');");

            action.Next(PdfAction.CreateGoToR(new FileInfo(C06E04_TOC_GoToNamed.DEST).Name, 1, true));
            Link      link = new Link("here", action);
            Paragraph p    = new Paragraph().Add("Click ").Add(link.SetFontColor(ColorConstants.BLUE)).Add(" if you want to be scared."
                                                                                                           );

            document.Add(p);
            document.Close();
        }
Example #8
0
        public virtual void SoundAndNextJavaScriptActionTest()
        {
            String      fileName = "soundAndNextJavaScriptActionTest.pdf";
            PdfDocument document = CreateDocument(new PdfWriter(destinationFolder + fileName), false);
            Stream      @is      = new FileStream(sourceFolder + "sample.aif", FileMode.Open, FileAccess.Read);
            PdfStream   sound1   = new PdfStream(document, @is);

            sound1.Put(PdfName.R, new PdfNumber(32117));
            sound1.Put(PdfName.E, PdfName.Signed);
            sound1.Put(PdfName.B, new PdfNumber(16));
            sound1.Put(PdfName.C, new PdfNumber(1));
            PdfAction action = PdfAction.CreateSound(sound1);

            action.Next(PdfAction.CreateJavaScript("this.setPageRotations(0,2,90)"));
            document.GetPage(2).SetAdditionalAction(PdfName.O, action);
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + fileName, sourceFolder
                                                                             + "cmp_" + fileName, destinationFolder, "diff_"));
        }
        public virtual void CreatePdf(String dest)
        {
            PdfDocument   pdf      = new PdfDocument(new PdfWriter(dest));
            Document      document = new Document(pdf);
            PdfAction     js       = PdfAction.CreateJavaScript("app.alert('Boo!');");
            PdfAnnotation la1      = ((PdfLinkAnnotation) new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0)).SetHighlightMode(
                                          PdfAnnotation.HIGHLIGHT_INVERT).SetAction(js)).SetBorderStyle(PdfAnnotation.STYLE_UNDERLINE);
            Link link1 = new Link("here", (PdfLinkAnnotation)la1);

            document.Add(new Paragraph().Add("Click ").Add(link1).Add(" if you want to be scared."));
            PdfAnnotation la2 = new PdfLinkAnnotation(new Rectangle(0, 0, 0, 0)).SetDestination(PdfExplicitDestination
                                                                                                .CreateFit(2)).SetHighlightMode(PdfAnnotation.HIGHLIGHT_PUSH).SetBorderStyle(PdfAnnotation.STYLE_INSET
                                                                                                                                                                             );
            Link link2 = new Link("next page", (PdfLinkAnnotation)la2);

            document.Add(new Paragraph().Add("Go to the ").Add(link2).Add(" if you're too scared."));
            document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
            document.Add(new Paragraph().Add("There, there, everything is OK."));
            document.Close();
        }
Example #10
0
        // ********************************************************************
        // Fct:     CreateAnnot
        //
        // Descr:   -
        //
        // Owner:   erst
        // ********************************************************************
        public static void CreateAnnot(String src, String dest)
        {
            try
            {
                // Initialize PDF document
                PdfDocument ScrPdf  = new PdfDocument(new PdfReader(src));
                PdfPage     SrcPage = null;


                PdfDocument destPdf = new PdfDocument(new PdfWriter(dest));
                PageSize    ps      = PageSize.A4;
                destPdf.SetDefaultPageSize(ps);

                string idx = String.Empty;


                for (int i = 1; i <= 3; i++)
                {
                    idx = i.ToString();
                    // Add a page from Source PDF
                    SrcPage = ScrPdf.GetPage(i);
                    destPdf.AddPage(SrcPage.CopyTo(destPdf));
                    // Add empty page
                    //destPdf.AddNewPage(PageSize.A4);

                    PdfPage page = destPdf.GetPage(i);

                    // add title
                    PdfFont   fontHelvetica = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
                    PdfCanvas canvas        = new PdfCanvas(page);
                    canvas.BeginText().SetFontAndSize(fontHelvetica, 8).MoveText(420, 770).ShowText("My Title " + idx).EndText();

                    //Add acroform
                    PdfAcroForm form = PdfAcroForm.GetAcroForm(destPdf, true);
                    //Create text field
                    PdfTextFormField title = PdfTextFormField.CreateText(destPdf, new Rectangle(420, 750, 60, 10), idx + "_Title", idx + ".Page", fontHelvetica, 6);
                    PdfTextFormField user1 = PdfTextFormField.CreateText(destPdf, new Rectangle(420, 740, 60, 10), idx + "_User", "enter User", fontHelvetica, 6);
                    PdfTextFormField date1 = PdfTextFormField.CreateText(destPdf, new Rectangle(420, 730, 60, 10), idx + "_Date", "enter date" + idx, fontHelvetica, 6);
                    form.AddField(title);
                    form.AddField(user1);
                    form.AddField(date1);

                    //// Create Button Check

                    PdfButtonFormField button = PdfFormField.CreatePushButton(destPdf, new Rectangle(420, 720, 19, 10), idx + "_BtnOK", "OK");
                    //button.SetAction(PdfAction.CreateResetForm(new String[] { "name", "language", "experience1", "experience2", "experience3", "shift", "info" }, 0));
                    PdfAction action = new PdfAction();
                    PdfAction date   = PdfAction.CreateJavaScript("AFDate_FormatEx(\"yyyy-mm-dd\");");

                    //PdfAction date = PdfAction.CreateNamed
                    //action.Put(button.GetFormType(), PdfName.F);

                    button.SetAction(date);
                    form.AddField(button);
                }


                ScrPdf.Close();
                destPdf.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
        }
Example #11
0
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

            pdf.GetCatalog().SetOpenAction(PdfDestination.MakeDestination(new PdfString("toc")));
            pdf.GetCatalog().SetAdditionalAction(PdfName.WC, PdfAction.CreateJavaScript("app.alert('Thank you for reading');"
                                                                                        ));
            pdf.AddNewPage().SetAdditionalAction(PdfName.O, PdfAction.CreateJavaScript("app.alert('This is where it starts!');"
                                                                                       ));
            // Initialize document
            Document document = new Document(pdf);
            PdfFont  font     = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont  bold     = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);

            document.SetTextAlignment(TextAlignment.JUSTIFIED).SetHyphenation(new HyphenationConfig("en", "uk", 3, 3))
            .SetFont(font).SetFontSize(11);
            StreamReader sr = File.OpenText(SRC);
            String       name;
            String       line;
            Paragraph    p;
            bool         title   = true;
            int          counter = 0;
            IList <Util.Pair <String, Util.Pair <String, int> > > toc = new List <Util.Pair
                                                                                  <String, Util.Pair <String, int> > >();

            while ((line = sr.ReadLine()) != null)
            {
                p = new Paragraph(line);
                p.SetKeepTogether(true);
                if (title)
                {
                    name = String.Format("title{0:00}", counter++);
                    p.SetFont(bold).SetFontSize(12).SetKeepWithNext(true).SetDestination(name);
                    title = false;
                    document.Add(p);
                    toc.Add(new Util.Pair <string, Util.Pair <string, int> >(name, new Util.Pair <string, int>(line, pdf.GetNumberOfPages())));
                }
                else
                {
                    p.SetFirstLineIndent(36);
                    if (String.IsNullOrEmpty(line))
                    {
                        p.SetMarginBottom(12);
                        title = true;
                    }
                    else
                    {
                        p.SetMarginBottom(0);
                    }
                    document.Add(p);
                }
            }
            document.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
            p = new Paragraph().SetFont(bold).Add("Table of Contents").SetDestination("toc");
            document.Add(p);
            toc.RemoveAt(0);
            IList <TabStop> tabstops = new List <TabStop>();

            tabstops.Add(new TabStop(580, TabAlignment.RIGHT, new DottedLine()));
            foreach (Util.Pair <String, Util.Pair <String, int> > entry in toc)
            {
                Util.Pair <String, int> text = entry.Value;
                p = new Paragraph().AddTabStops(tabstops).Add(text.Key).Add(new Tab()).Add(text.Value.ToString()).SetAction
                        (PdfAction.CreateGoTo(entry.Key));
                document.Add(p);
            }
            PdfPage page = pdf.GetLastPage();

            page.SetAdditionalAction(PdfName.C, PdfAction.CreateJavaScript("app.alert('Goodbye last page!');"));
            //Close document
            document.Close();
        }