将数据库中的数据放入PDF(C#)

Put data from my database to my PDF (C#)
2020-11-21
  •  译文(汉语)
  •  原文(英语)

我正在C#中创建一个from,在学生登录时将生成PDF.完成模板后,我的问题是如何将数据库中的数据放入PDF.

PDF中需要显示的数据是学生的姓名,学生的课程,学分以及他/她注册的所有学科.

所有数据都存储在学生表和登记表中.表格如下:

mysql> describe enroll;
+-----------------+---------+------+-----+---------+----------------+
| Field           | Type    | Null | Key | Default | Extra          |
+-----------------+---------+------+-----+---------+----------------+
| Eid             | int(11) | NO   | PRI | NULL    | auto_increment |
| Students_studid | int(11) | NO   | MUL | NULL    |                |
| Subjects_Subjid | int(11) | NO   | MUL | NULL    |                |
+-----------------+---------+------+-----+---------+----------------+

mysql> describe students;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| studid     | int(11)     | NO   | PRI | NULL    | auto_increment |
| StudName   | varchar(45) | YES  |     | NULL    |                |
| StudCourse | varchar(45) | YES  |     | NULL    |                |
| StudYr     | int(11)     | YES  |     | NULL    |                |
| StudGender | varchar(45) | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

这是我生成的PDF表单:

    class Form_PDF : Form {

        public Form_PDF(){
        Text = "PDF";

        CenterToScreen();

        System.IO.FileStream fs = new FileStream(""+Form_Login.getUser+".pdf", FileMode.Create);

           Document document = new Document(PageSize.A4, 25, 25, 30, 30);

           PdfWriter writer = PdfWriter.GetInstance(document, fs);


           document.AddAuthor("The Author");

           document.AddCreator("using iTextSharp");

           document.AddKeywords("C# PDF");

           document.AddSubject("C# PDF");

           document.AddTitle("PDF creation using iTextSharp");


           document.Open();




           PdfContentByte cb = writer.DirectContent;
           PdfContentByte xy = writer.DirectContent;

           //BaseFont f_cb = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
            BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);


           //XY
           xy.BeginText();
           xy.SetFontAndSize(f_cn, 9);

           xy.SetTextMatrix(110, 661); // Left, Top
           xy.ShowText("Student Name: " +Form_Login.getUser);

           xy.SetTextMatrix(110, 637); // Left, Top
           xy.ShowText("Semester: ");



           xy.SetTextMatrix(230, 661); // Left, Top
           xy.ShowText("Course: "); 
            sqlConnection1.Close();

           xy.SetTextMatrix(230, 637); // Left, Top
           xy.ShowText("Section: ");

           xy.SetTextMatrix(230, 613); // Left, Top
           xy.ShowText("Division: ");

           xy.SetTextMatrix(375, 661); // Left, Top
           xy.ShowText("School Year: ");

           xy.SetTextMatrix(375, 637); // Left, Top
           xy.ShowText("Credit Units: ");

           cb.SetLineWidth(0f);
           cb.MoveTo(100, 601);
           cb.LineTo(510, 601);
           cb.Stroke();

           xy.SetTextMatrix(120, 577); // Left, Top
           xy.ShowText("Subject Code");

           xy.SetTextMatrix(195, 577); // Left, Top
           xy.ShowText("Subject Description");

           xy.SetTextMatrix(310, 577); // Left, Top
           xy.ShowText("Subject Schedule");

           xy.SetTextMatrix(405, 577); // Left, Top
           xy.ShowText("Room #");

           xy.SetTextMatrix(470, 577); // Left, Top
           xy.ShowText("Units");

           cb.SetLineWidth(0f);
           cb.MoveTo(100, 560);
           cb.LineTo(510, 560);
           cb.Stroke();

           xy.SetTextMatrix(120, 313); // Left, Top
           xy.ShowText("NOTE: Assessment on this Registration Card is TEMPORARY only. Final assessment will be determined by");

           xy.SetTextMatrix(145, 301); // Left, Top
           xy.ShowText("the Finance office.");

           cb.SetLineWidth(0f);
           cb.MoveTo(400, 253);
           cb.LineTo(510, 253);
           cb.Stroke();

           xy.SetTextMatrix(425, 241); // Left, Top
           xy.ShowText("Registrar Officer");

           int row = 1;
            for (int y = 0; y != 70; y++)
            {
               xy.SetTextMatrix(10, row);
               xy.ShowText("Y: " + row.ToString());
               row += 12; // The spacing between the rows is set to 12 "points"
            }
            int col = 35;
            for (int x = 0; x != 22; x++)
            {
               xy.SetTextMatrix(col, 829);
               xy.ShowText("X: " + col.ToString());
               col += 25; // The spacing between the columns is set to 25 "points"
            }
            xy.EndText();


            cb.BeginText();
            cb.SetFontAndSize(f_cn, 16);


            //START DIRI
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("C:\\Users\\db\\Desktop\\addu-logo.jpg");
            img.SetAbsolutePosition(185, 697);
            img.ScalePercent(10f);
            cb.AddImage(img);


            cb.SetTextMatrix(240, 733); // Left, Top
            cb.ShowText("Ateneo de Davao University");

            cb.SetTextMatrix(280, 718);
            cb.ShowText("Registrar's Office");

            cb.SetTextMatrix(266, 703);
            cb.ShowText("Student Registration");


            cb.EndText();



           //document.Add(new Paragraph("Ateneo de Davao University"));
           document.Close();
           writer.Close();
           fs.Close();



    }
}
速聊1:
那你的问题是什么?
速聊2:
我需要插入学生的课程,以及他/她注册的PDF课程(我该怎么做?)
速聊3:
太...改写您的问题,您想知道的是...如何从C#中的MySql数据库获取数据?
解决过程1

这只是一个例子:

using System;
using MySql.Data.MySqlClient; 

public class Example
{

    static void Main() 
    {
        string cs = @"server=localhost;userid=user12;
            password=34klq*;database=mydb";

        MySqlConnection conn = null;
        MySqlDataReader rdr = null;

        try 
        {
            conn = new MySqlConnection(cs);
            conn.Open();

            string stm = "SELECT * FROM Authors";
            MySqlCommand cmd = new MySqlCommand(stm, conn);
            rdr = cmd.ExecuteReader();

            while (rdr.Read()) 
            {
                Console.WriteLine(rdr.GetInt32(0) + ": " 
                    + rdr.GetString(1));
            }

        } catch (MySqlException ex) 
        {
            Console.WriteLine("Error: {0}",  ex.ToString());

        } finally 
        {
            if (rdr != null) 
            {
                rdr.Close();
            }

            if (conn != null) 
            {
                conn.Close();
            }

        }
    }
}

对于复杂的信息请访问

http://zetcode.com/db/mysqlcsharptutorial/

来自MySQL的数据将在这里

while (rdr.Read()) 
            {
                Console.WriteLine(rdr.GetInt32(0) + ": " 
                    + rdr.GetString(1));
            }

在这里循环可以让您一一录制,

更改密码

Console.WriteLine(rdr.GetInt32(0)+":" + rdr.GetString(1));

如您所愿

I am creating a from in C# that will generate a PDF when a student logs in. I am done with the template, my problem is how to put the data on my database to my PDF.

The data that is needed to be present in the PDF is the student's name, student's course, credit units, and all the subjects he/she is enrolled.

all data are stored in the students and enrolled table. here are the tables:

mysql> describe enroll;
+-----------------+---------+------+-----+---------+----------------+
| Field           | Type    | Null | Key | Default | Extra          |
+-----------------+---------+------+-----+---------+----------------+
| Eid             | int(11) | NO   | PRI | NULL    | auto_increment |
| Students_studid | int(11) | NO   | MUL | NULL    |                |
| Subjects_Subjid | int(11) | NO   | MUL | NULL    |                |
+-----------------+---------+------+-----+---------+----------------+

and

mysql> describe students;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| studid     | int(11)     | NO   | PRI | NULL    | auto_increment |
| StudName   | varchar(45) | YES  |     | NULL    |                |
| StudCourse | varchar(45) | YES  |     | NULL    |                |
| StudYr     | int(11)     | YES  |     | NULL    |                |
| StudGender | varchar(45) | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+

and here is my generate PDF form:

    class Form_PDF : Form {

        public Form_PDF(){
        Text = "PDF";

        CenterToScreen();

        System.IO.FileStream fs = new FileStream(""+Form_Login.getUser+".pdf", FileMode.Create);

           Document document = new Document(PageSize.A4, 25, 25, 30, 30);

           PdfWriter writer = PdfWriter.GetInstance(document, fs);


           document.AddAuthor("The Author");

           document.AddCreator("using iTextSharp");

           document.AddKeywords("C# PDF");

           document.AddSubject("C# PDF");

           document.AddTitle("PDF creation using iTextSharp");


           document.Open();




           PdfContentByte cb = writer.DirectContent;
           PdfContentByte xy = writer.DirectContent;

           //BaseFont f_cb = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
            BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);


           //XY
           xy.BeginText();
           xy.SetFontAndSize(f_cn, 9);

           xy.SetTextMatrix(110, 661); // Left, Top
           xy.ShowText("Student Name: " +Form_Login.getUser);

           xy.SetTextMatrix(110, 637); // Left, Top
           xy.ShowText("Semester: ");



           xy.SetTextMatrix(230, 661); // Left, Top
           xy.ShowText("Course: "); 
            sqlConnection1.Close();

           xy.SetTextMatrix(230, 637); // Left, Top
           xy.ShowText("Section: ");

           xy.SetTextMatrix(230, 613); // Left, Top
           xy.ShowText("Division: ");

           xy.SetTextMatrix(375, 661); // Left, Top
           xy.ShowText("School Year: ");

           xy.SetTextMatrix(375, 637); // Left, Top
           xy.ShowText("Credit Units: ");

           cb.SetLineWidth(0f);
           cb.MoveTo(100, 601);
           cb.LineTo(510, 601);
           cb.Stroke();

           xy.SetTextMatrix(120, 577); // Left, Top
           xy.ShowText("Subject Code");

           xy.SetTextMatrix(195, 577); // Left, Top
           xy.ShowText("Subject Description");

           xy.SetTextMatrix(310, 577); // Left, Top
           xy.ShowText("Subject Schedule");

           xy.SetTextMatrix(405, 577); // Left, Top
           xy.ShowText("Room #");

           xy.SetTextMatrix(470, 577); // Left, Top
           xy.ShowText("Units");

           cb.SetLineWidth(0f);
           cb.MoveTo(100, 560);
           cb.LineTo(510, 560);
           cb.Stroke();

           xy.SetTextMatrix(120, 313); // Left, Top
           xy.ShowText("NOTE: Assessment on this Registration Card is TEMPORARY only. Final assessment will be determined by");

           xy.SetTextMatrix(145, 301); // Left, Top
           xy.ShowText("the Finance office.");

           cb.SetLineWidth(0f);
           cb.MoveTo(400, 253);
           cb.LineTo(510, 253);
           cb.Stroke();

           xy.SetTextMatrix(425, 241); // Left, Top
           xy.ShowText("Registrar Officer");

           int row = 1;
            for (int y = 0; y != 70; y++)
            {
               xy.SetTextMatrix(10, row);
               xy.ShowText("Y: " + row.ToString());
               row += 12; // The spacing between the rows is set to 12 "points"
            }
            int col = 35;
            for (int x = 0; x != 22; x++)
            {
               xy.SetTextMatrix(col, 829);
               xy.ShowText("X: " + col.ToString());
               col += 25; // The spacing between the columns is set to 25 "points"
            }
            xy.EndText();


            cb.BeginText();
            cb.SetFontAndSize(f_cn, 16);


            //START DIRI
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("C:\\Users\\db\\Desktop\\addu-logo.jpg");
            img.SetAbsolutePosition(185, 697);
            img.ScalePercent(10f);
            cb.AddImage(img);


            cb.SetTextMatrix(240, 733); // Left, Top
            cb.ShowText("Ateneo de Davao University");

            cb.SetTextMatrix(280, 718);
            cb.ShowText("Registrar's Office");

            cb.SetTextMatrix(266, 703);
            cb.ShowText("Student Registration");


            cb.EndText();



           //document.Add(new Paragraph("Ateneo de Davao University"));
           document.Close();
           writer.Close();
           fs.Close();



    }
}
Talk1:
And what is your question?
Talk2:
I need to insert the course of the student, and the subjects he/she is enrolled in the PDF (how do I do it?)
Talk3:
Sooo... rephrasing your question, what you want to know is... how to get data from a MySql database in C#?
Solutions1

it is just a Example :

using System;
using MySql.Data.MySqlClient; 

public class Example
{

    static void Main() 
    {
        string cs = @"server=localhost;userid=user12;
            password=34klq*;database=mydb";

        MySqlConnection conn = null;
        MySqlDataReader rdr = null;

        try 
        {
            conn = new MySqlConnection(cs);
            conn.Open();

            string stm = "SELECT * FROM Authors";
            MySqlCommand cmd = new MySqlCommand(stm, conn);
            rdr = cmd.ExecuteReader();

            while (rdr.Read()) 
            {
                Console.WriteLine(rdr.GetInt32(0) + ": " 
                    + rdr.GetString(1));
            }

        } catch (MySqlException ex) 
        {
            Console.WriteLine("Error: {0}",  ex.ToString());

        } finally 
        {
            if (rdr != null) 
            {
                rdr.Close();
            }

            if (conn != null) 
            {
                conn.Close();
            }

        }
    }
}

for complite information kindly visit

http://zetcode.com/db/mysqlcsharptutorial/

the data from mysql will come in this

while (rdr.Read()) 
            {
                Console.WriteLine(rdr.GetInt32(0) + ": " 
                    + rdr.GetString(1));
            }

here loop will gives you to record one by one,

change the code

Console.WriteLine(rdr.GetInt32(0) + ": " + rdr.GetString(1));

as you want in your example

转载于:https://stackoverflow.com/questions/28333835/put-data-from-my-database-to-my-pdf-c

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入