打印时设置页面格式

问题描述 投票:0回答:1

我尝试打印报告,但在格式化标题和页面时遇到一些问题。

这是一个例子:

在此输入图片描述

要显示的ID是39。

我想打印多页并在每页上打印页眉。

这是我现在的代码:

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    string IMAGE = "";
    sqlite.Open();  //Initiate connection to the db
    string stm = "SELECT VALORE FROM IMPOSTAZIONI WHERE IMPOSTAZIONE = 'LOGO';";
    using var cmd = new SQLiteCommand(stm, sqlite);
    using SQLiteDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        IMAGE = (string)rdr["VALORE"];
    }
    sqlite.Close();

    Image bgFront = Image.FromFile(@"Immagini\" + IMAGE);
    e.Graphics.DrawImage(bgFront, 50, 0, 100, 100);
    e.Graphics.DrawString("Report Movimenti in Uscita non Pagati", new Font("Courier New", 18, FontStyle.Bold), Brushes.Black, new PointF(210, 40));
    e.Graphics.DrawString(richTextBox1.Text, new Font("Courier New", 14, FontStyle.Bold), Brushes.Black, new PointF(50, 150));
}

private void button1_Click(object sender, EventArgs e)
{
    if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
    {
        printDocument1.DefaultPageSettings.Landscape = true;
        printDocument1.Print();
    }
}

private void printPreviewDialog1_Load(object sender, EventArgs e)
{
    printDocument1.DefaultPageSettings.Landscape = true;
}

您能帮我正确格式化此报告吗?

我想打印多页并在每页上打印页眉。

c# visual-studio winforms printing
1个回答
0
投票

从 SQLite 读取数据并将每个项目作为新行附加到 RichTextBox。 而要将RichTextBox的内容打印到带有页眉的多页,每页显示30行数据,可以尝试参考下面的示例代码。

    private void Form1_Load(object sender, EventArgs e)
    {
        CreateDatabase();
        CreateTable();
        for (int i = 0; i < 40; i++)
        {
            InsertData($"LOGO{i}", $"logo_value_{i}");

        }
        PopulateRichTextBox();
    }
 
 

    public void CreateDatabase()
    {
        SQLiteConnection.CreateFile("MyDatabase.sqlite");
     
    }
    public void PopulateRichTextBox()
    {
        using (SQLiteConnection connection = new SQLiteConnection("Data Source=MyDatabase.sqlite"))
        {
            connection.Open();
            string sql = "SELECT * FROM IMPOSTAZIONI";
        
            using (SQLiteCommand command = new SQLiteCommand(sql, connection))
            {
               
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                      
                        string impostazione = reader.GetString(reader.GetOrdinal("impostazione"));
                        string valore = reader.GetString(reader.GetOrdinal("valore"));

                      
                        richTextBox1.AppendText($"{impostazione}: {valore}\n");
                    }
                }
            }
        }

    }
    public void CreateTable()
    {
        using (SQLiteConnection connection = new SQLiteConnection("Data Source=MyDatabase.sqlite"))
        {
            connection.Open();
            string sql = " CREATE TABLE IF NOT EXISTS IMPOSTAZIONI (Id INTEGER PRIMARY KEY AUTOINCREMENT, IMPOSTAZIONE varchar(20), VALORE varchar(20)) ";
            SQLiteCommand command = new SQLiteCommand(sql, connection);
            command.ExecuteNonQuery();

          

            connection.Close();
        }

      
    }

    public void InsertData(string impostazione, string valore)
    {
        using (SQLiteConnection connection = new SQLiteConnection("Data Source=MyDatabase.sqlite"))
        {
            connection.Open();
            string sql = "INSERT INTO IMPOSTAZIONI (IMPOSTAZIONE, VALORE) VALUES (@impostazione, @valore)";
            using (SQLiteCommand command = new SQLiteCommand(sql, connection))
            {
                command.Parameters.AddWithValue("@impostazione", impostazione);
                command.Parameters.AddWithValue("@valore", valore);
                command.ExecuteNonQuery();
            }
        }
    }

    private int currentPageIndex = 0;
    private List<string> pagesContent;
    private string headerText = "Id\tIMPOSTAZIONE\tVALORE";
    private void PreparePagesContent()
    {
        string allText = richTextBox1.Text;
        int linesPerPage = 30;
        List<string> lines = new List<string>(allText.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries));
        pagesContent = new List<string>();

        for (int i = 0; i < lines.Count; i += linesPerPage)
        {
            List<string> pageLines = lines.Skip(i).Take(linesPerPage).ToList();
            StringBuilder pageContent = new StringBuilder();
            foreach (string line in pageLines)
            {
                pageContent.AppendLine(line);
            }
            pagesContent.Add(pageContent.ToString());
        }
    }

    private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
        PreparePagesContent();
        currentPageIndex = 0; 
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        string pageContent = pagesContent[currentPageIndex];

        e.Graphics.DrawString(headerText, new Font("Arial", 10, FontStyle.Bold), Brushes.Black, new PointF(50, 50));

        e.Graphics.DrawString(pageContent, new Font("Arial", 10), Brushes.Black, new PointF(50, 80));

        currentPageIndex++; 

        e.HasMorePages = (currentPageIndex < pagesContent.Count);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
        {
            printDocument1.DefaultPageSettings.Landscape = true;
            printDocument1.Print();
        }
    }
    private void printPreviewDialog1_Load(object sender, EventArgs e)
    {
        printPreviewDialog1.Document = printDocument1;
        printDocument1.DefaultPageSettings.Landscape = true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.