C#, Winform - creating PDF

前端 未结 4 1795
时光说笑
时光说笑 2020-12-09 23:48

I\'m still kinda new on the programming thing, but i would like to create a program wich could create PDF\'s with some information in them.

Could anybody recommend a

相关标签:
4条回答
  • 2020-12-10 00:29

    As @Jon Skeet said, you can use iTextSharp (which is a C# port of the Java iText).

    First, download iTextSharp (currently 5.1.2), extract itextsharp.dll to some location and add a reference to it in Visual Studio. Then use the following code which is a full-working WinForms app that creates a very basic table in an A4 document. See the comments in the code for more of an explanation.

    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();
            }
        }
    }
    
    0 讨论(0)
  • You probably want to use a library such as iText, which will let you build up a PDF document programmatically.

    It's not clear what you mean by "creating it from within Visual Studio 2010" - if you're expecting a visual designer, I think you'll be disappointed; I don't know of anything which would allow you to do that easily. However, it doesn't sound like you have particularly tricky requirements, so just writing the code to do it shouldn't be too hard.

    0 讨论(0)
  • 2020-12-10 00:40

    There are several libraries available for that purpose. Here are a few:

    • iTextSharp
    • PDFSharp
    • SharpPDF
    0 讨论(0)
  • 2020-12-10 00:54

    I have used iTextSharp once to merge and split pdf, but has lost those files. But iTextSharp is good. And regarding your need to create tables and all, I think you will have to write regular code and then you will have to convert them into bytes and then create a pdf file out of it. As I remember, iTextSharp work that way. Hope it helps.

    0 讨论(0)
提交回复
热议问题