Click here to Skip to main content
14,735,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a bit of a complicated question. I am looking for a way to add a page to a pdf I generate if the int I use for each repeating record exceeds 5. The generator works, and it will dynamically fill in form fields depending on user input, but I am having trouble adding a page if the number of records exceeds 5. I want it to be conditional on the integers value, but I am completely lost. Any examples I find online seem to be using totally different approaches to PDF generation, and I just don't have the C# experience to understand exactly what I need to do. Here is my code up to this point, I'm taking out a lot of the stuff that is repetitive, and I'm not including my exceptions, because they are not part of the problem.

I apologize for how messy it looks, it's just how it copied in. I promise my code does not look like this in the project. I just don't know how I would go about generating a new page while not affecting another part of the project. I know that I will be putting the commands for adding the page within the if statement at the end, I just can't find any examples like mine to help me understand how to go about this.

private void FillForm()
            string pdfTemplate = openFileDialog.FileName;
            string newFile = @"c:\Data\" + Guid.NewGuid().ToString() +        "TableForm.pdf";

            string sql = "SELECT m.site_name, m.meter_no, m.address,, p.state_abbr,, p.addr1, p.legalname FROM meter m "
                         + "JOIN property p ON = "
                         + "WHERE property_id = " + idTextBox.Text + " "
                         + "ORDER BY meter_no ASC LIMIT " + accountsComboBox.Text + ";";

            NpgsqlConnection conn = new NpgsqlConnection(connString0);
            NpgsqlConnection subConn1 = new NpgsqlConnection(connString1);

            int c = 0;

                NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
                NpgsqlDataReader dr = cmd.ExecuteReader();
                PdfReader pdfReader = new PdfReader(pdfTemplate);
                PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
                           newFile, FileMode.Create));
                while (dr.Read())                    
                    //Retrieve Site Name and the first query result attached to the property id
                    if (!dr.IsDBNull(1))
                        string subSql1 = "SELECT m.site_name, m.meter_no, m.address,, p.state_abbr,, p.addr1, p.legalname from " + sql
                                        + "JOIN property p ON = "
                                        + " WHERE m.property_id = " + idTextBox.Text + ";";

                        NpgsqlCommand subCmd1 = new NpgsqlCommand(subSql1, subConn1);
                        String meter_no = "MeterNumber" + c.ToString();
                        String site_name = "SiteName" + c.ToString(); ;
                        String address = "Address" + c.ToString();
                        String citystatezip = "CityStateZip" + c.ToString();
                        String billingaddress = "BillingAddress" + c.ToString();
                        String companyname = dr.GetString(7);

                            // The form's checkboxes
                            //pdfFormFields.SetField("CheckBox1", "Yes or 0");

                            //Map form field names to results, and insert results into fields.

                            pdfFormFields.SetField(site_name, (dr.GetString(0)));
                            pdfFormFields.SetField(meter_no, (dr.GetString(1)));
                            pdfFormFields.SetField(address, (dr.GetString(2)));
                            pdfFormFields.SetField(citystatezip, dr.GetString(3) +    ", " + dr.GetString(4) + " " + dr.GetString(5));
                            pdfFormFields.SetField(billingaddress, dr.GetString(6));
                            pdfFormFields.SetField("CompanyName", (companyname));
                            pdfFormFields.SetField("Date", (DateTime.Now.ToString("d")));
                            // report by reading values from completed PDF  
                    if (c > 5)
                string sTmp = "Form Completed for " + pdfFormFields.GetField("SiteName") + " @ " +
                MessageBox.Show(sTmp, "Finished");
                // flatten the form to remove editting options, set it to false
                // to leave the form open to subsequent manual edits
                pdfStamper.FormFlattening = false;
Updated 20-Jun-12 5:57am

Please make the question simple. However, I am not sure, are you looking for this:

if( c % 5 ==0) // This means every five page

instead of if(c > 5)?
MikeVaros 20-Jun-12 13:46pm
I apologize, it is an extremely complicated question, but I tried to be as specific as I can. I'll explain what my program does right now, and what I want to add to it. Right now, it takes a pdf template with repeating form fields like "MeterNumber1" "MeterNumber2" and iterates through a loop to assign database values to each form field like that.

How I have that working is using the integer "c" to increment by 1 every time the loop iterates. So the original pass will insert the meter_no database value matching it to the field"MeterNumber1" in the PDF it generates from that template. The next pass would do the same thing, but since the integer "c" incremented in the loop, it would write the value to "MeterNumber2". So that can loop through as many times as is needed, but it will only write as many values to the pdf as there are fields present (currently 5).

What I want to do, is to set a condition so that if the incrementing integer "c" exceeds 5, it adds a new page to the document, where I would eventually dynamically create a table to handle the additional results. I have dug around all morning looking for a way to get this to work.

Every example always uses the command, document.NewPage(); however as you can see from my code, I do not reference the Document Class anywhere, and I have no idea how I could even fit it into the project currently. This is mainly because I am a c# novice still, so I don't fully understand the relationships between functions yet. My best guess is that I would have to eliminate one of the strings that declares the Path the pdf is written to, and use the Document Class in it's place.

The template that pdfStamper writes to:
string pdfTemplate = openFileDialog.FileName;

The Pdf that is generated after pdfStamper is finished inserting field values:
string newFile = @"c:\Data\" + Guid.NewGuid().ToString() + "TableForm.pdf";

I'm sorry if that made it seem even more complicated, but it's what I need to do for this project. I'll do my best to simplify my problem right here. How do I add a page to the PDF called out in the newFile String?
Can you please try this:
1) First time initiate the Document.NewPage() at the begining.
2) Then, create five pages with the above initiation. Namely, do this until c++ equal to five (5).
3) Then, use this condition: if(c % 5 ==0){ }
4) Inside if(c % 5 ==0){ here create new Document class initiation. and do the same process above }
if (c > i && c > k && sb.ToString().Contains("AccountNumber") && sb.ToString().Contains("MeterNumber"))
    pdfStamper.FormFlattening = false;
    appendMeterTable(formFile, tableFile, property_id.ToString());
    mergeFiles(completedFile, sourceFiles);
   // ExtractText(completedFile, completedHtml);

I ended up just putting the functions to create tables/handle additional pages in another method, and called it from the if loop here. If anyone wants the whole method, or just has a question relating to this, just reply to this comment, and I can elaborate for 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