Home > Enterprise >  Auto-Breakline PdfTable Cells PdfFileWriter c#
Auto-Breakline PdfTable Cells PdfFileWriter c#

Time:11-09

Im writing a Programm that retrieves Cutomer Data from a SQLite-File and stores them in a PDF-File in a PdfTable like:

PdfContents contentsTable = new PdfContents(page);

PdfTable table = new PdfTable(page, contentsTable, ArialNormal, fontSize);
table.TableArea = new PdfRectangle(border (2 * border   fontHight), (210 - border), (297 - (2 * border   4 * linespacing   5 * fontHight))); // <- 'border', 'fontHight' and 'linespacing' are simply variables i use to define lengths that also apear at other locations
table.SetColumnWidth(30, 75, 35, 10, 30, 22, 8);
table.HeaderOnEachPage = true;
table.MinRowHight = 5;
table.Header[0].Style = table.HeaderStyle;
table.Header[1].Style = table.HeaderStyle;
table.Header[2].Style = table.HeaderStyle;
table.Header[3].Style = table.HeaderStyle;
table.Header[4].Style = table.HeaderStyle;
table.Header[5].Style = table.HeaderStyle;
table.Header[6].Style = table.HeaderStyle;
table.Header[0].Value = "CODE";
table.Header[1].Value = "BESCHREIBUNG";
table.Header[2].Value = "MATERIALNAME";
table.Header[3].Value = "NoFl";
table.Header[4].Value = "ERGEBNIST";
table.Header[5].Value = "BEWERTUNG";
table.Header[6].Value = "ART";

// Querys the 'ANALYT' and 'RESULTS' Table and Adds the Answers into the Table in the PDF-File
using (var connection = new SQLiteConnection("Data Source = "   dbFile)) // <- 'dbFile' contains the Path to the File
{
// Opens the connection to the SQLite File
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "select A.CODE, A.BESCHERIBUNG, A.MATERIALNAME, R.NORMFLAG, R.ERGEBNIST, R.BEWERTUNG, R.ART "  
"from ANALYT A, RESULTS R "  
"and A.ANALYTX = R.ANALYTX "  
"order by A.MATERIALNAME desc, R.SORT ";

using (var reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
table.Cell[0].Value = reader[0].ToString();
table.Cell[1].Value = reader[1].ToString();
table.Cell[2].Value = reader[2].ToString();
table.Cell[3].Value = reader[3].ToString();
table.Cell[4].Value = reader[4].ToString();
table.Cell[5].Value = reader[5].ToString();
table.Cell[6].Value = reader[6].ToString();
}
contentToBeAdded = true; // <- A variable i use to determine if the processes are all successfull
}
else
{
// Code that lets the user know something went wrong
}
connection.Close();
}
table.Close();
document.CreateFile();

This code works perfectly fine in almost all circumstances, but as the entrences in the Database have been collected by hundreds of people over decades, so the data values can have a wide range in length. While the Width-VAlues i entered are adequate for most situations, there can be specific entrenceses that are far longer. As the data i work with is rather critical, I cant just ignore these few cases. My original thought was that the PdfTable would automatically Benter a new line and make the Cell higher. But when i ran into this exact problem in my testing, I noticed that the Text just kept going and was covering the next cell. Is there an easy fix that i can tell PdfTable to do this automatically or do i have to count the number of letters in the string and devide it maually for every value that is to long? Since I am still rather a beginner in programming, I greatly appreciate any help and input.

CodePudding user response:

I've found the solution to my question and its actually quite simple. I hope that in the future someone might have the same problem and find this helpful.

Instead of adding a simple string, use PdfFileWriter.TextBox like:

PdfFileWriter.TextBox textBox0 = new PdfFileWriter.TextBox(16, 0, 0.5); // <- 16 = TextboxWidth, 0 = FirstLineIndent, 0.5 = LineBreakFactor

textBox0.AddText(ArialNormal, fontSize, reader0); // <- ArialNormal = PdfFont Variable containing a Font, fontSize = int Variable containing the Font Size, reader0 = string Variable containing the Content

table.Cell[0].Value = textBox0;

This will add the Text, AutoLineBreak and increase the Height of the Cell.

Hope anyone finds this helpful.

  • Related