MMM*_*MMM 2 c# pdf visual-studio winforms
我在编程方面仍然有点新鲜,但我想创建一个可以创建PDF的程序,其中包含一些信息.
任何人都可以推荐这样做的简洁方法,我需要创建一个带有桌子的常规A4页面......以及其他一些信息.
是否可以在Visual Studio 2010中创建它 - 或者我是否需要某种类似的加载项?
正如@Jon Skeet所说,你可以使用iTextSharp(它是Java iText的C#端口).
首先,下载iTextSharp(当前为5.1.2),解压缩itextsharp.dll
到某个位置并在Visual Studio中添加对它的引用.然后使用以下代码,这是一个全功能的WinForms应用程序,可在A4文档中创建一个非常基本的表.有关更多说明,请参阅代码中的注释.
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11759 次 |
最近记录: |