检索信息后生成pdf文件

发布于 2024-10-21 09:44:16 字数 1536 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-10-28 09:44:16

以下是您可以使用的开源 PDF 库列表

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 返回二进制数据,请参阅 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();

Here's a list of Open Source PDF Libraries that you can use:

Example of 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");

Example of 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; 

Example of 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");

Example of iTextSharp

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

Returning on the fly created PDF-files

You can return Binary Data using Response.Write see MSDN on "How To Write Binary Data"

Here is an example on how you use Response.WriteFile to give the user a 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();
独自←快乐 2024-10-28 09:44:16

您可以使用 itextsharp 来实现

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();
}

you can use itextsharp for that

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文