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 Pechkin.Synchronized;
using System.Drawing.Printing;
using Pechkin;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
                    TextBox1.Text = @"<html>
                <head>
                    <title>Test HTML-PDF file</title>
        
                    <style type=" + '"' + "text/css" + '"' + @">
                        body {
                            font-face:" + '"' + "Calibry" + '"' + "," + '"' + "Tahoma" + '"' + "," + @"sans-serif;
                        }
                        p { text-indent: 3em; }
                        .last-cell {
                            text-align: center;
                            font-style: italic;
                        }
                        code {
                            color: #484848;
                            border: 1px solid #dadada;
                            background-color: rgb(250, 250, 250);
                            padding: 5px;
                            margin: 15px; display: block;
                        }
                    </style>
                </head>
                <body>
                    <h1>Hello world!</h1>
                    <p>
                        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pulvinar pellentesque tincidunt. Duis eu massa augue. Pellentesque bibendum lacinia ipsum eget semper. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec in diam mi, vitae tincidunt leo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Quisque feugiat nibh vel dui ultrices a sagittis eros auctor. Curabitur at scelerisque tortor. Maecenas vel egestas lorem. Vivamus auctor hendrerit pharetra. Proin laoreet ornare ultricies. Nunc lectus ante, accumsan sed dictum id, vulputate at dui. Pellentesque nisl odio, tincidunt et consectetur ac, tristique ornare purus. Sed bibendum dolor vel eros auctor eleifend. Ut tortor metus, cursus sit amet tempor non, consectetur in nisl.
                    </p>
                    <table>
                        <tr><th>Property</th><th>Value</th></tr>
                        <tr><td>Text</td><td>Html to pdf test app</td></tr>
                        <tr><td>Object type</td><td>Form</td></tr>
                        <tr><td>Purpose</td><td><code>libraray test</code></td></tr>
                        <tr><td colspan=" + '"' + "2" + '"' + "class=" + '"' + "last-cell" + '"' + @">Nothing more.</td></tr>
                    </table>
                </body>
            </html>";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig().SetMargins(new Margins(180, 10, 120, 10))
            .SetDocumentTitle("Title").SetCopyCount(1).SetImageQuality(50)//.SetPaperOrientation(false)
            .SetLosslessCompression(true).SetMaxImageDpi(120).SetOutlineGeneration(true).SetOutputDpi(1200).SetPaperOrientation(true)
            .SetPaperSize(PaperKind.A4));
        
        byte[] buf = sc.Convert(new ObjectConfig(), TextBox1.Text);

        try
        {
            string fn = MapPath("~/pdf/test.pdf");//Path.GetTempFileName() + ".pdf";

            FileStream fs = new FileStream(fn, FileMode.Create);
            fs.Write(buf, 0, buf.Length);
            fs.Close();
            
            Process myProcess = new Process();
            myProcess.StartInfo.FileName = fn;
            myProcess.Start();
        }
        catch { }

    }

}
