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.city, p.state_abbr, p.zip, p.addr1, p.legalname FROM meter m "
+ "JOIN property p ON p.id = m.id "
+ "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;
try
{
conn.Open();
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())
{
if (!dr.IsDBNull(1))
{
string subSql1 = "SELECT m.site_name, m.meter_no, m.address, p.city, p.state_abbr, p.zip, p.addr1, p.legalname from " + sql
+ "JOIN property p ON p.id = m.id "
+ " WHERE m.property_id = " + idTextBox.Text + ";";
NpgsqlCommand subCmd1 = new NpgsqlCommand(subSql1, subConn1);
c++;
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);
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")));
}
if (c > 5)
{
}
}
string sTmp = "Form Completed for " + pdfFormFields.GetField("SiteName") + " @ " +
pdfFormFields.GetField("SiteName");
MessageBox.Show(sTmp, "Finished");
pdfStamper.FormFlattening = false;
pdfStamper.Close();
}