Write PDF or Word Document from Byte Array To Memory Stream

Once you store a Word Document or PDF Document in your database as a BLOB you then need to convert it to a Byte Array in order for the browse to launch Word or Adobe to open the PDF. This example pulls either the Word or PDF version from a table. 

//Select either the Word or PDF version
byte[] ProposalDoc;
string ContentType = string.Empty;
string Extension = string.Empty;

if (asWord)
    ProposalDoc = row.PROPOSAL_WORD;
    ContentType = "application/word";
    Extension = ".doc";
    ProposalDoc = row.PROPOSAL_PDF;
    ContentType = "application/pdf";
    Extension = ".pdf";

MemoryStream MemStream = new MemoryStream(ProposalDoc);
CTX.Response.ContentType = ContentType;
       "attachment; filename=" + DocumentName + Extension);
//Response.AddHeader("Content-Length", oStream.Length.ToString());

CTX.Response.Buffer = true;

Save APSOSE Document to PDF or Word save to Memory Stream

The ASPOSE document object is not searializeable.  In order to save the object to the database you first have to convert it to a memory from the object (such as PDF of Word). Once you have it in a Byte Array you can updated a BLOB column in the database.

//Convert to WORD
MemoryStream outStream = new MemoryStream();
asposeDocument.Save(outStream, Aspose.Words.SaveFormat.Doc);
byte[] docBytes = outStream.ToArray();

//Convert to PDF
outStream = new MemoryStream();
asposeDocument.Save(outStream, Aspose.Words.SaveFormat.Pdf);
byte[] pdfBytes = outStream.ToArray();

ASP.NET Stop Users from Bookmarking Pages

You created an ASP.NET site with Windows Authentication. Your users like to bookmark various pages, but you do not want them to bookmark a data entry page.

This madness can be stopped by checking if the session is new, and if so redirect the user to the default page.

First create a Master Page base class and make your master pages inherit this class.  The response.buffer code is stop caching the page, but that is not related to this example of stopping users from book marking pages.  Take a look a the PageSetup() method.

The PageSetup() method will redirect the user to Default.aspx on a new session.  Also, if you do not want this to be the case during development there is a config setting that can be set to bypass this feature. 

protected override void OnLoad(EventArgs e)
    //Expire the page to stop the back and forward button usage.
    Response.Buffer = true;
    Response.ExpiresAbsolute = DateTime.Now.AddHours(-1);
    Response.Expires = 0;
    Response.CacheControl = "no-cache";


    if (!Page.IsPostBack) PageSetup();

/// Redirects user to default on new sessions. This only applies to users testing with the URL.
/// There is now security check here since it is handled outside of this site.
private void PageSetup()
    string URL = Context.Request.Url.ToString().ToLower();
    if (URL.Contains("anonymous/")) return;

    if (Context.Session != null)
        if (Session.IsNewSession)

            if (!URL.ToLower().Contains("default.aspx")) Response.Redirect("default.aspx");