WPF -可以将DataGrid导出为PDF吗?

8xiog9wr  于 2023-01-14  发布在  其他
关注(0)|答案(2)|浏览(260)

我有以下带有自定义视觉效果的DataGrid:

是否可以将此DataGrid导出为pdf文件,以保留所有自定义视觉效果?
如果它帮助某人在这里是我的datagrid的xaml(代码是混乱的,可能不是最佳的,但这不是现在的重点,我只是需要帮助pdf的问题),也是另一个注意,我从C#添加列:

<DataGrid Name="dgMain" AlternationCount="2" AlternatingRowBackground="#f2f2f2" IsReadOnly="True" GridLinesVisibility="None" VerticalGridLinesBrush="White">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <Setter Property="Background" Value="LightGray"/>
                <Setter Property="FontSize" Value="18"/>
                <Setter Property="Foreground" Value="White"/>
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Height" Value="50"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="DataGridColumnHeader">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Border BorderThickness="1" Padding="3,0,3,0" Height="{TemplateBinding Height}" Grid.Column="0">
                                    <Border.Background>
                                        <SolidColorBrush Color="#c7d9f1"/>
                                    </Border.Background>
                                    <ContentPresenter VerticalAlignment="{TemplateBinding VerticalAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                                </Border>

                                <Thumb x:Name="PART_RightHeaderGripper" Grid.Column="1" HorizontalAlignment="Right" Width="2" BorderThickness="1" Cursor="SizeWE" BorderBrush="{Binding VerticalGridLinesBrush, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Padding" Value="10" />
                <Setter Property="FontSize" Value="16" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border Padding="{TemplateBinding Padding}">
                                <ContentPresenter></ContentPresenter>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.Columns>
        </DataGrid.Columns>
    </DataGrid>
6ioyuze2

6ioyuze21#

我已经修改了这个example,以适应数据网格中的动态列计数,这建议安装iTextSharp nuget包。
https://www.nuget.org/packages/itext7/

public void ExportToPdf(DataTable dt)
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("c://sample.pdf", FileMode.Create));
        document.Open();

        Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 5);

        PdfPTable table = new PdfPTable(dt.Columns.Count);

        Array floatArray = Array.CreateInstance(typeof(float), dt.Columns.Count);
        //float[] widths = new float[] { };
        for (int i = 0; i < dt.Columns.Count; i++)
            floatArray.SetValue(4f, i);

        table.SetWidths((float[])floatArray);

        table.WidthPercentage = 100;
        PdfPCell cell = new PdfPCell(new Phrase("Products"));

        cell.Colspan = dt.Columns.Count;

        foreach (DataColumn c in dt.Columns)
        {
            table.AddCell(new Phrase(c.ColumnName, font5));
        }

        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    table.AddCell(new Phrase(r[i].ToString(), font5));
                }
            }
        }
        document.Add(table);
        document.Close();
    }
sd2nnvve

sd2nnvve2#

下面是一个使用iText 7的例子,类似于@farrukh-niaz的回答。

//Imports
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.IO.Font;
using iText.Kernel.Pdf.Canvas.Draw;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.IO.Font.Constants;

//Export to PDF function
public void ExportToPdf(DataTable dt)
{
            PdfWriter writer = new PdfWriter(dest);
            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf);
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);                       

            Table table = new Table(dataGrid.Columns.Count);            

            foreach (DataColumn c in dt.Columns)
            {
                table.AddCell(new Paragraph(c.ColumnName.ToString()).SetFont(bold).SetBackgroundColor(ColorConstants.GRAY).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER));
            }

            foreach(DataRow r in dt.Rows)
            {
                if(dt.Rows.Count > 0)
                {
                    for(int i = 0; i<dt.Columns.Count; i++)
                    {
                        table.AddCell(new Paragraph(r[i].ToString()).SetFont(font).SetBackgroundColor(ColorConstants.WHITE).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER));
                    }
                }
            }

 document.Add(table);
 document.Close();
}

相关问题