Tuesday, October 12, 2010

How to print long page with multiple pages as PDF

using System.Windows.Forms;
using iTextSharp.text.pdf;
using iTextSharp.text;
 
namespace ActiveTest
{
    public partial class Test : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string mode = this.Request.QueryString.Get("mode");
            string page = this.Request.QueryString.Get("page");
            if (!string.IsNullOrEmpty(mode) && mode.Equals("Print"))
            {
                int p = int.Parse(page);
                ///
                /// bind relevent page of the gridview
                /// 
                Response.Write("This is page " + page);
            }
        }
        protected void PrintPDF(object sender, EventArgs e)
        {
            int listId = 12345;
            ///
            /// A4 Size:
            ///
            int width = 595;
            int height = 792;
            int leftMargin = 20;
            int rightMargin = 20;
 
            iTextSharp.text.Rectangle pgSize = new iTextSharp.text.Rectangle(width, height);
            Document doc = new Document(pgSize, leftMargin, rightMargin, 48, 24);
            string f = Server.MapPath(string.Format("~/Temp/{0}.pdf", listId));
            PdfWriter.GetInstance(doc, new FileStream(f, FileMode.Create));
            doc.Open();
 
            int rowCount = this.GetRowCount();
            int pageSize = 30;
            for (int j = 0; j < (rowCount / pageSize) + 1; j++)
            {
                string url = this.Request.Url.ToString() + "?id=" + listId + "&mode=Print&page=" + j;
                Image thumbnail = new Image(url, width, height, width, height, Image.ImageMethod.Url);
                using (Bitmap image = thumbnail.GenerateImage())
                {
                    iTextSharp.text.Image i = iTextSharp.text.Image.GetInstance(image, 
                                                        System.Drawing.Imaging.ImageFormat.Bmp);
                    doc.Add(i);
                    doc.NewPage();
                }
            }
            doc.Close();
            ///
            /// Use pdf file 
            ///
            File.Delete(f);            
        }
        protected int GetRowCount()
        {
            ///
            /// Get from database.
            ///
            return 130;
        }
    }
    public class Image
    {
        public enum ImageMethod { Url, Html };
        public string Url { getset; }
        public Bitmap Current { getset; }
        public int Width { getset; }
        public int Height { getset; }
        public int BrowserWidth { getset; }
        public int BrowserHeight { getset; }
        public string Html { getset; }
        public ImageMethod Method { getset; }
 
        public Image(string data, int browserWidth, int browserHeight, int width,
                                        int height, ImageMethod method)
        {
            this.Method = method;
            if (method == ImageMethod.Url)
                this.Url = data;
            else if (method == ImageMethod.Html)
                this.Html = data;
            this.BrowserWidth = browserWidth;
            this.BrowserHeight = browserHeight;
            this.Height = height;
            this.Width = width;
        }
        public Bitmap GenerateImage()
        {
            Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return Current;
        }
        private void GenerateThumbnailInteral()
        {
            WebBrowser webBrowser = new WebBrowser();
            webBrowser.ScrollBarsEnabled = false;
            if (this.Method == ImageMethod.Url)
                webBrowser.Navigate(this.Url);
            else webBrowser.DocumentText = this.Html;
            webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
            while (webBrowser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
            webBrowser.Dispose();
        }
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser webBrowser = (WebBrowser)sender;
            webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
            webBrowser.ScrollBarsEnabled = false;
            this.Current = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
            Graphics g = Graphics.FromImage((System.Drawing.Image)this.Current);
            g.InterpolationMode = InterpolationMode.High;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            webBrowser.BringToFront();
            webBrowser.DrawToBitmap(Current, webBrowser.Bounds);
            this.Current = (Bitmap)this.Current.GetThumbnailImage(Width, Height, nullIntPtr.Zero);
            g.Dispose();
        }
    }
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...