コード例 #1
0
// ---------------------------------------------------------------------------
    /**
     * Creates a PDF file with director names.
     * @param pdf the PDF file to be used as a reader
     */
    public byte[] CreatePdf(byte[] pdf) {
      byte[] tmpDoc = null;
      using ( MemoryStream ms = new MemoryStream() ) {
        using (Document tmp = new Document()) {
          PdfWriter writer = PdfWriter.GetInstance(tmp, ms);
          // step 3
          tmp.Open();
          // step 4
          var SQL = 
  @"SELECT name, given_name 
  FROM film_director 
  ORDER BY name, given_name";
        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() ) {
                  tmp.Add(CreateDirectorParagraph(writer, r));
                }
              }
            }
          }
        }
        tmpDoc = ms.ToArray();
      }

      jsContents = File.ReadAllText(
        Path.Combine(Utility.ResourceJavaScript, RESOURCE)
      );
      List<byte[]> readers = new List<byte[]>() {tmpDoc, pdf};
      using (MemoryStream ms = new MemoryStream()) {
        // step 1
        using (Document document = new Document()) {
          // step 2
          using (PdfCopy copy = new PdfCopy(document, ms)) {
            // step 3
            document.Open();
            // step 4
            copy.AddJavaScript(jsContents);
            for (int i = 0; i < readers.Count; ++i) {
              PdfReader reader = new PdfReader(readers[i]);
              int n = reader.NumberOfPages;
              for (int page = 0; page < n; ) {
                copy.AddPage(copy.GetImportedPage(reader, ++page));
              }
            }
          }
        } 
        return ms.ToArray();     
      }
    }