vb.netpdfpdfsharpmigradoc

How to create a table using PDFsharp?


Just started using PDFsharp and it works fine, but now I want to create tables in my PDF, but tried other sources and found nothing.

So far I know how to use graph.drawString().


Solution

  • I could not find any clear and simple example of a basic table-template. pdfSharp is a very powerful but extremely low level library, so it's difficult to draw rectangles and text with fixed positions. Anyway, here is a little complete example that shows how to draw a table:

    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");
    
            }
    

    Notice you can print more pages within the document; just change the first "for".

    It should render like this: like this.