Thursday, March 28, 2013

Dynamic Word Document Creation,view as thumbnail and Download in asp.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;

namespace AccordionMenu
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        //http://wfjanjua.blogspot.in/2011/09/read-ms-word-document-edit-it-and.html
        //http://cathalscorner.blogspot.in/2009/08/docx-v1007-released.html
        //http://docx.codeplex.com/releases/view/31554
        //http://richtexteditor.com/

        //<script type="text/javascript">
        // function createTextSnippet() {
        //     //example as before, replace YOUR_TEXTAREA_ID
        //     var html=CKEDITOR.instances.YOUR_TEXTAREA_ID.getSnapshot();
        //     var dom=document.createElement("DIV");
        //     dom.innerHTML=html;
        //     var plain_text=(dom.textContent || dom.innerText);
        //     //create and set a 128 char snippet to the hidden form field
        //     var snippet=plain_text.substr(0,127);
        //     document.getElementById("hidden_snippet").value=snippet;
        //     //return true, ok to submit the form
        //     return true;
        // }
        //</script>

        protected void Page_Load(object sender, EventArgs e)
        {

        }
      
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (fupDoc.HasFile == true)
            {
                if (System.IO.Path.GetExtension(fupDoc.FileName) == ".doc")
                {
                    fupDoc.SaveAs(Server.MapPath("~\\docs\\") +fupDoc.FileName);
                    this.lblupload.Text = "Doc successfully uploaded";
                    ViewState["doc"] = fupDoc.FileName;
                    //DocX doc = DocX.Load(Server.MapPath("~\\docs\\") + fupDoc.FileName);     
                    ////Created a Docx variable doc and using Docx.Load("word file") to load the word 2007 file                        
                    //txtWord.Text = doc.Text;
                    ////Set the text property of doc to textbox text property. Text property of doc returns all the text from the word document.              
               }
               else
               {
                    Response.Write("");        
               }
            }
            else
            {
                   Response.Write("");        
            }

            if (fupImage.HasFile == true)
            {
                if (System.IO.Path.GetExtension(fupImage.FileName) == ".jpg")
                {
                    fupImage.SaveAs(Server.MapPath("~\\images\\") + fupImage.FileName);
                    this.lblupload.Text = "Image successfully uploaded";                               
                }
                else
                {
                    Response.Write("");
                }
            }
            else
            {
                Response.Write("");
            }
         }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                FileInfo file = new FileInfo(MapPath("~\\docs\\") + ViewState["doc"].ToString());
                if (file.Exists)
                {
                    File.Delete(MapPath("~\\docs\\") + ViewState["doc"].ToString());
                    // Delete existing word file
                }
                //DocX doc = DocX.Create(Server.MapPath("~\\CVs\\") + ViewState["doc"].ToString();
                //// Create a new word document          
                //Formatting f = new Formatting();
                //f.FontFamily = new FontFamily("Tahoma");
                ////Defines font family
                //doc.InsertParagraph(txtWord.Text,false,f);   
                //doc.Save();  
       
               
            }
            catch (Exception)
            {

            }
        }

        protected void btnDownload_Click(object sender, EventArgs e)
        {
            string data = txtWord.Text.ToString();
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            Byte[] bytes = encoding.GetBytes(data);
            data = System.Text.ASCIIEncoding.ASCII.GetString(bytes);

            byte[] stringdata = System.Text.Encoding.UTF8.GetBytes(txtWord.Text);
            string result = System.Text.Encoding.UTF8.GetString(stringdata);

           
            Color FontColor = Color.Black;
            Color BackColor = Color.White;
            string FontName = "Times New Roman";
            int FontSize = 12;
            int Height = 40;
            int Width = 200;          

            MemoryStream ms = new MemoryStream();
            Bitmap objBitmap = new Bitmap(Width, Height);
            Graphics objGraphics = Graphics.FromImage(objBitmap);
            Font objFont = new Font(FontName, FontSize);
            PointF objPoint = new PointF(5f, 5f);
            SolidBrush objBrushForeColor = new SolidBrush(FontColor);
            SolidBrush objBrushBackColor = new SolidBrush(BackColor);
            objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height);
            objGraphics.DrawString(result, objFont, objBrushForeColor, objPoint);
            objBitmap.Save(ms, ImageFormat.Jpeg);

         
            objBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] bmpBytes = ms.GetBuffer();
            objBitmap.Dispose();
            ms.Close();

            //Bitmap bit1 = new Bitmap(200, 30, System.Drawing.Imaging.PixelFormat.Format64bppArgb);
            //Graphics gph1 = Graphics.FromImage(bit1);
            //string text;
            //gph1.Clear(Color.Orange);
            //text = result;
            //gph1.DrawString(text, new Font("Arial", 12, FontStyle.Bold),
            //new SolidBrush(Color.Red), new PointF(0.4F, 2.4F));
            //Response.ContentType = "image/Jpeg";          
            //bit1.Save(Response.OutputStream, ImageFormat.Jpeg);
            //bit1.Dispose();

            Response.Clear();
            Response.AppendHeader("content-disposition", "attachment; filename=test.doc");
            Response.ContentType = "application/msword";
            Response.BinaryWrite(stringdata);
            Response.Flush();
            Response.End();

            Response.Clear();
            Response.AppendHeader("content-disposition", "attachment; filename=test.jpg");
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(bmpBytes);
            Response.Flush();
            Response.End();
        }
    }
}

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...