Thanks Bruno, this looks like it should work,
 
However, I repeatedly get the error "Specified cast is not valid" on document.Close() when using the example (adapted for .NET). I am using the PDF files from the example, and creating a new output file each time.
 
I have tried flushing the writer, and creating the FileStream outside of the PdfCopy method in order to flush and close it properly also; but the same error occurs, and the file, although created, is corrupted and cannot be repaired or opened.
 
I don't understand what is being cast. Am I using the correct Stream type?
 
(code attached)
thanks,
Mark

Bruno Lowagie <[EMAIL PROTECTED]> wrote:
michelle jeffreys wrote:
> thanks Bruno,
>
> In fact, on reading your answer it appears my question is of a more
> basic nature. We're using CrystalReports in C# to create the PDF's and
> archive them. All I actually need to do is figure out how to
> concatenate those PDFs using iText and C#...
>
> I'm not sure if it's possible to call the class
> *com.lowagie.tools.concat_pdf* from within *C#* instead of from the
> command line?

If you're using C#, then you should download a .NET port of iText,
for instance: http://itextsharp.sourceforge.net/
From there it's easy to adapt the examples in the iText tutorial:
http://itextdocs.lowagie.com/tutorial/general/copystamp/
br,
Bruno


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as ea sy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions


Yahoo! Messenger NEW - crystal clear PC to PC calling worldwide with voicemail
using System;
using System.IO;
using System.Collections;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace MitisNetClient
{
        /// <summary>
        /// Summary description for ConcatenatePDFs.
        /// </summary>
        public class ConcatenatePDFs
        {
                public ConcatenatePDFs()
                {
                }

                public static void concat(string[] args)
                {
                        #region Calling Code
//                      string firstDoc = @"C:\temp\ChapterSection.pdf";
//                      string secondDoc = @"C:\temp\Destinations.pdf";
//                      string thirdDoc = @"C:\temp\SimpleAnnotations1.pdf";
//                      string destinationFile = @"C:\temp\output.pdf";
//                      string[] args = 
{firstDoc,secondDoc,thirdDoc,destinationFile};
//                      ConcatenatePDFs.concat(args);
                        #endregion


                        try 
                        {
                                int pageOffset = 0;
                                ArrayList master = new ArrayList();
                                int f = 0;
                                String outFile = args[args.Length-1]; //concat 
all files into last one.
                                Document document = null;
                                PdfCopy  writer = null;

                                while (f < args.Length-1) 
                                {
                                        // we create a reader for a certain 
document
                                        PdfReader reader = new 
PdfReader(args[f]);      // Error: PDF Header Signature not found
                                        reader.ConsolidateNamedDestinations();
                                        // we retrieve the total number of pages
                                        int n = reader.NumberOfPages;
                                        ArrayList bookmarks = 
SimpleBookmark.GetBookmark(reader);
                                        if (bookmarks != null) 
                                        {
                                                if (pageOffset != 0) 
SimpleBookmark.ShiftPageNumbers(bookmarks, pageOffset, null);
                                                master.Add(bookmarks);
                                        }
                                        pageOffset += n;
                    
                                        if (f == 0) 
                                        {
                                                // step 1: creation of a 
document-object
                                                document = new 
Document(reader.GetPageSizeWithRotation(1));
                                                // step 2: we create a writer 
that listens to the document
                                                writer = new PdfCopy(document, 
new FileStream(outFile, FileMode.Create, FileAccess.Write));
                                                // step 3: we open the document
                                                document.Open();
                                        }
                                        // step 4: we add content
                                        PdfImportedPage page;
                                        for (int i = 0; i < n; ) 
                                        {
                                                ++i;
                                                page = 
writer.GetImportedPage(reader, i);
                                                writer.AddPage(page);
                                        }
                                        PRAcroForm form = reader.AcroForm;
                                        if (form != null) 
writer.CopyAcroForm(reader);
                                        f++;
                                }
                                if (master.Count > 0)   
writer.SetOutlines(master);
                                // step 5: we close the document
//                              writer.Flush();
                                document.Close();       //Error here: Specified 
cast is not valid
//                              writer.Close();

                                //The document saves but is damaged and 
unreadable each time.
//                              fs.Flush();
//                              fs.Close();
                                
                        }
                        catch(Exception e) 
                        {
                                string error = e.Message;
                        }

                        //***************************Stream to 
browser***************************
//                      MemoryStream ms = new MemoryStream();
//                      pdfDocument.Save(ms);
//                      ms.Close();
//
//                      // Write the PDF in the response
//                      Response.Clear();
//
//                      //C1PDF encodes the bytes in UTF8
//                      Response.ContentEncoding = System.Text.Encoding.UTF8;
//                      Response.ContentType = "Application/pdf";
//
//                      // force the client to open a file open dialog
//                      Response.AddHeader("content-disposition", "attachment; 
filename=" + "planning_" + UserSite + ".pdf");
//
//                      byte[] buffer = ms.ToArray();
//                      Response.BinaryWrite(buffer);
//                      Response.End();
                        
//************************************************************************
        
                        //return masterFileName;
                }//end concat

        }
}

#region JAVA EXAMPLE

///*
// * $Id: Concatenate.java,v 1.2 2005/04/13 13:52:06 blowagie Exp $
// * $Name:  $
// *
// * This code is free software. It may only be copied or modified
// * if you include the following copyright notice:
// *
// * This class by Mark Thompson. Copyright (c) 2002 Mark Thompson.
// *
// * This code is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// *
// * [EMAIL PROTECTED]
// */
//
///**
// * This class demonstrates copying a PDF file using iText.
// * @author Mark Thompson
// */
//package com.lowagie.examples.general.copystamp;
//
//import java.io.FileOutputStream;
//import java.util.ArrayList;
//import java.util.List;
//
//import com.lowagie.text.Document;
//import com.lowagie.text.pdf.PRAcroForm;
//import com.lowagie.text.pdf.PdfCopy;
//import com.lowagie.text.pdf.PdfImportedPage;
//import com.lowagie.text.pdf.PdfReader;
//import com.lowagie.text.pdf.SimpleBookmark;
//
///**
// * Tool that can be used to concatenate existing PDF files.
// */
//public class Concatenate 
//{
//
//      /**
//       * This class can be used to concatenate existing PDF files.
//       * (This was an example known as PdfCopy.java)
//       * @param args the command line arguments
//       */
//      public static void main(String args[]) 
//      {
//              if (args.length < 2) 
//              {
//                      System.err.println("arguments: file1 [file2 ...] 
destfile");
//              }
//              else 
//              {
//                      System.out.println("PdfCopy example");
//                      try 
//                      {
//                              int pageOffset = 0;
//                              ArrayList master = new ArrayList();
//                              int f = 0;
//                              String outFile = args[args.length-1];
//                              Document document = null;
//                              PdfCopy  writer = null;
//                              while (f < args.length-1) 
//                              {
//                                      // we create a reader for a certain 
document
//                                      PdfReader reader = new 
PdfReader(args[f]);
//                                      reader.consolidateNamedDestinations();
//                                      // we retrieve the total number of pages
//                                      int n = reader.getNumberOfPages();
//                                      List bookmarks = 
SimpleBookmark.getBookmark(reader);
//                                      if (bookmarks != null) 
//                                      {
//                                              if (pageOffset != 0)
//                                                      
SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
//                                              master.addAll(bookmarks);
//                                      }
//                                      pageOffset += n;
//                    
//                                      if (f == 0) 
//                                      {
//                                              // step 1: creation of a 
document-object
//                                              document = new 
Document(reader.getPageSizeWithRotation(1));
//                                              // step 2: we create a writer 
that listens to the document
//                                              writer = new PdfCopy(document, 
new FileOutputStream(outFile));
//                                              // step 3: we open the document
//                                              document.open();
//                                      }
//                                      // step 4: we add content
//                                      PdfImportedPage page;
//                                      for (int i = 0; i < n; ) 
//                                      {
//                                              ++i;
//                                              page = 
writer.getImportedPage(reader, i);
//                                              writer.addPage(page);
//                                      }
//                                      PRAcroForm form = reader.getAcroForm();
//                                      if (form != null)
//                                              writer.copyAcroForm(reader);
//                                      f++;
//                              }
//                              if (master.size() > 0)
//                                      writer.setOutlines(master);
//                              // step 5: we close the document
//                              document.close();
//                      }
//                      catch(Exception e) 
//                      {
//                              e.printStackTrace();
//                      }
//              }
//      }
//}


#endregion

Reply via email to