转载:http://dev.mjxy.cn/a-Create-pdf-using-Apache-NFOP.aspx

//**************************************
// Name: Create in memory PDF documents in ASP.NET using Apache NFOP
// Description:The sample demonstrates how to create PDF documents in memory using the open source Apache NFOP(http://sourceforge.net/projects/nfop/) and stream the same to browser instead of saving the PDF documents to harddrive.
// By: Azeet Chebrolu
//
//
// Inputs:Path to your XML Data File and Path to the XSLT Transformation file.
//
// Returns:None
//
//Assumes:Add reference to ApacheFop.Net.dll and vJsLib.dll which comes with Visual J#.net
//
//Side Effects:None
//**************************************

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using org.apache.fop;
using org.apache.fop.apps;
using org.apache.fop.tools;
using org.xml.sax;
using java.io;
using System.Text;

public partial class output : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e){}
 protected void Button1_Click(object sender, EventArgs e)
 {
  StreamPDF(Server.MapPath("CP0000001.xml"), Server.MapPath("pdf.xslt"));
 }
 private static void StreamPDF(string XMLFile,string XSLTFile)
 {
  // Load the style sheet.
  XslCompiledTransform xslt = new XslCompiledTransform();
  xslt.Load(XMLFile);
  XmlDocument objSourceData = new XmlDocument();

//Load the Source XML Document
  objSourceData.Load(XSLTFile);

// Execute the transform and output the results to a file.
  MemoryStream ms = new MemoryStream();
  xslt.Transform(objSourceData, null, ms);

//Convert the Byte Array from MemoryStream to SByte Array
  sbyte[] inputFOBytes = ToSByteArray(ms.ToArray());
  InputSource inputFoFile = new org.xml.sax.InputSource(new ByteArrayInputStream(inputFOBytes));
  ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
  org.apache.fop.apps.Driver dr = new org.apache.fop.apps.Driver(inputFoFile, bos);
  dr.setRenderer(org.apache.fop.apps.Driver.RENDER_PDF);
  dr.run();

//Convert the SByte Array to Byte Array to stream to the Browser
  byte[] getBytes = ToByteArray(bos.toByteArray());
  MemoryStream msPdf = new MemoryStream(getBytes);
  Response.ContentType = "application/pdf";
  Response.AddHeader("Content-disposition", "filename=output.pdf");
  Response.OutputStream.Write(getBytes, 0, getBytes.Length);
  Response.OutputStream.Flush();
  Response.OutputStream.Close();
 }

private static SByte[] ToSByteArray(Byte[] source)
 {
  sbyte[] sbytes = new sbyte[source.Length];
  System.Buffer.BlockCopy(source, 0, sbytes, 0, source.Length);
  return sbytes;
 }

private static Byte[] ToByteArray(SByte[] source)
 {
  byte[] bytes = new byte[source.Length];
  System.Buffer.BlockCopy(source, 0, bytes, 0, source.Length);
  return bytes;
 }
 public static string GetStringFromStream(Stream stream)
 {
  // Create a stream reader.
  stream.Seek(0, SeekOrigin.Begin);
  using (StreamReader reader = new StreamReader(stream))
  {
   // Just read to the end.
   return reader.ReadToEnd();
  }
 }
}

转载于:https://www.cnblogs.com/xingquan/archive/2011/07/08/2101046.html

使用Apache NFOP创建pdf相关推荐

  1. java itext word操作_使用JAVA中的Apache POI和iText从Word(DOC)创建PDF

    docx4j包含 code,用于使用iText从docx创建PDF.它还可以使用POI将doc转换为docx. 曾经有一段时间我们平等地支持这两种方法(以及通过XHTML的PDF),但我们决定专注于X ...

  2. 使用iText库创建PDF文件

    前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...

  3. pdfbox创建pdf_PDFBox创建PDF文档

    现在让我们了解如何使用PDFBox库创建PDF文档. 创建一个空的PDF文档 可以通过实例化PDDocument类来创建一个空的PDF文档.使用这个类的Save()方法将文档保存在所需的位置. 以下是 ...

  4. 使用C# 创建PDF

    所需dll-iTextSharp.dll-在VS的右键引用 "管理NuGet程序包"中搜索添加 调用 private void button1_Click(object sende ...

  5. Java使用 PDFBox创建 PDF 文件并将文本写入其中

    在本 PDFBox 教程中,我们将了解如何使用 PDFBox 2.0 创建 PDF 文件并将文本写入其中.我们将在此过程中逐步了解. 以下是使用 PDFBox 2.0 创建文本并将其写入 PDF 文件 ...

  6. 使用 Apache PDFBox 操作PDF文件

    简介 Apache PDFBox库是一个用于处理PDF文档的开源Java工具.该项目允许创建新的PDF文档,操作现有PDF文档,并从PDF文档中提取内容.Apache PDFBox还包括几个命令行实用 ...

  7. ABBYY在MS Office中创建PDF文件的方法

    2019独角兽企业重金招聘Python工程师标准>>> ABBYY PDF Transformer+是一款可创建.编辑及将PDF文件转换为其他可编辑格式的OCR图文识别软件,不仅可以 ...

  8. python使用fpdf创建pdf文件包含:页眉、页脚并嵌入logo图片、设置使用中文字体

    python使用fpdf创建pdf文件包含:页眉.页脚并嵌入logo图片.设置使用中文字体 #python使用fpdf创建页眉.页脚并嵌入logo图片.设置使用中文字体 from fpdf impor ...

  9. python使用fpdf创建pdf并写入hello world

    python使用fpdf创建pdf并写入hello world from fpdf import FPDF # 创建pdf并写入hello world文本内容: from fpdf import FP ...

最新文章

  1. 世界上最大的超级计算机,科学网—区块链(blockchain)如何能造出世界上最大的超级计算机? - 刘进平的博文...
  2. mysql明明有索引却用不到的情况
  3. Redis、Memcache和MongoDB的区别
  4. 查看使用的那个USB口和开发板通讯
  5. VS2019-写opengl时Bugs合集(持续更新)
  6. Keras MNIST
  7. 多元回归求解 机器学习_金融领域里的机器学习算法介绍:人工神经网络
  8. linux lcd显示流程,求助 armlinux中实现lcd显示
  9. nginx+php简单配置环境
  10. Python3不需要uuid:导入/usr/local/lib/python3.x/site-packages/uuid.py提示if not 0 time_low 32L错误
  11. 灵悟礼品网上专卖店——画出项目的主要框架
  12. vue开发:前端项目模板
  13. 用html制作ps,ps制作图片的步骤
  14. QT中修改图标(图片)颜色
  15. 《最后的教父》小说人物一览表
  16. UVA815 洪水Flooded
  17. 如何配置和测试ChatBot
  18. SEO白帽必备孙子兵法之三十六计
  19. 最老程序员创业开发实训12---Android---在MVC架构下Activity设计及实现
  20. iPhone微信语音导出

热门文章

  1. android用户界面之AlarmManager教程实例汇
  2. Jumpserver0.4.0基于Centos7安装
  3. WEB前端常用JavaScript代码整理(二)
  4. 简单配置nginx使之支持pathinfo
  5. 李洪强iOS开发之Foundation框架—结构体
  6. 奔跑中的2015--成长之路
  7. 提升自己身价的四个方式
  8. struts1.3.8与hibernate3.2.5整合所遇到的问题
  9. 《如何更改其他程序ListView控件中某个Item的内容》
  10. qt 实现拖动矩形角度_Qt 绘图之图形视图框架