检索信息后生成pdf文件

时间:2011-03-09 10:22:24

标签: c# asp.net pdf-generation

我需要根据从数据库获取的数据创建pdf文件。我必须从数据库中检索数据并将这些数据生成为pdf文件。

我想有指针(有用的信息)。

谢谢。

5 个答案:

答案 0 :(得分:9)

以下是您可以使用的list of Open Source PDF Librarie

PDFjet示例

PDF pdf = new PDF();
Font f1 = new Font(pdf, "Helvetica");
Image image1 = new Image(pdf, "images/eu-map.png");
Image image2 = new Image(pdf, "images/fruit.jpg");
Image image3 = new Image(pdf, "images/mt-map.gif");
// Please note:
// All font and image objects must be created
// before the first page object.


Page page = new Page(pdf, A4.PORTRAIT);

text.SetText(
        "The map on the right is an embedded GIF image");
text.SetPosition(90.0, 800);
text.DrawOn(page);

image3.SetPosition(390, 630);
image3.ScaleBy(0.5);
image3.DrawOn(page);

pdf.wrap();
pdf.save("Example_03.pdf");

SharpPDF的例子

pdfDocument myDoc = new pdfDocument("TUTORIAL","ME");
pdfPage myPage = myDoc.addPage();
myPage.addText("Hello World!",200,450,predefinedFont.csHelvetica,20);
myDoc.createPDF(@"c:\test.pdf");
myPage = null;
myDoc = null; 

Report.NET示例

Report report = new Report(new PdfFormatter());
FontDef fd = new FontDef(report, "Helvetica");
FontProp fp = new FontPropMM(fd, 25);
Page page = new Page(report);
page.AddCenteredMM(80, new RepString(fp, "Hello World!"));
RT.ViewPDF(report, "HelloWorld.pdf");

iTextSharp示例

Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("hello.pdf"));
document.open();  
document.add(new Paragraph("Hello Pdf"));
document.close(); 

动态返回创建PDF文件

您可以使用Response.Write返回二进制数据,请参阅"How To Write Binary Data"

上的MSDN

以下是有关如何使用Response.WriteFile向用户提供PDF的示例:

//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();

答案 1 :(得分:6)

您可以{/ 3}}使用

protected void btnExportPdf_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=BusinessUnit.pdf");
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView grd = new GridView();
    grd.DataSource = yourdatatable.DefaultView//get data from DB in Datatable
    grd.DataBind();
    grd.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A2, 8f, 8f, 8f, 8f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

答案 2 :(得分:4)

答案 3 :(得分:2)

尝试使用已建立的库之一来创建pdf。这是一篇很好的信息和很多资源的帖子

https://stackoverflow.com/questions/177/how-do-i-programmatically-create-a-pdf-in-my-net-application/2384209#2384209

答案 4 :(得分:1)

菲利普的早期答案太棒了。此链接可能对itextsharp有帮助:

http://forums.asp.net/t/1419119.aspx