Asp.Net aspxページをPDFファイルに置き換える
5693 ワード
protected void Page_Load(object sender, EventArgs e)
{
string procinstid = "";
string host = "";
string url = "";
procinstid = Request.QueryString["procinstid"].ToString();
host = Request.QueryString["host"].ToString();
url = "http://" + host + "/PluginsFor/PrintPage/EngineeringChangeApplication/EngineeringChangeApplication.aspx?step=view&procinstid=" + procinstid;
//Get the Url
if (url.Trim() == string.Empty)
{
Logger.Log.Info("------URL ");
}
//
string outputFileName = "";
string path = ConfigurationManager.AppSettings["UpLoadFilePath"];
DateTime now = DateTime.Now;
string time = now.Year.ToString() + now.Month.ToString() + now.Day.ToString() + now.Hour.ToString() + now.Minute.ToString() + now.Second.ToString() + now.Millisecond.ToString();
string fileName = " " + time + ".pdf";
string newfileName = path + fileName; //
////
outputFileName = System.IO.Path.Combine(path, fileName);
EO.Pdf.HtmlToPdf.Options.HeaderHtmlFormat = " ";
EO.Pdf.HtmlToPdf.Options.FooterHtmlFormat = " ";
EO.Pdf.HtmlToPdf.Options.PageSize = EO.Pdf.PdfPageSizes.A5;
EO.Pdf.HtmlToPdf.Options.NoLink = false;
HtmlToPdf.ConvertUrl(url, outputFileName);
PdfDocument doc = new PdfDocument(outputFileName);
doc.Security.Disallow(PdfDocumentPermissions.Printing);
AcmRender render = new AcmRender(doc);
render.BeforeRenderPage += new AcmPageEventHandler(BeforeRenderPage);
AcmContent content = new AcmContent();
render.Render(content);
doc.Save(outputFileName);
Response.Redirect("/PluginsFor/PrintPage/DownLoadPDF/DownLoadPDF.aspx?filename=" + System.Web.HttpUtility.UrlEncode(newfileName.ToString()));//
Context.Response.Write("{success:'true',fileName:'" + newfileName.ToString() + "'}");
}
private void BeforeRenderPage(object sender, AcmPageEventArgs e)
{
EO.Pdf.Contents.PdfTextLayer textLayer = new EO.Pdf.Contents.PdfTextLayer();
textLayer.Font = new EO.Pdf.Drawing.PdfFont("Arial", 30);
textLayer.NonStrokingColor = Color.FromArgb(1, 150, 150, 150);
textLayer.GfxMatrix.Rotate(45);
EO.Pdf.Contents.PdfTextContent textContent = new EO.Pdf.Contents.PdfTextContent(" ".ToUpper());
textContent.PositionMode = EO.Pdf.Contents.PdfTextPositionMode.Offset;
textContent.Offset = new EO.Pdf.Drawing.PdfPoint(400, 150);
textLayer.Contents.Add(textContent);
e.Page.Contents.Add(textLayer);
}
public partial class DownLoadPDF : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
EmployeeEntity empEnt = null;
Object user = HttpContext.Current.Session["LoginUser"];
//if (user == null)
// return;
//empEnt = (EmployeeEntity)user;
try
{
string filePath = Request.QueryString["fileName"].ToString();
//string filePath = "C:\\Users\\Administrator\\Desktop\\word\\ 20141227233018.docx";
string fileName = string.Empty;
//string filePath = string.Empty;
//filePath = ConfigurationManager.AppSettings[id];
fileName = filePath.Substring(filePath.LastIndexOf(@"/") + 1);
//filePath = Server.MapPath(filePath);
FileStream steam = File.Open(filePath, FileMode.Open, FileAccess.Read);
byte[] data = new byte[steam.Length];
int result = steam.Read(data, 0, data.Length);
if (!File.Exists(filePath))
Response.Redirect("");
if (data.Length <= 0)
data = new Byte[] { 13, 10 };
int i = filePath.LastIndexOf('/');
steam.Close();
string UserAgent = Request.ServerVariables["http_user_agent"].ToLower();
if (UserAgent.IndexOf("firefox") == -1)
{//
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(fileName));
}
else
{
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
}
HttpContext.Current.Response.AddHeader("Content-Length", data.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(data);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex)
{
Logger.Log.Error(ex.Message);
Response.Redirect("/Web/Page/Download/FileNotFound.htm");
}
}
}
転載先:https://www.cnblogs.com/hsdt/p/6439000.html