using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ABCpdf = WebSupergoo.ABCpdf7;
using WebSupergoo.ABCpdf7;
using System.Net;
using System.IO;
using System.Text;
using System.Configuration;
using System.Collections;
public partial class Headerfooter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ABCpdf.Doc theDoc = new Doc();
//BELOW CODE IS USED TO SET COLOR OF DOCUMENT
//theDoc.Color.String = "255 0 0";
ABCpdf.Doc theDoc1 = new Doc();
ABCpdf.Doc theDoc2 = new Doc();
ABCpdf.Doc theDoc3 = new Doc();
ABCpdf.Doc theDoc4 = new Doc();
int theID = 0, thID1=0, theCount = 0, i = 0;
//Header html
string theText1 = string.Empty;
string theText2 = string.Empty;
string theText3 = string.Empty;
theText1 = this.GetHTML(GetAbsoluteURI("~/headerwithhtml.htm", "", false), "TDR");
theText2 = this.GetHTML(GetAbsoluteURI("~/test2.htm", "", false), "TDR");
theText3 = this.GetHTML(GetAbsoluteURI("~/SampleDocument.htm", "", false), "TDR");
//Header logo
XImage theImg = new XImage();
theImg.SetFile(Server.MapPath("../HTMLToPDF/logo.jpg"));
/*Start Content*/
string test = "Add Text to First Document";
int theFont1 = theDoc1.EmbedFont("Comic Sans MS", "Latin", false, true);
int theFont2 = theDoc1.EmbedFont("Comic Sans MS", "Latin", false, true);
test = "" + test + "";
test = test.Replace("", "");
test = test.Replace("", "");
string teststring = "Add Text to Second Document";
int theFont3 = theDoc2.EmbedFont("Comic Sans MS", "Latin", false, true);
int theFont4 = theDoc2.EmbedFont("Comic Sans MS", "Latin", false, true);
teststring = "" + teststring + "";
teststring = teststring.Replace("", "");
teststring = teststring.Replace("", "");
theDoc.Rect.String = "50 50 560 680";
theID = theDoc.AddHtml(theText2);
while (theDoc.Chainable(theID))
{
// theDoc.Page = theDoc.AddPage();
theID = theDoc.AddHtml("", theID);
}
//XReadOptions xr = new XReadOptions();
//xr.ReadModule = ReadModuleType.OpenOffice;
theDoc1.Rect.String = "50 50 560 680";
//theDoc1.FontSize = 8;
thID1 = theDoc1.AddHtml(theText3);
/*
* Loop till there are more pages in the output
*/
while(theDoc1.Chainable(thID1))
{
theDoc1.Page = theDoc1.AddPage();
thID1 = theDoc1.AddHtml("", thID1);
}
//theDoc2.Read("test5.rtf", xr);
//theDoc3.Read("test4.rtf", xr);
//theDoc4.Read("test5.rtf", xr);
//theDoc1.Pos.X = 100;
//theDoc1.Pos.Y = 20;
// theDoc1.Rect.Position(70, -1000);
//theDoc1.AddHtml(test);
//theDoc2.Pos.X = 200;
//theDoc2.Pos.Y = 100;
//theDoc2.AddHtml(teststring);
//theDoc.Append(theDoc1);
//theDoc.Append(theDoc2);
//theDoc.Append(theDoc3);
theDoc.Append(theDoc1);
theCount = theDoc.PageCount;
/*End Content*/
/*Start header*/
theDoc.HPos = 0.5;
theDoc.VPos = 0.5;
for (i = 1; i <= theCount; i++)
{
theDoc.PageNumber = i;
//this loop set header on odd pages
if (theDoc.PageNumber % 2 != 0)
{
theDoc.Rect.Left = 60;
theDoc.Rect.Right = 250;
theDoc.Rect.Top = 10;
theDoc.Rect.Bottom = 725;
theDoc.Rect.Width = 60;
theDoc.Rect.Height = 60;
theDoc.AddImageObject(theImg, false);
theID = theDoc.AddImageObject(theImg, false);
while (theDoc.Chainable(theID))
{
theID = theDoc.AddImageObject(theImg, false);
}
theDoc.Rect.String = "90 780 560 670";
theDoc.FontSize = 8;
theID = theDoc.AddHtml(theText1);
/*
* Loop till there are more pages in the output
*/
while (theDoc.Chainable(theID))
{
theID = theDoc.AddHtml("", theID);
}
theDoc.Rect.String = "30 -40 106 100";
theDoc.FontSize = 8;
theDoc.AddText("Confidential to Ebix");
}
else
{
}
}
/*end header*/
/*Start footer "100 50 500 150";*/
theDoc.Rect.String = "570 -40 300 100";
theDoc.HPos = 1.0;
theDoc.VPos = 0.5;
theDoc.FontSize = 8;
for (i = 1; i <= theCount; i++)
{
theDoc.PageNumber = i;
theDoc.AddText("Page " + i.ToString() + " of " + theCount.ToString());
//theDoc.FrameRect();
}
/*end footer*/
/*Save file as PDF*/
string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "Headerfooter.pdf";
if (File.Exists(path))
{
File.Delete(path);
theDoc.Save(Server.MapPath("headerfooter.pdf"));
theDoc.Clear();
}
else
{
theDoc.Save(Server.MapPath("headerfooter.pdf"));
theDoc.Clear();
}
}
}
private string GetHTML(string url, string resourceName)
{
try
{
StringBuilder content = new StringBuilder();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StringWriter writer = new StringWriter(content))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
{
writer.Write(reader.ReadToEnd());
reader.Close();
}
writer.Close();
}
return content.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
public string GetAbsoluteURI(string virtualPath, string queryString, bool useSecure)
{
try
{
string path = VirtualPathUtility.ToAbsolute(virtualPath);
Uri newUri = new Uri(HttpContext.Current.Request.Url, path);
UriBuilder uriBuilder = new UriBuilder(newUri);
if (!string.IsNullOrEmpty(queryString))
{
if (queryString.StartsWith("?")) queryString = queryString.Substring(1);
uriBuilder.Query = queryString;
}
if (!useSecure)
{
uriBuilder.Scheme = "http";
if (uriBuilder.Port == 443) uriBuilder.Port = 80;
}
return uriBuilder.Uri.ToString();
}
catch
{
return string.Empty;
}
}
}
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment