Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Displaying & Submitting "Interactive PDF Forms".Displaying & Submitting "Interactive PDF Forms".
Previous
 
Next
New Post
11/30/2007 12:22 PM
 

Displaying & Submitting "Interactive PDF Forms".

1) User browses to page on website.
2) Page displays an interactive pdf form
3) User fills out the fields in the form
4) User click the "submit" button, the interactive pdf form is flattened and saved to a folder on the webserver. Or, the flattened pdf is emailed to other users.

I can easily create the interactive pdf using Adobe Professional, but how can I display the pdf and capture the "submitted" flattened pdf?

Thanks!!

 
New Post
11/30/2007 3:49 PM
 

Quite a fun little project.  Let me tell you how we did it.  We purchased DynamicPDF so we could use their merging tools, then we created the PDF and told it to submit to a page, we'll call it PDFPoster.aspx.  That page exists within our DNN instance, so http://someplace/DesktopModules/SomePDFThinger/PDFPoster.aspx and it tries to read XFDF data coming in from the PDF post.  It parses the XFDF, finds the original, performs an "in-memory" merge of the PDF, and then emails the result to one or more people.  Sound like fun?

I apologize for the poorly formatted code below (straight copy from my source file), but enjoy!

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.IO;
using System.Xml;
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 DotNetNuke.Entities.Users;
using DotNetNuke.Entities.Portals;

using ceTe.DynamicPDF;
using ceTe.DynamicPDF.Merger;
using ceTe.DynamicPDF.PageElements;

namespace Macu.Modules.PDF
{
 public partial class Poster : System.Web.UI.Page
 {
  protected void Page_Load(object sender, EventArgs e)
  {
   UserInfo   user   = UserController.GetCurrentUserInfo();
   PortalSettings  portal   = PortalController.GetCurrentPortalSettings();

   string    tmpStoreLoc  = @"~\Portals\" + portal.PortalId.ToString() + @"\PDFTemp\";
   string    physicalPath = string.Empty;

   byte[]    incomingData;
   string    mailTo   = string.Empty;
   string    mailSubject  = string.Empty;
   string    xfdfFilePath = string.Empty;
   string    pdfFilePathCopy = string.Empty;
   string    pdfFilePath  = string.Empty;
   string    xfdfData  = string.Empty;
   string    originalPath = string.Empty;

   XmlDocument   xfdfDoc;
   XmlNamespaceManager xmlNSMan;
   XmlNode    xmlMailTo;
   XmlNode    xmlMailSubject;
   XmlNode    xmlPdfFilePath;

   MergeOptions  options;
   MergeDocument  document;

   // no form data
   if (Request.ContentLength == 0)
   {
    SetStatusMessage("No form submitted.", false);
    return;
   }

   // find physical path to the portal storage location, check that it exists...
   physicalPath = Server.MapPath(tmpStoreLoc);
   if (!System.IO.Directory.Exists(physicalPath))
    System.IO.Directory.CreateDirectory(physicalPath);

   incomingData = Request.BinaryRead(Request.ContentLength);
   xfdfData = System.Text.ASCIIEncoding.ASCII.GetString(incomingData);
   xfdfData = xfdfData.Replace("\n","").Replace("\r","");
   xfdfDoc = new XmlDocument();

   // try to load the XFDF (xml) data
   try
   {
    xfdfDoc.LoadXml(xfdfData);
   }
   catch (Exception ex)
   {
    Macu.ErrorHandling.ErrorLogic.SendErrorEmail("MACU PDF Forms (DNN)", "jseeley@macu.org", ex, xfdfData);
    SetStatusMessage("Form data included invalid character(s).", false);
    return;
   }

   xmlNSMan = new XmlNamespaceManager(xfdfDoc.NameTable);
   xmlNSMan.AddNamespace("adobe", "http://ns.adobe.com/xfdf/");

   xmlMailTo = xfdfDoc.SelectSingleNode("//adobe:xfdf/adobe:fields/adobe:field[@name='mailTo']/adobe:value", xmlNSMan);
   xmlMailSubject = xfdfDoc.SelectSingleNode("//adobe:xfdf/adobe:fields/adobe:field[@name='mailSubject']/adobe:value", xmlNSMan);

   // get the PDF file path
   try
   {
    xmlPdfFilePath = xfdfDoc.SelectSingleNode("//adobe:xfdf/adobe:f", xmlNSMan);
    pdfFilePath = xmlPdfFilePath.Attributes[0].InnerText;//.Replace("file:///", "").Replace("|", ":");
    originalPath = pdfFilePath;

    // need to know if this is a file ticket, straight http, or a file path
    if (pdfFilePath.ToLower().StartsWith("file"))
    {
     pdfFilePath = pdfFilePath.ToLower().Replace("|",":").Replace("file:///","");
     pdfFilePath = pdfFilePath.Replace("y:", @"\\d83hqdc2\dfs");
    }
    else if (pdfFilePath.ToLower().IndexOf("fileticket") > -1)
    {
     string queryString = pdfFilePath.Substring(pdfFilePath.IndexOf(".aspx?")+5);
 
     string fileTicket = Macu.Web.WebHelper.GetQueryString(queryString, "fileticket", "");
     string tabId = Macu.Web.WebHelper.GetQueryString(queryString, "tabid", "");
     string moduleId = Macu.Web.WebHelper.GetQueryString(queryString, "mid", "");

     pdfFilePath = DotNetNuke.Common.Utilities.UrlUtils.DecryptParameter(fileTicket);
     pdfFilePath = ReplaceHTTPLink(pdfFilePath);
    }
    else
    {
     pdfFilePath = ReplaceHTTPLink(pdfFilePath);
     //int indexOfPortal = pdfFilePath.ToLower().IndexOf("portals");
     //string latePath = pdfFilePath.Substring(indexOfPortal + 9);
     //pdfFilePath = @"\\d83hqdc2\dfs\" + latePath;
     //pdfFilePath = pdfFilePath.Replace("%20", " ");
    }

    //if (pdfFilePath.ToLower().StartsWith("y:"))
    //    pdfFilePath = pdfFilePath.ToLower().Replace("y:", @"\\d83hqdc2\dfs");
    //if (pdfFilePath.ToLower().StartsWith("http"))
    //{
    //    int indexOfPortal = pdfFilePath.ToLower().IndexOf("portals");
    //    string latePath = pdfFilePath.Substring(indexOfPortal+9);
    //    pdfFilePath = @"\\d83hqdc2\dfs\" + latePath;//Server.MapPath(pdfFilePath);//pdfFilePath.Replace("Y:", @"\\d83hqdc2\dfs");
    //}

    int indexOfPdf = pdfFilePath.IndexOf(".pdf");

    if (indexOfPdf > -1)
     pdfFilePathCopy = physicalPath + "pdf_" + user.UserID.ToString() + "_" + DateTime.Now.Millisecond.ToString() + ".pdf";

    // create a mergeoptions object to import the form
    options = new MergeOptions(true);

    // create a document and set it's properties
    document = new MergeDocument(pdfFilePath, options);
    document.Creator = "XFDF Poster";
    document.Author = "XFDF Poster";
   }
   catch (Exception ex)
   {
    Macu.ErrorHandling.ErrorLogic.SendErrorEmail("MACU PDF Forms (DNN)", "someemail", ex, "Original Path: " + originalPath + "<br />Path: " + pdfFilePath);
    SetStatusMessage("Error loading source PDF.", false);
    return;
   }

   // get the mail to and email subject
   try
   {
    mailTo = xmlMailTo.InnerText;
    mailSubject = xmlMailSubject.InnerText;
   }
   catch { }

   if (mailTo == string.Empty)
   {
    SetStatusMessage("Form missing mailTo variable.", false);
    return;
   }

   if (mailSubject == string.Empty)
   {
    SetStatusMessage("Form missing mailSubject variable.", false);
    return;
   }

   // get file path, try to output
   xfdfFilePath = physicalPath + DateTime.Now.ToString("yyyyMMddHHmmssff") + ".xfdf";

   try
   {
    using (StreamWriter writer = new StreamWriter(xfdfFilePath))
    {
     writer.Write(xfdfData);
     writer.Flush();
     writer.Close();
    }
   }
   catch
   {
    SetStatusMessage("Error writing file.", false);
    return;
   }

   // now try to generate (merge) the PDF document
   if (document != null)
   {
    // get all the fields
    XmlNodeList list = xfdfDoc.SelectNodes("//adobe:xfdf/adobe:fields/adobe:field", xmlNSMan);
    foreach (XmlNode node in list)
    {
     try
     {
      // fill the field value with the one from XFDF
      document.Form.Fields[node.Attributes[0].InnerText].Value = node.InnerText;
     }
     catch (Exception ex)
     {
      System.Diagnostics.EventLog.WriteEntry("MACU.PDF", "Cannot merge field: " + node.Attributes[0].InnerText + "\nValue: " + node.InnerText + "\n\nError: " + ex.Message);
     }
    }

    // save new pdf out
    document.Draw(pdfFilePathCopy);
   }

   try
   {
    System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
    client.Host = "mail.macu.org";

    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    message.Subject = mailSubject;
    message.Body = string.Empty;

    foreach (string address in mailTo.Split(new char[] { ';', ',' }))
    {
     if (address.Trim().Length > 0)
      message.To.Add(address);
    }

    if (user != null && user.UserID > 0)// && user.Email.Trim().Length > 0 && !user.IsSuperUser)
    {
     message.CC.Add(user.Email);
     message.From = new System.Net.Mail.MailAddress(user.Email);
    }
    else
     message.From = new System.Net.Mail.MailAddress("someaddress@somehwher.com");

    System.Net.Mail.Attachment attch2 = new System.Net.Mail.Attachment(pdfFilePathCopy);
    message.Attachments.Add(attch2);

    client.Send(message);
    attch2.Dispose();

    SetStatusMessage("Form sent successfully.", true);
   }
   catch (Exception ex)
   {
    string userStr = user != null ? user.Username + "/" + user.Email : "N/A";
    Macu.ErrorHandling.ErrorLogic.SendErrorEmail("MACU PDF Forms (DNN)", "somebody@somewhere.com", ex, "Address(es): " + mailTo + "<br><br>Path: " + pdfFilePath +
     "User: " + userStr);
    SetStatusMessage("Form could not be sent.", false);
   }
   finally
   {
    // try to delete the pdf copy and the xfdf
    try
    {
     if (File.Exists(pdfFilePathCopy))
      File.Delete(pdfFilePathCopy);

     if (File.Exists(xfdfFilePath))
      File.Delete(xfdfFilePath);
    }
    catch { }
   }
  }

  private void SetStatusMessage(string message, bool isSuccess)
  {
   imgStatus.ImageUrl = isSuccess ? "~/DesktopModules/MACU.PDF/Images/success_48.png" : "~/DesktopModules/MACU.PDF/Images/warning.gif";
   lblMessage.Text = message;
   lblMessage.CssClass = isSuccess ? "macu_success" : "macu_error";
  }

  private string ReplaceHTTPLink(string input)
  {
   string output = string.Empty;

   int indexOfPortal = input.ToLower().IndexOf("portals");
   string latePath = input.Substring(indexOfPortal + 9);
   output = @"\\d83hqdc2\dfs\" + latePath;
   output = output.Replace("%20", " ");

   return output;
  }
 }
}


-- Jon Seeley
DotNetNuke Modules
Custom DotNetNuke and .NET Development
http://www.seeleyware.com
 
New Post
11/30/2007 6:11 PM
 

Thanks for the code!!

Question.... In your interactive pdf form did the "submit" button just post back to PDFPoster.aspx?

I'm guessing I could also use the iTextSharp instead of the DynamicPDF.

Thanks!

Kyle

 
New Post
12/1/2007 4:10 PM
 

Yeah, no problem.  The submit button did just that (posted to the PDFPoster.aspx);

Any one of the PDF libraries out there would be fine so long as they have the "merge" capability.  DynamicPDF just happened to be one we were already subscribed to so I used that one.


-- Jon Seeley
DotNetNuke Modules
Custom DotNetNuke and .NET Development
http://www.seeleyware.com
 
New Post
3/24/2016 11:36 PM
 
Jon / Kyle,

Once you compiled code, did you create a DDN install file and dropped the module on a page?
 
Previous
 
Next
HomeHomeArchived Discus...Archived Discus...Developing Under Previous Versions of .NETDeveloping Under Previous Versions of .NETASP.Net 2.0ASP.Net 2.0Displaying & Submitting "Interactive PDF Forms".Displaying & Submitting "Interactive PDF Forms".


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out