When installing an MSI from a Visual Studio Web Project, a dreaded “The installer was interrupted before the Application was installed” message may appear. The Server Role, “IIS 6 Management Capability” is required for the MSI to register with the IIS 6 Metabase.
Windows 2008 (IIS7): The installer was interrupted
Posted in Uncategorized
Generate Dynamic iTextSharp PDF Documents from ASP.Net
Here’s two examples to dynamically create PDF’s using iTextSharps’ HTML Parser: in the browser and also a method to write the pdf to disk.
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Xml;
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
namespace Web
{
public partial class iTextSharpDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Example HTML String
string html = "<html><p align='center'>Hello World!</p><newpage />Hello 2nd Page!</html>";
//Parse the HTML to PDF and Write PDF to disk
WritePdf(html, @"c:\temp\share\mypdf.pdf");
//Parse the HTML to PDF and write PDF bytes to browser via the outputstream
MemoryStream m = CreatePdf(html);
Response.ContentType = "application/pdf";
Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
}
protected void WritePdf(string html, string destination)
{
MemoryStream ms = CreatePdf(html);
FileStream fs = File.OpenWrite(destination);
fs.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
fs.Close();
ms.Close();
}
public static MemoryStream CreatePdf(string html)
{
MemoryStream m = new MemoryStream();
try
{
Document document = new Document(PageSize.LETTER);
PdfWriter.GetInstance(document, m);
StringReader sr = new StringReader(html);
XmlTextReader xtr = new XmlTextReader(sr);
document.Open();
HtmlParser.Parse(document, xtr);
xtr.Close();
document.Close();
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Application", ex.Message);
throw ex;
}
return m;
}
}
}
Posted in Uncategorized
Taking an ASP.Net Application Offline with an Outage Message
App_offline.htm is an easy, but not widely published, method to take your favorite ASP.Net Web Application offline and present a friendly outage message to your users. Simply create an outage message with your downtime info formatted in clear, readable html. Copy the file to the root of your website, and any requests will be redirected to this file.
Posted in Uncategorized
iTextSharp – 401 Error when parsing images!
When using the html parser to convert a web page into PDF and also using an image tag to an IIS server that has Integrated Authentication enabled, you will receive: The remote server returned an error: (401) Unauthorized.
The easy solution is to find the GetImage(Uri url) method and add the default credentials to the WebRequest after it is created. These can be found in the itextsharp.txt.pdf.codec namespace in the following classes: BmpImage, GifImage, and PngImage.
WebRequest wr = WebRequest.Create(url);
wr.Credentials = CredentialCache.DefaultCredentials;
isp = wr.GetResponse().GetResponseStream();
Since iTextSharp is Open Source, it was easy to download the code, isolate the error, and quickly add a line of code for the authentication. Now, it’s time to submit the change to the iTextSharp project on SourceForge.
Posted in Uncategorized
iTextSharp + NewPage -> “The document has no pages”
The constructor for an iTextSharp Pdf document doesn’t require the page size to be initialized and it does not default to a lovely letter sized page, therefore, attempting to add a new page to the document may result in an exception of “The document has no pages.”
When creating a document, always specify the page size such as:
Document document = new Document(PageSize.LETTER);
Using the HTML parser to generate a pagebreak in the Pdf file requires the simple tag:
<newpage />
Unfortunately, the HTML pagebreak, <p style=”page-break-before: always” />, isn’t supported, but it should be fairly easy to add this functionality to the parser if needed.
Posted in Uncategorized
MS SQL Insert Statement Generator
Narayana Vyas Kondreddi has created a wonderful stored procedure for MS SQL Server that will easily, quickly, and accurately create your SQL insert scripts for your next migration. He has two versions (SQL 2000 and 2005) of the sp_generate_inserts stored procedure along with a host of other neat T-SQL code scripts.
Posted in Uncategorized
MSBuild and Quotation Marks “!!”
To publish a web site using Team Build, I added this task:
<Exec Command = “xcopy /s /y /e $(OutDir)_PublishedWebsites\webproject \\webserver\webdocs\website“/>
Posted in Uncategorized
Extension Methods and Linq to SQL
Using Extension Methods with Linq to SQL is easy and handy to use! To change the insert behavior on the Linq to SQL object without having to use a stored procedure and create a business object around the l2s object, create an Extension Method. This example only insert users that haven’t already been added. Now, ToolUserInfo.Insert() is ready to be called.
public static void Insert(this ToolUserInfo userInfo)
{
//Create a DB Context
DataClassDataContext db = new DataClassDataContext();// Get a list of existing tool users
var ToolUsers =
(from u in db.R2ToolUserInfos
select u.UserId);if (!ToolUsers.Contains(userInfo.UserId))
{
db.ToolUserInfos.InsertOnSubmit(userInfo);
db.SubmitChanges();
}
}
Posted in Uncategorized
ProperCase Function in .Net
There’s no need to roll your own proper case function in .Net! The ToTitleCase Method in the TextInfo class will format your name to the customs of the current culture. In general, only the first letter is uppercased and the remainder is lowercased. Also, it will not format a word if all the characters are uppercased (to not interfere with abbreviations). Our names were all captialized so I added ToLower, however that will cause any suffixes such as “III” to be formated as “Iii”.
public static string ProperCase(string input){return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(input.ToLower());}
Posted in Uncategorized
Pismo File Mount Audit Package (pfmap) – Best ISO Mount!
While the most common search terms don’t return this software at the top of the list, this is simply the best ISO file mounter available today (and free!). Sorry MagicISO but, pfmap lets me right click on the ISO and either mount it as a drive letter or (my favorite) perform a quick mount that makes the image appear as part of your current directory structure.
Head over to Pismo Techic, and try out pfmap.
Posted in Uncategorized