如何使用 PDFsharp 创建表格?

雷格伦·格拉姆霍尔特

刚开始使用 PDFsharp 并且它工作正常,但现在我想在我的 PDF 中创建表格,但尝试了其他来源并没有发现任何东西。

到目前为止,我知道如何使用graph.drawString().

乔治·卡拉科

我找不到任何清晰简单的基本表格模板示例。pdfSharp 是一个非常强大但极低级别的库,因此很难绘制具有固定位置的矩形和文本。无论如何,这里有一个完整的例子,展示了如何绘制表格:

protected void ExportGraf_Click(object sender, EventArgs e)
{
    PdfDocument document = new PdfDocument();
    document.Info.Title = "Table Example";

    for (int p=0; p<1; p++) 
    { 
        // Page Options
        PdfPage pdfPage = document.AddPage();
        pdfPage.Height = 842;//842
        pdfPage.Width = 590;

        // Get an XGraphics object for drawing
        XGraphics graph = XGraphics.FromPdfPage(pdfPage);

        // Text format
        XStringFormat format = new XStringFormat();
        format.LineAlignment = XLineAlignment.Near;
        format.Alignment = XStringAlignment.Near;
        var tf = new XTextFormatter(graph);
           
        XFont fontParagraph = new XFont("Verdana", 8, XFontStyle.Regular);

        // Row elements
        int el1_width = 80;
        int el2_width = 380;

        // page structure options
        double lineHeight = 20;
        int marginLeft = 20;
        int marginTop = 20;
           
        int el_height = 30;
        int rect_height = 17;

        int interLine_X_1 = 2;
        int interLine_X_2 = 2 * interLine_X_1;

        int offSetX_1 = el1_width;
        int offSetX_2 = el1_width + el2_width;

        XSolidBrush rect_style1 = new XSolidBrush(XColors.LightGray);
        XSolidBrush rect_style2 = new XSolidBrush(XColors.DarkGreen);
        XSolidBrush rect_style3= new XSolidBrush(XColors.Red);

        for (int i = 0; i < 30; i++)
        {
            double dist_Y = lineHeight * (i + 1);
            double dist_Y2 = dist_Y - 2;

            // header della G
            if (i == 0)
            {
                graph.DrawRectangle(rect_style2, marginLeft, marginTop, pdfPage.Width-2* marginLeft, rect_height);

                tf.DrawString("column1", fontParagraph, XBrushes.White,
                              new XRect(marginLeft, marginTop, el1_width, el_height), format);

                tf.DrawString("column2", fontParagraph, XBrushes.White,
                              new XRect(marginLeft + offSetX_1 + interLine_X_1, marginTop , el2_width, el_height), format);

                tf.DrawString("column3", fontParagraph, XBrushes.White,
                              new XRect(marginLeft + offSetX_2 + 2 * interLine_X_2, marginTop, el1_width, el_height), format);

                // stampo il primo elemento insieme all'header
                graph.DrawRectangle(rect_style1, marginLeft, dist_Y2 + marginTop, el1_width, rect_height);
                tf.DrawString("text1", fontParagraph, XBrushes.Black,
                              new XRect(marginLeft, dist_Y + marginTop, el1_width, el_height), format);

                //ELEMENT 2 - BIG 380
                            graph.DrawRectangle(rect_style1, marginLeft + offSetX_1 + interLine_X_1, dist_Y2 + marginTop, el2_width, rect_height);
                            tf.DrawString(
                                "text2",
                                fontParagraph,
                                XBrushes.Black,
                                new XRect(marginLeft + offSetX_1 + interLine_X_1, dist_Y + marginTop, el2_width, el_height),
                                format);


                            //ELEMENT 3 - SMALL 80

                            graph.DrawRectangle(rect_style1, marginLeft + offSetX_2 + interLine_X_2, dist_Y2 + marginTop, el1_width, rect_height);
                            tf.DrawString(
                                "text3",
                                fontParagraph,
                                XBrushes.Black,
                                new XRect(marginLeft + offSetX_2 + 2 * interLine_X_2, dist_Y + marginTop, el1_width, el_height),
                                format);
              

                    }
                        else { 

                        //if (i % 2 == 1)
                        //{
                        //  graph.DrawRectangle(TextBackgroundBrush, marginLeft, lineY - 2 + marginTop, pdfPage.Width - marginLeft - marginRight, lineHeight - 2);
                        //}

                        //ELEMENT 1 - SMALL 80
                        graph.DrawRectangle(rect_style1,  marginLeft, marginTop + dist_Y2, el1_width, rect_height);
                        tf.DrawString(
                
                            "text1",
                            fontParagraph,
                            XBrushes.Black,
                            new XRect(marginLeft, marginTop + dist_Y, el1_width, el_height),            
                            format);

                        //ELEMENT 2 - BIG 380
                        graph.DrawRectangle(rect_style1, marginLeft + offSetX_1 + interLine_X_1 , dist_Y2 + marginTop, el2_width, rect_height);
                        tf.DrawString(
                            "text2",
                            fontParagraph,
                            XBrushes.Black,
                            new XRect(marginLeft + offSetX_1 + interLine_X_1, marginTop + dist_Y, el2_width, el_height),
                            format);


                        //ELEMENT 3 - SMALL 80
         
                        graph.DrawRectangle(rect_style1, marginLeft + offSetX_2 + interLine_X_2, dist_Y2 + marginTop, el1_width, rect_height);
                        tf.DrawString(
                            "text3",
                            fontParagraph,
                            XBrushes.Black,
                            new XRect(marginLeft + offSetX_2 + 2 *interLine_X_2, marginTop + dist_Y, el1_width, el_height),
                            format);

                        }

                    }


            }


            const string filename = "C:\\Users\\Desktop\\test\\HelloWorld.pdf";
            document.Save(filename);

            //byte[] bytes = null;
            //using (MemoryStream stream = new MemoryStream())
            //{
            //    document.Save(stream, true);
            //    bytes = stream.ToArray();
            //}

            //SendFileToResponse(bytes, "HelloWorld_test.pdf");

        }

请注意,您可以在文档中打印更多页面;只需更改第一个“for”。

它应该呈现这样的:像这样

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章