/// <summary> /// Creates dictionary referring to a target document that is the parent of the current document. /// </summary> /// <param name="nested">null if this is the actual target, another target if this is only an intermediate target.</param> public PdfTargetDictionary(PdfTargetDictionary nested) { Put(PdfName.R, PdfName.P); if (nested != null) { AdditionalPath = nested; } }
/** * Creates a GoToE action to an embedded file. * @param filename the root document of the target (null if the target is in the same document) * @param dest the named destination * @param isName if true sets the destination as a name, if false sets it as a String * @return a GoToE action */ public static PdfAction GotoEmbedded(String filename, PdfTargetDictionary target, String dest, bool isName, bool newWindow) { if (isName) { return(GotoEmbedded(filename, target, new PdfName(dest), newWindow)); } else { return(GotoEmbedded(filename, target, new PdfString(dest, null), newWindow)); } }
/** * Creates a GoToE action to an embedded file. * @param filename the root document of the target (null if the target is in the same document) * @param target a path to the target document of this action * @param dest the destination inside the target document, can be of type PdfDestination, PdfName, or PdfString * @param newWindow if true, the destination document should be opened in a new window * @return a GoToE action */ public static PdfAction GotoEmbedded(String filename, PdfTargetDictionary target, PdfObject dest, bool newWindow) { PdfAction action = new PdfAction(); action.Put(PdfName.S, PdfName.GOTOE); action.Put(PdfName.T, target); action.Put(PdfName.D, dest); action.Put(PdfName.NEWWINDOW, new PdfBoolean(newWindow)); if (filename != null) { action.Put(PdfName.F, new PdfString(filename)); } return(action); }
// --------------------------------------------------------------------------- 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); } }
// --------------------------------------------------------------------------- /** * 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()); } }
// --------------------------------------------------------------------------- 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); } }