C# 如何在pdf文件中显示文本?
我使用的是c#.net,需要在.pdf文件中显示一些数据。我可以通过使用PdfTable来实现这一点,但它是以表格格式显示的。 我想要一个简单的格式,如: 我不想要精确的表格格式。我正在使用iTextSharpdll。 是否可以以上述格式显示数据C# 如何在pdf文件中显示文本?,c#,.net,itextsharp,C#,.net,Itextsharp,我使用的是c#.net,需要在.pdf文件中显示一些数据。我可以通过使用PdfTable来实现这一点,但它是以表格格式显示的。 我想要一个简单的格式,如: 我不想要精确的表格格式。我正在使用iTextSharpdll。 是否可以以上述格式显示数据 欢迎您提出任何建议。您可以使用PdfParagraph类来呈现PDF格式的genreal文本在iTextSharp dll中,您只需通过创建字符串形式的table/tr/td结构来传递数据,如下所示: string strData = string.
欢迎您提出任何建议。您可以使用
PdfParagraph
类来呈现PDF格式的genreal文本在iTextSharp dll中,您只需通过创建字符串形式的table/tr/td结构来传递数据,如下所示:
string strData = string.Emrty;
strData = "<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
strData += "<tr>";
strData += "<td>";
strData += "</td>";
strData += "</tr>";
strData += "</table>";
string strData=string.Emrty;
strData=“”;
标准数据+=”;
标准数据+=”;
标准数据+=”;
标准数据+=”;
标准数据+=”;
通过创建数据的table/tr/td结构,只需将此变量数据传递到pdf,然后您将得到pdf格式的结果输出。那么,当您说不需要精确的表格格式时,您是在谈论边界吗?如果是这样,我建议使用
PdfPTable
,然后关闭边框。PdfPTable
中的单元格都基于该表的DefaultCell
,因此要关闭名为t
的表的边框,请执行以下操作:
t.DefaultCell.Border = 0;
下面是一个针对iTextSharp 5.1.1.0的完整WinForms应用程序,该应用程序正是这样做的:
//PdfPTable Example
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//PDF file to output
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
//Create a basic stream to write to
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
//Create a new PDF document
using (Document doc = new Document())
{
//Bind a the document to the stream
using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
{
//Open our PDF for writing
doc.Open();
//Insert a new page into our output PDF
doc.NewPage();
//Create a three column table
PdfPTable t = new PdfPTable(3);
//Remove the border from the table
t.DefaultCell.Border = 0;
//For the first row we some extra padding
t.DefaultCell.PaddingBottom = 10;
t.AddCell("ColumnA");
t.AddCell("ColumnB");
t.AddCell("ColumnC");
//Reset the padding for the remaining cells
t.DefaultCell.PaddingBottom = 0;
t.AddCell("Value1");
t.AddCell("Value1");
t.AddCell("Value1");
t.AddCell("Value2");
t.AddCell("Value2");
t.AddCell("Value2");
//Add the table to the document
doc.Add(t);
//Close our output PDF
doc.Close();
}
}
}
this.Close();
}
}
}
如果出于某种原因,您不想使用PdfPTable
进行此操作,您可以尝试使用段落
对其进行黑客攻击,但除非对齐不重要,否则您会感到头痛。下面的示例使用段落,并将段落中的每个“列”的格式设置为每个20个字符,根据需要填充空格注意:这是20个字符的可变宽度字体,因此实际上不会对齐。如果您想这样做,可以使用上面的PdfPTable
,或者您必须测量不有趣的字符串
//Paragraph Example
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//PDF file to output
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
//Create a basic stream to write to
using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
//Create a new PDF document
using (Document doc = new Document())
{
//Bind a the document to the stream
using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
{
//Open our PDF for writing
doc.Open();
//Insert a new page into our output PDF
doc.NewPage();
//Right-pad each "column" for a total of 20 characters
string FormatString = "{0,-20}{1,-20}{2,-20}";
//Add the first row
doc.Add(new Paragraph(String.Format(FormatString, "ColumnA", "ColumnB", "ColumnC")));
//Add a blank line
doc.Add(new Paragraph(""));
//Add the two data rows
doc.Add(new Paragraph(String.Format(FormatString, "Value1", "Value1", "Value1")));
doc.Add(new Paragraph(String.Format(FormatString, "Value2", "Value2", "Value2")));
//Close our output PDF
doc.Close();
}
}
}
this.Close();
}
}
}