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

Hi, I want to create a new combined pdf from different pdfs on grid from specific page to end page in the grid. Using the below code I am able to create the combine pdf without any issues. But, this code should also write the original pdf name from the grid at the top left followed by the page number of that page in the original pdf using the following format.

For eg if the current combined page is from the original pdf examplepdf page #10, then it should be written in the following format on the combined pdf in the top left.

(examplepdf-page 10)

But, it is not working. Kindly guide me where I am going wrong.


C#
private void createReviewFiles()
{
    try
    {
        objarraylist = new ArrayList();
        if (dg_codedetails.Rows.Count > 0)
        {
            int pageOffset = 0;
            ArrayList master = new ArrayList();
            int f = 0;
            Document document = null;
            PdfCopy writer = null;

            String pfilename = String.Empty;
            String pdfilename = String.Empty;
            String outputPdfPath = String.Empty;


            DirectoryInfo dirReview = new DirectoryInfo(dir.FullName + "\\" + "ReviewFolder");
            if (!dirReview.Exists)
                dirReview.Create();
            outputPdfPath = dirReview + "\\" + "ReviewFiles.pdf";
            foreach (DataGridViewRow dgr in dg_codedetails.Rows)
            {
                pfilename = dgr.Cells[0].Value.ToString();//pdf file name
                pdfilename = dgr.Cells[7].Value.ToString();// pdf file name in specific format.


                Int16 startvalue = Convert.ToInt16(dgr.Cells[2].Value);//start page #
                Int16 endvalue = Convert.ToInt16(dgr.Cells[3].Value);//end page #


                PdfReader reader = new PdfReader(dir.FullName + "\\" + pfilename);
                reader.ConsolidateNamedDestinations();
                // we retrieve the total number of pages
                int n = reader.NumberOfPages;



                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(outputPdfPath, FileMode.Create));
                    // step 3: we open the document
                    document.Open();
                    f += 1;
                }

                TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(1, 1, 100, 300), pdfilename);
                ////Change the orientation of the text
                tf.Rotation = 90;
                writer.AddAnnotation(tf.GetTextField());

                // put the alignment and coordinates here
                cb.ShowTextAligned(1, pdfilename, 1, 1, 0);

                // step 4: we add content
                for (int i = startvalue; i <= endvalue; i++)
                {

                    if (writer != null)
                    {
                        string text = "(" + pfilename.Replace(".pdf", "").Replace(".PDF", "") + "-page " + i + ")";
                        if (!objarraylist.Contains(text))
                            objarraylist.Add(text);
                        PdfContentByte cb = writer.DirectContent;
                        cb.BeginText();
                        cb.SetFontAndSize(BaseFont.CreateFont(), 10f);
                        cb.SetTextMatrix(600, 15);
                        cb.ShowText(pdfilename);
                        cb.EndText();
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        OnStartPage(page.PdfWriter, page.PdfDocument, pfilename);
                        writer.AddPage(page);

                    }
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null && writer != null)
                {
                    writer.CopyAcroForm(reader);
                }
            }
            if (document != null)
            {
                document.Close();
            }
          

        }

    }
    catch (Exception ex) { MessageBox.Show(ex.Message); }

}
Posted
Updated 10-Jan-14 22:29pm
v2

1 solution

Quote:
But, it is not working
ok .. what is it doing - what does it produce if anything/where does the program stop ??

"it is not working" is of no use to us whatsoever, because we cant see your system

You need to be able to describe the problem with a lot more accuracy if you expect people to be able to help you

I suggest you start putting logging/displays around things like

Quote:
pfilename = dgr.Cells[0].Value.ToString();//pdf file name
pdfilename = dgr.Cells[7].Value.ToString();// pdf file name in specific format.


so you can see what values these cells have - that may be part of your problem
 
Share this answer
 
v3

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