Click here to Skip to main content
15,120,268 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

in my web application have functionality merging two pdf documents to another new document(combined of two pdf files).

any way iam merging two files here (results.pdf file)one pdf file will be print all results(what are content are there) another file printing but unable to print some results

So,let me know wre i wrong or post me how to merging tow pdf documents(files)

//here is my code is

class Program
   {
       //Static variables used throughout the program
       //The folder that we'll be saving things to
       static string WorkingFolder = Environment.GetFolderPath ( Environment.SpecialFolder.Desktop );
       //The samples images that we'll create and work with
       static string Img1 = Path.Combine ( WorkingFolder, "1st.pdf" );
       static string Img2 = Path.Combine ( WorkingFolder, "2nd.pdf" );
       //These two arrays are just used to create our sample images
       static Color[] colors = new Color[] { Color.RED, Color.BLUE };
       static string[] files = new string[] { Img1, Img2 };
       //Our main export file
       static string MergedFile = Path.Combine ( WorkingFolder, "Merged.pdf" );

       static void Main ( string[] args )
       {
           MergeDocuments ( );
       }
       public static void MergeDocuments ( )
       {
           //Create our PDF readers
           PdfReader r1 = new PdfReader ( Img1 );
           PdfReader r2 = new PdfReader ( Img2 );

           //Our new page size, an A3 in landscape mode
           iTextSharp.text.Rectangle NewPageSize = PageSize.A3.Rotate ( );
           using (FileStream fs = new FileStream ( MergedFile, FileMode.Append, FileAccess.Write, FileShare.None ))
           {                 //Create our document without margins
               //using (Document doc = new Document ( NewPageSize, 0, 0, 0, 0 ))
               //{
               Document doc = new Document ( NewPageSize, 0, 0, 0, 0 );
               if (doc != null)
               {
                   //using (PdfWriter w = PdfWriter.GetInstance ( doc, fs ))
                   PdfWriter w = PdfWriter.GetInstance ( doc, fs );
                   if (w != null)
                   {
                       doc.Open ( );
                       //Get our imported pages
                       PdfImportedPage imp1 = w.GetImportedPage ( r1, 1 );
                       PdfImportedPage imp2 = w.GetImportedPage ( r2, 1 );
                       //Add them to our merged document at specific X/Y coords
                       w.DirectContent.AddTemplate ( imp1, 0, 0 );
                       w.DirectContent.AddTemplate ( imp2, NewPageSize.Width / 2, 0 );
                       doc.Close ( );
                   }
               }
           }
           r1.Close ( );
           r2.Close ( );

       }
   }


Thanks and advance,
Posted
Comments
Rajesh Anuhya 23-Jan-12 5:49am
   
Edited: added pre tags

1 solution

finally i am solving the problem

insted of itextsharp.dll iam using
//
 using PdfSharp.Pdf.IO;
 using PdfSharp.Pdf;
  
 class Program
 {
 //Static variables used throughout the program 
//The folder that we'll be saving things to 
static string WorkingFolder = Environment.GetFolderPath ( Environment.SpecialFolder.Desktop );
 static string filepath = "F:\\";
 //The samples images that we'll create and work with 
static string pdffile1 = Path.Combine ( filepath, "pdf1.pdf" );
 static string pdffile2 = Path.Combine ( filepath, "pdf2.pdf" );
 //These two arrays are just used to create our sample images 
//static Color[] colors = new Color[] { Color.RED, Color.BLUE };
 static string[] files = new string[] { pdffile1, pdffile2 };
 //Our main export file 
static string MergedFileresultspath = Path.Combine ( WorkingFolder, "Mergedresults.pdf" );
  
 
static void Main ( string[] args )
 {
 string[] strings = { pdffile1, pdffile2 };
 MergeMultiplePDFIntoSinglePDF ( MergedFileresultspath, strings );
 //MergeDocuments ( );
 }

 private static void MergeMultiplePDFIntoSinglePDF ( string outputFilePath, string[] pdfFiles )
 {
 Console.WriteLine ( "Merging started....." );
 PdfDocument outputPDFDocument = new PdfDocument ( );
  
 foreach (string pdfFile in pdfFiles)
 {
 PdfDocument inputPDFDocument = PdfReader.Open ( pdfFile, PdfDocumentOpenMode.Import );
 outputPDFDocument.Version = inputPDFDocument.Version;
 foreach (PdfPage page in inputPDFDocument.Pages)
 {
 outputPDFDocument.AddPage ( page );
 }
 }
 outputPDFDocument.Save ( outputFilePath );
 Console.WriteLine ( "Successfully Completed the pdf documents" );
 Console.WriteLine ( "File path is: {0}", MergedFileresultspath );
 Process.Start ( MergedFileresultspath );
 Console.ReadLine ( );
 }
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900