Creating PDF files from a .FO file

Hi,

Many time we need to createa pdf file prgramaticaly using the .fo file. if we are havinf a .fo file we can create a PDF file from that. for this code i am using the two name spaces that are

org.apache.fop and org.apache.fop.pdf;

Here the Nfop source is used, that provide the various function to create a PDF file. The Nfop is freely availbe on the net..

please refer the sample code below for the code. and the .aspx file is aatched with this code..


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 org.apache.fop;
using org.apache.fop.pdf;

public partial class GeneratePDF : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "";
lblMessage.ForeColor = System.Drawing.Color.Black;
dvGeneratePDF.Visible = true;
dvBackToHome.Visible = false;
}
protected void btnGeneratePDF_Click(object sender, EventArgs e)
{
if (fupld.HasFile)
{
string strSource = string.Empty;
string strDestination = string.Empty;
string strTimeStamp=string.Empty;

strTimeStamp=DateTime.Today.ToString("MMMM-dd-yyyy");
strSource = Server.MapPath(@"uploads\temp\") + fupld.FileName;
fupld.PostedFile.SaveAs(strSource);
strDestination = Server.MapPath(@"uploads\temp\") + fupld.FileName.Replace(".fo","") + strTimeStamp + ".pdf";

try
{
Create_PDF_from_FO(strSource, strDestination);
dvBackToHome.Visible = true;
dvGeneratePDF.Visible = false;
hlnkPathToPDfFile.Text = "View PDF";
hlnkPathToPDfFile.NavigateUrl = "~/uploads/temp/" + fupld.FileName.Replace(".fo", "") + strTimeStamp + ".pdf";
if (File.Exists(strSource))
{
File.Delete(strSource);
}
}
catch (Exception ex)
{
if (File.Exists(strSource))
{
File.Delete(strSource);
}
lblMessage.Text = ex.Message;
}
}
else
{
lblMessage.Text = "Please select any FO file..";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}

public static void Create_PDF_from_FO(string strFO, string pdfFile)
{
string fo = Fop.Net.NFop.GetFo(strFO);
byte[] bytes = Fop.Net.NFop.CreatePDF(fo);
Fop.Net.NFop.CreateFile(bytes, pdfFile);
}
}


Attachments

  • The aspx file (27541-7315-GeneratePDF.aspx.txt)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: