locked
How to merge two crystal reports into one pdf programmatically RRS feed

  • Question

  • User-1558842060 posted

    Hi all,

    I have two crystal reports (cr1, cr2) with data in memory.

    I want to create one single pdf using both of this. I want to save cr1 first and then cr2 on the next page.

    I did not find any way to do this.

    Please help.

    Thursday, September 4, 2008 2:55 AM

All replies

  • User879354489 posted

    put those two reports in to one report as subreport. generate pdf for that main report..!!

    Thursday, September 4, 2008 4:13 AM
  • User-1558842060 posted

    I have resolved the problem using PDFBox-0.7.3.

    Thanks for all the participants.

    Thursday, September 4, 2008 5:53 AM
  • User1824819777 posted

    Hi

    You can use following code to merege two pdfs into single.

    This code would be PDFMerger.cs


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.Collections;
    using System.IO;
    
    namespace WebApplication
    {
        public class PDFMerger
        {
            #region Fields
            private string sourcefolder;
            private string destinationfile;
            private IList fileList = new ArrayList();
            #endregion
    
            public PDFMerger()
            {
            }
            public PDFMerger(IList _fileList)
            {
                this.fileList = _fileList;
            }
            #region Public Methods
            ///
            /// Add a new file, together with a given docname to the fileList and namelist collection
            ///
            public void AddFile(string pathnname)
            {
                fileList.Add(pathnname);
            }
            ///
            /// Generate the merged PDF
            ///
            public void Execute()
            {
                MergeDocs();
            }
            #endregion
    
            #region Private Methods
            ///
            /// Merges the Docs and renders the destinationFile
            ///
            private void MergeDocs()
            {
    
                //Step 1: Create a Docuement-Object
                Document document = new Document();
                try
                {
                    //Step 2: we create a writer that listens to the document
                    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationfile, FileMode.Create));
                    //Step 3: Open the document
                    document.Open();
                    PdfContentByte cb = writer.DirectContent;
                    PdfImportedPage page;
                    int n = 0;
                    int rotation = 0;
                    //Loops for each file that has been listed
                    foreach (string filename in fileList)
                    {
                        //The current file path
                        //string filePath = sourcefolder + filename; 
                        // we create a reader for the document
                        PdfReader reader = new PdfReader(filename);
                        //Gets the number of pages to process
                        n = reader.NumberOfPages;
                        int i = 0;
                        while (i < n)
                        {
                            i++;
                            document.SetPageSize(reader.GetPageSizeWithRotation(1));
                            document.NewPage();
    
                            //Insert to Destination on the first page
                            if (i == 1)
                            {
                                Chunk fileRef = new Chunk(" ");
                                fileRef.SetLocalDestination(filename);
                                document.Add(fileRef);
                            }
    
                            page = writer.GetImportedPage(reader, i);
                            rotation = reader.GetPageRotation(i);
                            if (rotation == 90 || rotation == 270)
                            {
                                cb.AddTemplate(page, 0, -1f, 1f, 0, 0,
    
                              reader.GetPageSizeWithRotation(i).Height);
                            }
                            else
                            {
                                cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                            }
                        }
                    }
                }
                catch (Exception e) { throw e; }
                finally { document.Close(); }
            }
            #endregion
    
            ///
            /// Gets or Sets the DestinationFile
            ///
            public string DestinationFile
            {
                get { return destinationfile; }
                set { destinationfile = value; }
            }
        }
    }

    This would be in MergePDFs.aspx.cs

    PDFMerger merge = new PDFMerger();
    merge.AddFile(Server.MapPath(txtFile1.Text));            
    merge.AddFile(Server.MapPath(txtFile2.Text)); merge.DestinationFile = Server.MapPath(txtFile3.Text); merge.Execute();



    I have used itextsharp.dll you can download it from http://sourceforge.net/projects/itextsharp/


    Friday, November 12, 2010 12:24 AM