Ejemplo n.º 1
0
// ---------------------------------------------------------------------------
    public void Write(Stream stream) {
      using (ZipFile zip = new ZipFile()) {
        MovieAds movieAds = new MovieAds();
        byte[] pdf = movieAds.CreateTemplate();
        zip.AddEntry(TEMPLATE, pdf);       
        
        using (MemoryStream msDoc = new MemoryStream()) {
          using (Document document = new Document()) {
            using (PdfSmartCopy copy = new PdfSmartCopy(document, msDoc)) {
              document.Open();
              PdfReader reader;
              PdfStamper stamper = null;
              AcroFields form = null;
              int count = 0;
              MemoryStream ms = null;
              using (ms) {
                foreach (Movie movie in PojoFactory.GetMovies()) {
                  if (count == 0) {
                    ms = new MemoryStream();
                    reader = new PdfReader(RESOURCE);
                    stamper = new PdfStamper(reader, ms);
                    stamper.FormFlattening = true;
                    form = stamper.AcroFields;
                  }
                  count++;
                  PdfReader ad = new PdfReader(
                    movieAds.FillTemplate(pdf, movie)
                  );
                  PdfImportedPage page = stamper.GetImportedPage(ad, 1);
                  PushbuttonField bt = form.GetNewPushbuttonFromField(
                    "movie_" + count
                  );
                  bt.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
                  bt.ProportionalIcon = true;
                  bt.Template = page;
                  form.ReplacePushbuttonField("movie_" + count, bt.Field);
                  if (count == 16) {
                    stamper.Close();
                    reader = new PdfReader(ms.ToArray());
                    copy.AddPage(copy.GetImportedPage(reader, 1));
                    count = 0;
                  }
                }
                if (count > 0) {
                  stamper.Close();
                  reader = new PdfReader(ms.ToArray());
                  copy.AddPage(copy.GetImportedPage(reader, 1));
                }
              }
            }
          }
          zip.AddEntry(RESULT, msDoc.ToArray());
        }

        zip.AddFile(RESOURCE, "");
        zip.Save(stream);             
      }
    }
Ejemplo n.º 2
0
// ---------------------------------------------------------------------------
        public void Write(Stream stream)
        {
            using (ZipFile zip = new ZipFile()) {
                MovieAds movieAds = new MovieAds();
                byte[]   pdf      = movieAds.CreateTemplate();
                zip.AddEntry(TEMPLATE, pdf);

                using (MemoryStream msDoc = new MemoryStream()) {
                    using (Document document = new Document()) {
                        using (PdfSmartCopy copy = new PdfSmartCopy(document, msDoc)) {
                            document.Open();
                            PdfReader    reader;
                            PdfStamper   stamper = null;
                            AcroFields   form    = null;
                            int          count   = 0;
                            MemoryStream ms      = null;
                            using (ms) {
                                foreach (Movie movie in PojoFactory.GetMovies())
                                {
                                    if (count == 0)
                                    {
                                        ms      = new MemoryStream();
                                        reader  = new PdfReader(RESOURCE);
                                        stamper = new PdfStamper(reader, ms);
                                        stamper.FormFlattening = true;
                                        form = stamper.AcroFields;
                                    }
                                    count++;
                                    PdfReader ad = new PdfReader(
                                        movieAds.FillTemplate(pdf, movie)
                                        );
                                    PdfImportedPage page = stamper.GetImportedPage(ad, 1);
                                    PushbuttonField bt   = form.GetNewPushbuttonFromField(
                                        "movie_" + count
                                        );
                                    bt.Layout           = PushbuttonField.LAYOUT_ICON_ONLY;
                                    bt.ProportionalIcon = true;
                                    bt.Template         = page;
                                    form.ReplacePushbuttonField("movie_" + count, bt.Field);
                                    if (count == 16)
                                    {
                                        stamper.Close();
                                        reader = new PdfReader(ms.ToArray());
                                        copy.AddPage(copy.GetImportedPage(reader, 1));
                                        count = 0;
                                    }
                                }
                                if (count > 0)
                                {
                                    stamper.Close();
                                    reader = new PdfReader(ms.ToArray());
                                    copy.AddPage(copy.GetImportedPage(reader, 1));
                                }
                            }
                        }
                    }
                    zip.AddEntry(RESULT, msDoc.ToArray());
                }

                zip.AddFile(RESOURCE, "");
                zip.Save(stream);
            }
        }