Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to view the file explorer in my windows forms listview and select file from the list view using checkbox .. it is working ... now i want to combine all the selected pdf in to one pdf .. it is working with direct path but i want to combine these using the combo box items. selected path s coming in combo box but combining isnt working

help me plzz

What I have tried:

C#
{
    string[] lstFiles = new string[comboBox1.Items.Count];
    for (int indexInteger = 0; indexInteger < comboBox1.Items.Count; indexInteger++)
    {
        lstFiles[indexInteger] = comboBox1.Items[indexInteger].ToString();
    }
            //comboBox1.Text=comboBox1.Items[0].ToString();
            //string[] lstFiles = new string[3];

            //lstFiles[0] = comboBox1.Items[0].ToString();
            //lstFiles[1] = comboBox1.Items[1].ToString();
            //lstFiles[2] = comboBox1.Items[2].ToString();

            //string a = lstFiles[0].ToString();
            ////lstFiles[0] = @"C:\Users\RASH\Documents\AERCEL\New Files\160216 BMC Rayyan Road Project 7 Protection Board.pdf";
            //lstFiles[1] = @"C:\Users\RASH\Documents\AERCEL\New Files\AERCEL SP 20 - TDS.pdf";
            //lstFiles[2] = @"C:\Users\RASH\Documents\AERCEL\New Files\BMC QATAR WLL PLG letter.pdf";

            iTextSharp.text.pdf.PdfReader reader = null;
            iTextSharp.text.Document sourceDocument = null;
            PdfCopy pdfCopyProvider = null;
            PdfImportedPage importedPage;
            string outputPdfPath = @"D:/1.pdf";
            //axAcroPDF1.src = outputPdfPath;

            sourceDocument = new iTextSharp.text.Document();
            pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

            //Open the output file
            sourceDocument.Open();
//axAcroPDF1.src = outputPdfPath;
            try
            {
                //Loop through the files list
                for (int f = 0; f <= lstFiles.Length-1 ; f++)
                {
                    int pages = get_pageCcount(lstFiles[f]);

                    reader = new PdfReader(lstFiles[f]);
                    //Add pages of current file
                    for (int i = 1; i <= pages; i++)
                    {
                        importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                        pdfCopyProvider.AddPage(importedPage);
                    }

                    reader.Close();
                }
                //At the end save the output file
                sourceDocument.Close();
                axAcroPDF1.src = outputPdfPath;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
private int get_pageCcount(string file)
{
    using (StreamReader sr = new StreamReader(File.OpenRead(file)))
    {
        Regex regex = new Regex(@"/Type\s*/Page[^s]");
        MatchCollection matches = regex.Matches(sr.ReadToEnd());

        return matches.Count;
    }
}



trying with direct path it is working but trying to give path from combox it showing error in catch
Posted
Updated 6-Jun-16 12:09pm
v2
Comments
Richard MacCutchan 5-Jun-16 11:28am    
What error?
Rashad Hameed 5-Jun-16 11:53am    
the document has no page root invalid pdf file
CHill60 5-Jun-16 12:07pm    
On which line is the exception originally thrown?

1 solution

With ItextSharp


C#
public void MergePDFs(string[] PoArchivos, string PsOutFile, string PsClave)
        {
            PoArchivos = OrdenarArchivosSegunClave(PoArchivos, PsClave);
            
            // step 1: creation of a document-object
            Document LoDocument = new Document();

            // step 2: we create a writer that listens to the document
            if(File.Exists(PsOutFile))
            {
                File.Delete(PsOutFile);
            }
            PdfCopy LoWriter = new PdfCopy(LoDocument, new FileStream(PsOutFile, FileMode.Create));
            if (LoWriter == null)
            {
                return;
            }

            // step 3: we open the document
            LoDocument.Open();
            foreach (string fileName in PoArchivos)
            {
                // we create a reader for a certain document
                PdfReader LoReader = new PdfReader(fileName);
                LoReader.ConsolidateNamedDestinations();
                
                // step 4: we add content
                for (int i = 1; i <= LoReader.NumberOfPages; i++)
                {
                    PdfImportedPage page = LoWriter.GetImportedPage(LoReader, i);
                    LoWriter.AddPage(page);
                }
                
                LoReader.Close();
            }

            // step 5: we close the document and writer
            LoWriter.Close();
            LoDocument.Close();
        }
 
Share this answer
 
Comments
Rashad Hameed 7-Jun-16 2:48am    
thank you ...

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