-1

I want to display the sequence as AA, AB, AC and so on.

This sequence should be applied to PDF rows line by line using ItextSharp Library and the Values comes from SQL DB.

eg of sequence--> 1st record AA
2nd record AB
3rd record AC
... and so on

I have code to generate sequence of AA, AB, AC .... so on, But the existing code has many tables with some number of rows, So if first table has 5 rows then the sequence is applied for 5 rows as AA, AB, AC, AD,AE and loop ends for table1, when table2 loop starts the sequence not starting from AF but it is taking as new table and again sequence repetition starting as AA, AB, AC upto so on and for next table again the sequence starts from AA,AB,AC...., The Project Existing code using loops to fetch the data apply to PDF from DB, Whenever new table comes from DB new sequence is getting started.

I am unable solve this problem please someone helpme to overcome this.

String index = "";

PdfPTable table = new PdfPTable(columnWidth.Count()) { TotalWidth = TABLE_WIDTH, LockedWidth = true };

index = GetIndex(table.Size);

private static string mColumnLetters = "zabcdefghijklmnopqrstuvwxyz";

    public static string GetIndex(int ColumnIndex)
    {             
        int ModOf26, Subtract;
        StringBuilder NumberInLetters = new StringBuilder();
        ColumnIndex += 26;
        while (ColumnIndex > 0)
        {
            if (ColumnIndex <= 26)
            {
                ModOf26 = ColumnIndex;
                NumberInLetters.Insert(0, mColumnLetters.Substring(ModOf26, 1));
                ColumnIndex = 0;
            }
            else
            {
                ModOf26 = ColumnIndex % 26;
                Subtract = (ModOf26 == 0) ? 26 : ModOf26;
                ColumnIndex = (ColumnIndex - Subtract) / 26;
                NumberInLetters.Insert(0, mColumnLetters.Substring(ModOf26, 1));
            }
        }
        return NumberInLetters.ToString().ToUpper();
    }

==>Please help me to get the program to display AA, AB, AC, etc without any splits. Getindex(Table.Size)==> here the input parameter making problem i think Instead Table.Size which input should i pass if i have to overcome Splits and repeated seq generation.

==>Can anyone please help me by spending sometime on this regard

Zeb
  • 1
  • 2
  • 1
    Hello and welcome to SO! Please read the [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) to improve your question and help us to understand your problem. – Trevor Mar 24 '21 at 12:34
  • Please tag the specific library you are using. I am guessing iTextSharp? – srk Mar 24 '21 at 12:41
  • Hi SRK, Yes, i am using iTextSharp Library, Please help me with the program. And the above program is working but it starts from A, B, C to Z and After AA, AB, AC .... So on, But i need sequence to be started from AA, AB, AC, AD....... so on without any splits. – Zeb Mar 25 '21 at 04:45
  • @srk, Can you please help me with the code. – Zeb Apr 12 '21 at 14:54
  • @Zeb I think this has what you need. https://stackoverflow.com/questions/181596/how-to-convert-a-column-number-e-g-127-into-an-excel-column-e-g-aa –  Apr 17 '21 at 15:18
  • Please add a [Minimal, Reproducable Example](https://stackoverflow.com/help/minimal-reproducible-example) to your question, to clarify what you're trying to achieve. Or, at least, add an example of the input data and the corresponding expected output. – rhens Apr 22 '21 at 14:58

1 Answers1

0

Your GetIndex method returns, for increasing values of value, starting from 1:

1: A
2: B
...
26: Z
27: AA
...
52: AZ
53: BA
etc

So, if you make sure to start with 27 for value, you could keep that method as is.

I'm not sure why you have an issue with table splitting. You should just add content to a single table, regardless of splitting. iText will take care of the splitting.

Document doc = new Document();
Stream outfile = new FileStream("SO66781099.pdf", FileMode.Create);
PdfWriter w = PdfWriter.GetInstance(doc, outfile);
doc.Open();
PdfPTable table = new PdfPTable(1);
for (int i = 1; i < 100; i++)
{
    // first call to GetIndex is for 27
    PdfPCell cell = new PdfPCell(new Phrase(GetIndex(i + 26)));
    cell.FixedHeight = 50;
    table.AddCell(cell);
}
doc.Add(table);
doc.Close();

End of page 1, start of page 2:

Resulting PDF output

rhens
  • 4,791
  • 3
  • 22
  • 38
  • Hi rhens, Here the problem is Getinex menthod reuiqred input parameter that i am passing as (table.size). SO the values coming from DB with many tables with some conditions it took splits and again sequence is getting started. – Zeb Apr 21 '21 at 14:11
  • You'll have to explain more clearly what you're trying to achieve. Maybe edit the question to include an example of the intended output. – rhens Apr 21 '21 at 14:17
  • Okay, There is some pdf generation code with itextsharp the values comes from DB that contains 7 categories, for with one category some 52 records are there for example for Train No.1 52 records, So sequence start from AA to upto 52 records and after that Train No.2 having 100 and here its not starting from 53 value, it considering as new table and again it starts from AA to upto the end number – Zeb Apr 21 '21 at 14:28
  • Hi rhens,Please check my question there i have edited some text with new code which is working with seq but splits unable to overcome with my newly added code also. – Zeb Apr 21 '21 at 14:51
  • So for the example in your comment above: For Train 1, you want to have a table with 52 rows, which are labeled from `AA` to `BZ`. Right? Then, for Train 2, you want a new table with 100 rows, labeled `CA` to `FV`. If that's the expectation, why don't you simply keep a running counter for the rows and use that as a parameter for the `GetIndex` method? At the end of table 1, that counter would be 52. Then it goes on to 53, at the start of table 2. – rhens Apr 22 '21 at 14:56
  • Hi rhens, Sorry to say this i am not familier with Dotnet coding i am just moved development to complete some small small enhamcements, Can you please help me with the code which you have suggested please, The Code i am using for the generation was i mentioned above. The logic that you have said i need to apply for the above code itself, Please help me with the code – Zeb Apr 23 '21 at 11:00