Не отображается текст в pdf-файле, экспортированного из DataGrid
Подскажите, что не так?
public DataGridCell GetCell(int row, int column)
{
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
dataGrid_postoffices.ScrollIntoView(rowContainer, dataGrid_postoffices.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
public DataGridRow GetRow(int index)
{
DataGridRow row = (DataGridRow)dataGrid_postoffices.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
dataGrid_postoffices.UpdateLayout();
dataGrid_postoffices.ScrollIntoView(dataGrid_postoffices.Items[index]);
row = (DataGridRow)dataGrid_postoffices.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
private void button_postoffices_exportpdf_Click(object sender, RoutedEventArgs e)
{
// Create a new PDF document
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, new FileStream("PostOffices.pdf", FileMode.Create));
pdfDoc.Open();
// Add a table to the PDF document
PdfPTable pdfTable = new PdfPTable(dataGrid_postoffices.Columns.Count);
pdfTable.WidthPercentage = 100;
// Add the column headers to the table
for (int i = 0; i < dataGrid_postoffices.Columns.Count; i++)
{
PdfPCell cell = new PdfPCell(new Phrase(dataGrid_postoffices.Columns[i].Header.ToString()));
pdfTable.AddCell(cell);
/*new Phrase(dataGrid_postoffices.Columns[i].Header.ToString())*/
}
// Copy the data from the DataGrid to the worksheet
for (int i = 0; i < dataGrid_postoffices.Items.Count; i++)
{
for (int j = 0; j < dataGrid_postoffices.Columns.Count; j++)
{
//var textData = GetCell(i, j);
//TextBlock tb = textData.Content as TextBlock;
//var value = tb.Text;
pdfTable.AddCell(new Phrase((dataGrid_postoffices.Columns[j].GetCellContent(dataGrid_postoffices.Items[i]) as TextBlock)?.Text ?? ""));
//pdfTable.AddCell(new Phrase(dataGrid_postoffices.Items[i].ToString()));
}
}
// Add the table to the PDF document
pdfDoc.Add(pdfTable);
// Close the PDF document
pdfDoc.Close();
}
