【问题标题】:Write to pdf and print in windows forms application [closed]写入pdf并在Windows窗体应用程序中打印[关闭]
【发布时间】:2016-04-05 13:38:53
【问题描述】:

我正在使用 c# 在 Windows 窗体应用程序中开发一个账单管理系统。 在订单表格中,我必须以 pdf 格式生成发票,保存 pdf 并触发打印。 有两个按钮 GenerateBill 和 GenerateBill&Print。当我单击 GenerateBill&Print 时,订单详细信息应保存在数据库中并打印 pdf。我需要在 windows 窗体中创建 pdf 的代码。

【问题讨论】:

  • 你要问问题吗?
  • 很好,所以?至少从发布minimal reproducible example 开始。 SO 不是代码生成站点。
  • 欢迎来到stackoverflow。不幸的是,就目前而言,这个问题是题外话,因为您没有提出明确的问题。这听起来更像是项目需求,这不是一个征求代码编写的网站。
  • 祝你项目顺利

标签: c# winforms pdf


【解决方案1】:
using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace Full_Profile1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            //This is the absolute path to the PDF that we will create
            string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Sample.pdf");

            //Create a standard .Net FileStream for the file, setting various flags
            using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                //Create a new PDF document setting the size to A4
                using (Document doc = new Document(PageSize.A4))
                {
                    //Bind the PDF document to the FileStream using an iTextSharp PdfWriter
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open the document for writing
                        doc.Open();

                        //Create a table with two columns
                        PdfPTable t = new PdfPTable(2);

                        //Borders are drawn by the individual cells, not the table itself.
                        //Tell the default cell that we do not want a border drawn
                        t.DefaultCell.Border = 0;

                        //Add four cells. Cells are added starting at the top left of the table working left to right first, then down
                        t.AddCell("R1C1");
                        t.AddCell("R1C2");
                        t.AddCell("R2C1");
                        t.AddCell("R2C2");

                        //Add the table to our document
                        doc.Add(t);

                        //Close our document
                        doc.Close();
                    }
                }
            }

            this.Close();
        }
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多