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