We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose pdf not exporting all columns of datatable to pdf

HI Support

I am writing a DataTable (PENTEST_01c: System Surface Components) to pdf with Aspose Pdf component. The problem is that Aspose is only exporting first 4 columns of DataTable to pdf file where as there are 7 columns in DataTable. I have attached a run time image “imageruntimeaspose.jpg” of my DataTable and also the output Pdf “Test5.pdf” which is generated by Aspose.
Please suggest me how can I export all columns of table “PENTEST_01c: System Surface Components” to pdf. The code I am writing is also shown in image I attached.

Thanks.

Hi Farhan,


Sorry for the inconvenience faced. Normally this is due to evaluation version limitation. Please request a 30 days temporary license for evaluating our product with out any limitations. Hopefully it will serve the purpose. If issue still persist then please share your sample code here. So we will test it at our side and provide you information accordingly.

Please feel free to contact us for any further assistance.

Best Regards,

Hi Farhan

Thanks for your reply. We already have bought a license for Aspose Total for .Net. Still we have a problem. Can you please figure out other causes of this problem.

Best regards
Daniyal

Hi Farhan

Here is the code that we are writing. We are setting license like this

Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense(“Aspose.Total.lic.xml”);

Pdf docPDF = new Pdf();
Section pdfSec1 = docPDF.Sections.Add();
PDFDBList(dbList, docPDF,pdfSec1);


foreach (DictionaryEntry entry in hash)
{


PDFTableRules(hash1, docPDF, pdfSec1);

}

docPDF.Save(reportFileName + “.pdf”);
Console.WriteLine("Created report " + reportFileName + “.pdf”);


private void PDFTableRules(Hashtable hash, Pdf pdf, Section pdfSec2)
{


ArrayList dbList = new ArrayList();
dbList = (ArrayList)hash[“tablerules”];


DataTable dataTable = new DataTable();


for (int i = 0; i < dbList.Count; i++)
{
String tableName = dataTable.TableName;
//ImportText(tableName, sheet);
dataTable = (DataTable)dbList[i];
if (dataTable.Rows.Count > 0)
{

Section pdfSec1 = pdf.Sections.Add();
//Add a text element for the document header

Text pdfHeadText = new Text(tableName);

//Define the text style for the header
pdfHeadText.TextInfo.FontName = “Arial”;
pdfHeadText.TextInfo.IsTrueTypeFontBold = true;
pdfHeadText.TextInfo.FontSize = 10;

pdfHeadText.TextInfo.LineSpacing = 5;
pdfSec1.Paragraphs.Add(pdfHeadText);

//Create a new table for the customer data
Aspose.Pdf.Generator.Table pdfTab = new Aspose.Pdf.Generator.Table();
pdfTab.Margin.Top = 10;
pdfSec1.PageInfo.PageWidth = 5760;
pdfSec1.PageInfo.PageHeight = 707.5F;

pdfSec1.Paragraphs.Add(pdfTab);

//Set with column widths of the table
// pdfTab.ColumnWidths = “800 800”;

//Create a default style for table text
TextInfo pdfTabTextInf = new TextInfo();
pdfTabTextInf.FontName = “Arial”;
// pdfTabTextInf.FontSize = 10;
// pdfTabTextInf.LineSpacing = 3;
pdfTab.DefaultCellTextInfo = pdfTabTextInf;
//Set column widths of the table
pdfTab.ColumnWidths = “40 100 100 100”;

//Set default cell border of the table using BorderInfo object
pdfTab.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All, 0.1F);

//Import data into the Table object from the DataTable created above
pdfTab.ImportDataTable(dataTable, true, 0, 0,dataTable.Rows.Count,dataTable.Columns.Count);

//Get 1st row from the table



Aspose.Pdf.Generator.Row row1 = pdfTab.Rows[0];

//Iterate through all cells in the row and set their background color to blue
foreach (Aspose.Pdf.Generator.Cell curCell in row1.Cells)
curCell.BackgroundColor = new Aspose.Pdf.Generator.Color(“SkyBlue”);

}

// sheet.Cells.ImportDataTable(dataTable, true, sheet.Cells.MaxRow + 2, 1);

}



}




Hi Daniyal,


Thanks for providing the code. Could you please set column widths for all of your columns, as following? Hopefully it will fix your issue. If issue persists then please confirm your Aspose.Pdf Api version.

//Set column widths of the table
pdfTab.ColumnWidths = “40 100 100 100 100 100 100”;


Best Regards,

Hi Tilal

Thanks for solution. Now I am able to export 7 columns to PDF instead of 4 but some of my tables have more than 7 columns and thats why not all of columns of tables are getting exported.

Regards
Dani

Hi Dani,


Thanks for your feed back. Please check following documentation link to change dynamically column width. Hopefully it will fit to your requirements.


Please feel free to contact us for any further assistance.

Best Regards,

Hi Tilal

Thanks for the code snippet. I have tried this code but I think that it doesn’t fit well to my need
as I have a lot of DataTable objects from database during runtime. If I use above snippet or code then I will have to change first Data.DataTable to Aspose Generator.DataTable and then export it to PDF,
I will prefer to use instead the function

pdfTab.ImportDataTable(dataTable, true, 0, 0,dataTable.Rows.Count,dataTable.Columns.Count);

to export data to PDF.

Can you please do
1. A simple connection to some database, retrieve some DataTable having more than 20 columns
2. Export it to PDF with AsposePDF using routine

ImportDataTable(dataTable, true, 0, 0,dataTable.Rows.Count,dataTable.Columns.Count);

and tell me how you can export 20 column table to aspose PDF correctly. Hopefully it will satisfy my needs.

Thanks.

Regards
Dani

Hi Dani,


Please accept my apology for the delayed response. I’m working over your query and will update you soon.

Best Regards,

Hi Dani,


Please check following code snippet hopefully it will meet your requirements.

Pdf pdf = new Pdf();
DataTable dt = new DataTable(“Employee”);
for (int dtc = 0; dtc < 20; dtc++)
dt.Columns.Add(“Head” + dtc, typeof(string));

for (int dtr = 0; dtr < 4; dtr++)
{
DataRow dr = dt.NewRow();
for (int i = 0; i < dt.Columns.Count; i++)
{
dr[i] = “colum” + i;
}
dt.Rows.Add(dr);
}

Pdf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();
Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table();
/* string colWidth = “”;
for (int i = 0; i < dt.Columns.Count; i++)
colWidth = colWidth + "50 ";
tab1.ColumnWidths = colWidth;*/

tab1.ColumnWidths = “100 100”;
tab1.ColumnAdjustment = ColumnAdjustmentType.AutoFitToContent;

// create a row object and add it to table
Aspose.Pdf.Generator.Row row1 = tab1.Rows.Add();
// create loop to add specify the number of columns to be added to table row
for (int Column_Counter = 0; Column_Counter < dt.Columns.Count; Column_Counter++)
{
// create a cell object and add it to table row
Aspose.Pdf.Generator.Cell Cell = row1.Cells.Add();
}
int TableColumn = 0;
// traverse through each table cell in row object
foreach (Aspose.Pdf.Generator.Cell InnerCell in row1.Cells)
{
// specify the width information for each column based on section objects margin and width values
// set the width value as, get the total width of section and subtract left and right margin and divide
// it with total number of cells in a particular table row
tab1.SetColumnWidth(TableColumn, 90);
// increase the value of variable holding the column count information
TableColumn += 1;
}
tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All, 0.1F);
tab1.ImportDataTable(dt, true, 0, 0, dt.Rows.Count, dt.Columns.Count);
sec1.Paragraphs.Add(tab1);
sec1.IsLandscape = true;
sec1.PageInfo.Margin.Left = 20;
sec1.PageInfo.Margin.Right = 0;
Aspose.Pdf.Generator.Row row = tab1.Rows[0];

//Iterate through all cells in the row and set their background color to blue
foreach (Aspose.Pdf.Generator.Cell curCell in row.Cells)
curCell.BackgroundColor = new Aspose.Pdf.Generator.Color(“SkyBlue”);
pdf1.Save(@“E:/test.pdf”);


Please feel free to contact us for any further assistance.

Best Regards,

Hi

Thanks for prividing the solution. The code you sent me works when dtc < 20. If there are more than 20 columns then these columns dont appear in output, Can you please increase dbc to 40 like


for (int dtc = 0; dtc < 40; dtc++)
dt.Columns.Add(“Head” + dtc, typeof(string));

and see what happens.

Thanks.

Best regards
Dani

Hi Dani,


Thanks for contacting support.

In case the table columns cannot be accommodated on single page, you may try using IsVerticalBroken property of table class.

tab1.IsVerticalBroken = true;

In case it does not resolve your problem, please feel free to contact.