SetAction() public method

Sets an action for this Chunk.
public SetAction ( PdfAction action ) : Chunk
action iTextSharp.text.pdf.PdfAction the action
return Chunk
Exemplo n.º 1
0
// ---------------------------------------------------------------------------    
    /**
     * Create a table that can be used as a footer
     * @param pagenumber the page that will use the table as footer
     * @param total the total number of pages
     * @return a tabel
     */
    public PdfPTable CreateNavigationTable(int pagenumber, int total) {
      PdfPTable table = new PdfPTable(4);
      table.DefaultCell.Border = Rectangle.NO_BORDER;
      table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
      Chunk first = new Chunk(((char)220).ToString(), SYMBOL);
      first.SetAction(actions[0]);
      table.AddCell(new Phrase(first));
      Chunk previous = new Chunk(((char)172).ToString(), SYMBOL);
      previous.SetAction(actions[pagenumber - 2 < 0 ? 0 : pagenumber - 2]);
      table.AddCell(new Phrase(previous));
      Chunk next = new Chunk(((char)174).ToString(), SYMBOL);
      next.SetAction(actions[pagenumber >= total ? total - 1 : pagenumber]);
      table.AddCell(new Phrase(next));
      Chunk last = new Chunk(((char)222).ToString(), SYMBOL);
      last.SetAction(actions[total - 1]);
      table.AddCell(new Phrase(last));
      table.TotalWidth = 120;
      return table;
    }    
Exemplo n.º 2
0
 // ---------------------------------------------------------------------------
 /**
  * Manipulates a PDF file src with the file dest as result (localhost)
  * @param src the original PDF
  */
 public byte[] ManipulatePdf(byte[] src)
 {
     // Create the reader
     PdfReader reader = new PdfReader(src);
     int n = reader.NumberOfPages;
     using (MemoryStream ms = new MemoryStream())
     {
         // Create the stamper
         using (PdfStamper stamper = new PdfStamper(reader, ms))
         {
             // Add JavaScript
             jsString = File.ReadAllText(
               Path.Combine(Utility.ResourceJavaScript, RESOURCE)
             );
             stamper.JavaScript = jsString;
             // Create a Chunk with a chained action
             PdfContentByte canvas;
             Chunk chunk = new Chunk("print this page");
             PdfAction action = PdfAction.JavaScript(
               "app.alert('Think before you print!');",
               stamper.Writer
             );
             action.Next(PdfAction.JavaScript(
               "printCurrentPage(this.pageNum);",
               stamper.Writer
             ));
             action.Next(new PdfAction("http://www.panda.org/savepaper/"));
             chunk.SetAction(action);
             Phrase phrase = new Phrase(chunk);
             // Add this Chunk to every page
             for (int i = 0; i < n; )
             {
                 canvas = stamper.GetOverContent(++i);
                 ColumnText.ShowTextAligned(
                   canvas, Element.ALIGN_RIGHT, phrase, 816, 18, 0
                 );
             }
         }
         return ms.ToArray();
     }
 }
Exemplo n.º 3
0
        // ---------------------------------------------------------------------------
        /**
         * Creates the PDF.
         * @return the bytes of a PDF file.
         */
        public byte[] CreateMoviePage(Movie movie)
        {
            using (MemoryStream ms = new MemoryStream()) {
            // step 1
            using (Document document = new Document()) {
              // step 2
              PdfWriter.GetInstance(document, ms);
              // step 3
              document.Open();
              // step 4
              Paragraph p = new Paragraph(
            movie.MovieTitle,
            FontFactory.GetFont(
              BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 16
            )
              );
              document.Add(p);
              document.Add(Chunk.NEWLINE);
              PdfPTable table = new PdfPTable(WIDTHS);
              table.AddCell(Image.GetInstance(
            String.Format(RESOURCE, movie.Imdb)
              ));
              PdfPCell cell = new PdfPCell();
              cell.AddElement(new Paragraph("Year: " + movie.Year.ToString()));
              cell.AddElement(new Paragraph("Duration: " + movie.Duration.ToString()));
              table.AddCell(cell);
              document.Add(table);

              PdfTargetDictionary target = new PdfTargetDictionary(false);
              target.AdditionalPath = new PdfTargetDictionary(false);
              Chunk chunk = new Chunk("Go to original document");
              PdfAction action = PdfAction.GotoEmbedded(
            null, target, new PdfString("movies"), false
              );
              chunk.SetAction(action);
              document.Add(chunk);
            }
            return ms.ToArray();
              }
        }
Exemplo n.º 4
0
// --------------------------------------------------------------------------- 
    public void Write(Stream stream) {
      // step 1
      using (Document document = new Document()) {
        // step 2
        PdfWriter writer = PdfWriter.GetInstance(document, stream);
        // step 3
        document.Open();
        // step 4
        Image img = Image.GetInstance(IMG_BOX);
        document.Add(img);
        List list = new List(List.UNORDERED, 20);
        PdfDestination dest = new PdfDestination(PdfDestination.FIT);
        dest.AddFirst(new PdfNumber(1));
        IEnumerable<Movie> box = PojoFactory.GetMovies(1)
          .Concat(PojoFactory.GetMovies(4))
        ;
        foreach (Movie movie in box) {
          if (movie.Year > 1960) {
            PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(
              writer, null,
              String.Format("kubrick_{0}.pdf", movie.Imdb),
              CreateMoviePage(movie)
            );
            fs.AddDescription(movie.Title, false);
            writer.AddFileAttachment(fs);
            ListItem item = new ListItem(movie.MovieTitle);
            PdfTargetDictionary target = new PdfTargetDictionary(true);
            target.EmbeddedFileName = movie.Title;
            PdfAction action = PdfAction.GotoEmbedded(null, target, dest, true);
            Chunk chunk = new Chunk(" (see info)");
            chunk.SetAction(action);
            item.Add(chunk);
            list.Add(item);
          }
        }
        document.Add(list);
      }
    }
Exemplo n.º 5
0
        public void CreateTaggedPdf13() {
            InitializeDocument("13");

            Paragraph p = new Paragraph();
            Chunk chunk = new Chunk("Please visit ");
            p.Add(chunk);

            PdfAction action = new PdfAction("http://itextpdf.com");
            chunk = new Chunk("http://itextpdf.com",
                              new Font(Font.FontFamily.HELVETICA, Font.UNDEFINED, Font.UNDERLINE, BaseColor.BLUE));
            chunk.SetAction(action);
            p.Add(chunk);
            p.Add(new Chunk(" for more details."));
            document.Add(p);
            document.Close();
//            int[] nums = new int[] {5};
//            CheckNums(nums);
            CompareResults("13");
        }
Exemplo n.º 6
0
// --------------------------------------------------------------------------- 
    public void Write(Stream stream) {
      // step 1
      using (Document document = new Document()) {
        // step 2
        PdfWriter writer = PdfWriter.GetInstance(document, stream);
        // step 3
        document.Open();
        
        
        // step 4
        PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN);
        PdfCollectionSchema schema = _collectionSchema();
        collection.Schema = schema;
        PdfCollectionSort sort = new PdfCollectionSort(KEYS);
        collection.Sort = sort;
        writer.Collection = collection;

        PdfCollectionItem collectionitem = new PdfCollectionItem(schema);
        PdfFileSpecification fs = PdfFileSpecification.FileEmbedded(
          writer, IMG_KUBRICK, "kubrick.jpg", null
        );
        fs.AddDescription("Stanley Kubrick", false);
        collectionitem.AddItem(TYPE_FIELD, "JPEG");
        fs.AddCollectionItem(collectionitem);
        writer.AddFileAttachment(fs);
        
        Image img = Image.GetInstance(IMG_BOX);
        document.Add(img);
        List list = new List(List.UNORDERED, 20);
        PdfDestination dest = new PdfDestination(PdfDestination.FIT);
        dest.AddFirst(new PdfNumber(1));
        PdfTargetDictionary intermediate;
        PdfTargetDictionary target;
        Chunk chunk;
        ListItem item;
        PdfAction action = null;

        IEnumerable<Movie> box = PojoFactory.GetMovies(1)
          .Concat(PojoFactory.GetMovies(4))
        ;
        StringBuilder sb = new StringBuilder();
        foreach (Movie movie in box) {
          if (movie.Year > 1960) {
            sb.AppendLine(String.Format(
              "{0};{1};{2}", movie.MovieTitle, movie.Year, movie.Duration
            ));
            item = new ListItem(movie.MovieTitle);
            if (!"0278736".Equals(movie.Imdb)) {
              target = new PdfTargetDictionary(true);
              target.EmbeddedFileName = movie.Title;
              intermediate = new PdfTargetDictionary(true);
              intermediate.FileAttachmentPage = 1;
              intermediate.FileAttachmentIndex = 1;
              intermediate.AdditionalPath = target;
              action = PdfAction.GotoEmbedded(null, intermediate, dest, true);
              chunk = new Chunk(" (see info)");
              chunk.SetAction(action);
              item.Add(chunk);
            }
            list.Add(item);
          }
        }
        document.Add(list);
        
        fs = PdfFileSpecification.FileEmbedded(
          writer, null, "kubrick.txt", 
          Encoding.UTF8.GetBytes(sb.ToString())
        );
        fs.AddDescription("Kubrick box: the movies", false);
        collectionitem.AddItem(TYPE_FIELD, "TXT");
        fs.AddCollectionItem(collectionitem);
        writer.AddFileAttachment(fs);

        PdfPTable table = new PdfPTable(1);
        table.SpacingAfter = 10;
        PdfPCell cell = new PdfPCell(new Phrase("All movies by Kubrick"));
        cell.Border = PdfPCell.NO_BORDER;
        fs = PdfFileSpecification.FileEmbedded(
          writer, null, KubrickMovies.FILENAME, 
          Utility.PdfBytes(new KubrickMovies())
          //new KubrickMovies().createPdf()
        );
        collectionitem.AddItem(TYPE_FIELD, "PDF");
        fs.AddCollectionItem(collectionitem);
        target = new PdfTargetDictionary(true);
        target.FileAttachmentPagename = "movies";
        target.FileAttachmentName = "The movies of Stanley Kubrick";
        cell.CellEvent = new PdfActionEvent(
          writer, PdfAction.GotoEmbedded(null, target, dest, true)
        );
        cell.CellEvent = new FileAttachmentEvent(
          writer, fs, "The movies of Stanley Kubrick"
        );
        cell.CellEvent = new LocalDestinationEvent(writer, "movies");
        table.AddCell(cell);
        writer.AddFileAttachment(fs);

        cell = new PdfPCell(new Phrase("Kubrick DVDs"));
        cell.Border = PdfPCell.NO_BORDER;
        fs = PdfFileSpecification.FileEmbedded(
          writer, null, 
          KubrickDvds.RESULT, new KubrickDvds().CreatePdf()
        );
        collectionitem.AddItem(TYPE_FIELD, "PDF");
        fs.AddCollectionItem(collectionitem);
        cell.CellEvent = new FileAttachmentEvent(writer, fs, "Kubrick DVDs");
        table.AddCell(cell);
        writer.AddFileAttachment(fs);
        
        cell = new PdfPCell(new Phrase("Kubrick documentary"));
        cell.Border = PdfPCell.NO_BORDER;
        fs = PdfFileSpecification.FileEmbedded(
          writer, null, 
          KubrickDocumentary.RESULT, new KubrickDocumentary().CreatePdf()
        );
        collectionitem.AddItem(TYPE_FIELD, "PDF");
        fs.AddCollectionItem(collectionitem);
        cell.CellEvent = new FileAttachmentEvent(
          writer, fs, "Kubrick Documentary"
        );
        table.AddCell(cell);
        writer.AddFileAttachment(fs);

        document.NewPage();
        document.Add(table);
      }
    }
Exemplo n.º 7
0
 // ---------------------------------------------------------------------------
 /**
  * Creates a Phrase with the name and given name of a director
  * using different fonts.
  * @param r the DbDataReader containing director records.
  */
 public Paragraph CreateDirectorParagraph(PdfWriter writer, DbDataReader r)
 {
     string n = r["name"].ToString();
     Chunk name = new Chunk(n);
     name.SetAction(PdfAction.JavaScript(
       string.Format("findDirector('{0}');", n),
       writer
     ));
     name.Append(", ");
     name.Append(r["given_name"].ToString());
     return new Paragraph(name);
 }
Exemplo n.º 8
0
        // ---------------------------------------------------------------------------    
        /**
         * Creates a PDF document.
         */
        public byte[] CreatePdf()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // step 1
                using (Document document = new Document())
                {
                    // step 2
                    PdfWriter.GetInstance(document, ms);
                    // step 3
                    document.Open();
                    // step 4
                    // Add text with a local destination
                    Paragraph p = new Paragraph();
                    Chunk top = new Chunk("Country List", FilmFonts.BOLD);
                    top.SetLocalDestination("top");
                    p.Add(top);
                    document.Add(p);
                    // Add text with a link to an external URL
                    Chunk imdb = new Chunk("Internet Movie Database", FilmFonts.ITALIC);
                    imdb.SetAction(new PdfAction(new Uri("http://www.imdb.com/")));
                    p = new Paragraph(
                      @"Click on a country, and you'll get a list of movies, 
            containing links to the "
                    );
                    p.Add(imdb);
                    p.Add(".");
                    document.Add(p);
                    // Add text with a remote goto
                    p = new Paragraph("This list can be found in a ");
                    Chunk page1 = new Chunk("separate document");
                    page1.SetAction(new PdfAction(RESULT1, 1));

                    p.Add(page1);
                    p.Add(".");
                    document.Add(p);
                    document.Add(Chunk.NEWLINE);
                    // Get a list with countries from the database      
                    var SQL =
          @"SELECT DISTINCT mc.country_id, c.country, count(*) AS c 
FROM film_country c, film_movie_country mc 
WHERE c.id = mc.country_id
GROUP BY mc.country_id, country 
ORDER BY c DESC";
                    using (var c = AdoDB.Provider.CreateConnection())
                    {
                        c.ConnectionString = AdoDB.CS;
                        using (DbCommand cmd = c.CreateCommand())
                        {
                            cmd.CommandText = SQL;
                            c.Open();
                            using (var r = cmd.ExecuteReader())
                            {
                                while (r.Read())
                                {
                                    Paragraph country = new Paragraph(r["country"].ToString());
                                    country.Add(": ");
                                    Chunk link = new Chunk(string.Format(
                                      "{0} movies", r["c"].ToString()
                                    ));
                                    link.SetAction(PdfAction.GotoRemotePage(
                                      RESULT1,
                                      r["country_id"].ToString(),
                                      false,
                                      true
                                    ));
                                    country.Add(link);
                                    document.Add(country);
                                }
                            }
                        }
                    }
                    document.Add(Chunk.NEWLINE);
                    // Add text with a local goto
                    p = new Paragraph("Go to ");
                    top = new Chunk("top");
                    top.SetAction(PdfAction.GotoLocalPage("top", false));
                    p.Add(top);
                    p.Add(".");
                    document.Add(p);
                }
                return ms.ToArray();
            }
        }
Exemplo n.º 9
-6
// ---------------------------------------------------------------------------    
    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     */
    public byte[] ManipulatePdf(byte[] src) {
    // Create a table with named actions
      Font symbol = new Font(Font.FontFamily.SYMBOL, 20);
      PdfPTable table = new PdfPTable(4);
      table.DefaultCell.Border = Rectangle.NO_BORDER;
      table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
      Chunk first = new Chunk( ((char)220).ToString() , symbol);
      first.SetAction(new PdfAction(PdfAction.FIRSTPAGE));
      table.AddCell(new Phrase(first));
      Chunk previous = new Chunk( ((char)172).ToString(), symbol);
      previous.SetAction(new PdfAction(PdfAction.PREVPAGE));
      table.AddCell(new Phrase(previous));
      Chunk next = new Chunk( ((char)174).ToString(), symbol);
      next.SetAction(new PdfAction(PdfAction.NEXTPAGE));
      table.AddCell(new Phrase(next));
      Chunk last = new Chunk( ((char)222).ToString(), symbol);
      last.SetAction(new PdfAction(PdfAction.LASTPAGE));
      table.AddCell(new Phrase(last));
      table.TotalWidth = 120;
      
      // Create a reader
      PdfReader reader = new PdfReader(src);
      using (MemoryStream ms = new MemoryStream()) {
        // Create a stamper
        using (PdfStamper stamper = new PdfStamper(reader, ms)) {
          // Add the table to each page
          PdfContentByte canvas;
          for (int i = 0; i < reader.NumberOfPages; ) {
            canvas = stamper.GetOverContent(++i);
            table.WriteSelectedRows(0, -1, 696, 36, canvas);
          }
        }
        return ms.ToArray();
      }    
    }