Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7970105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:20:46+00:00 2026-06-04T07:20:46+00:00

first at all, here is my code: string pdfTemplate = Application.StartupPath + \\Templates\\Template Medico.pdf;

  • 0

first at all, here is my code:

                    string pdfTemplate = Application.StartupPath + "\\Templates\\Template Medico.pdf";
                    string newFile = Application.StartupPath + "\\Relatórios\\Relatório Médico_" + dataArquivo + ".pdf";

                    PdfReader pdfReader = new PdfReader(pdfTemplate);
                    PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));

                    AcroFields pdfFormFields = pdfStamper.AcroFields;

                    // Form Filling
                    pdfFormFields.SetField("data", data);
                    pdfFormFields.SetField("medico_nome", campo);
                    pdfFormFields.SetField("numero_consultas", numeroConsultas);

                    // Table Building
                    int numColumns = ds.Tables[0].Columns.Count;
                    PdfPTable datatable = new PdfPTable(numColumns);
                    datatable.DefaultCell.Padding = 10;
                    datatable.WidthPercentage = 100; 
                    datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
                    float[] columnWidths = { 80, 80, 80, 80, 80, 80, 80, 80, 80 };
                    datatable.SetWidths(columnWidths);
                    float[] totalWidth = { 80, 80, 80, 80, 80, 80, 80, 80, 80 };
                    datatable.SetTotalWidth(totalWidth);

                    // Table Header
                    for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
                    {
                        Phrase collumname = new Phrase(ds.Tables[0].Columns[k].ColumnName, FontFactory.GetFont("Verdana", 9));
                        datatable.AddCell(collumname);
                    }
                    // Lines and Columns
                    if (ds.Tables[0].Rows.Count <= 9) // less than 9 rows = no problem, no new pages and table spliting needed
                    {
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                            {
                                datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
                                datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                                Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
                                datatable.AddCell(phrase);
                            }
                        }
                        // Write down the table on page 2
                        PdfContentByte content = pdfStamper.GetUnderContent(2);
                        datatable.WriteSelectedRows(0, -1, 70.0f, 495.0f, content);
                    }
                    else
                    {
                        int newPage = 3;
                        int currentPage = 2;
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                            {
                                datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
                                datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                                Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
                                datatable.AddCell(phrase);
                            }
                            if (i > 0 && i % 9 == 0)
                            {   //How do i print the table ONLY every 9 iterations? (9 iterations = 9 rows)
                                PdfContentByte content = pdfStamper.GetUnderContent(currentPage);
                                // Or how do i make a loop using the rowStart and rowEnd arguments of the
                                // WriteSelectedRows function in order to write only a set of 9 rows for each page?
                                datatable.WriteSelectedRows(0, -1, 70.0f, 495.0f, content);
                                pdfStamper.InsertPage(newPage, pdfReader.GetPageSize(2));
                                newPage++;
                                currentPage++;
                            }
                        }
                    }

The code is well commented with the main issues i’m having at the moment, but the real bigger one, as described on the thread title is to somehow “split” or “control” the loop/iteration through the rows and columns to match the page, jump to the other page, and keep writing the table.

Any help will be apreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:20:47+00:00Added an answer on June 4, 2026 at 7:20 am

    Did it guys, took me 2 days and lots of cooffe but i did it, here is the code:

                    else
                    {
                        int newPageNumber = 3;
                        int currentPage = 2;
                        int lastLinePrinted = 0;
                        int maxLine = 9;
                        bool lastPage = false;
                        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                            {
                                datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
                                datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
                                Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
                                datatable.AddCell(phrase);
                            }
                            // must decrement the ds.Tables[0].Rows.Count here, 
                            // otherwise it'll never enter the code below, we'll
                            // increment it back when we reach the last page
                            if ((i > 0 && i % 9 == 0) || i == ds.Tables[0].Rows.Count - 1)
                            {
                                PdfContentByte content = pdfStamper.GetUnderContent(currentPage);
                                datatable.WriteSelectedRows(lastLinePrinted, maxLine, 70.0f, 495.0f, content);
                                if (!lastPage)
                                {
                                    pdfStamper.InsertPage(newPage, pdfReader.GetPageSize(2));
                                    newPage++;
                                    currentPage++;
                                    lastLinePrinted = maxLine;
                                    maxLine += 9;
                                }
                                if (maxLine > ds.Tables[0].Rows.Count)
                                {
                                    maxLine = ds.Tables[0].Rows.Count+1;
                                    lastPage = true;
                                }
                            }
                        }
                    }
    

    Hope it helps a lot more people 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, Beginner here. I'm using this code. class MDArrays { public static
First of all, this is my code (just started learning java): Queue<String> qe =
First of all, here is my code snippet: var str = '<!--:de-->some german text<!--:--><!--:en-->some
First of All here's the Codes: Product Information: public class ProductInformation { public String
first of all this is my third question about web services here and i
First of all, I could not decide if I should ask this here or
First of all, I know there are a few quite similar questions here on
First of all, I do not believe this belongs on Superuser. This belongs here
Here is my code: private const string CONNECTION_STRING = Driver={Microsoft dBase Driver (*.dbf)}; +
I have posted in here before but all have led to dead ends. First

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.