我正在撰寫一個程式,從 SQLite 檔案中檢索客戶資料并將它們存盤在 PdfTable 中的 PDF 檔案中,例如:
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();
這段代碼幾乎在所有情況下都能正常作業,但由于數十年來數百人收集了資料庫中的入口,因此資料值的長度范圍很廣。雖然我輸入的 Width-VALues 對大多數情況來說都足夠了,但可能會有更長的特定入口。由于我使用的資料相當關鍵,我不能忽略這幾個案例。我最初的想法是 PdfTable 會自動彎曲一個新行并使單元格更高。但是當我在測驗中遇到這個確切的問題時,我注意到 Text 一直在繼續并覆寫下一個單元格。是否有一個簡單的解決方法,我可以告訴 PdfTable 自動執行此操作,或者我是否必須計算字串中的字母數并為每個過長的值手動劃分它?由于我仍然是編程的初學者,我非常感謝任何幫助和輸入。
uj5u.com熱心網友回復:
我找到了我的問題的解決方案,它實際上非常簡單。我希望將來有人可能會遇到同樣的問題并發現這有幫助。
而不是添加一個簡單的字串,使用PdfFileWriter.TextBox像:
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;
這將添加Text,AutoLineBreak并增加Height單元格的 。
希望任何人都覺得這有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/352702.html
