回复 13楼AvoCaDolol的帖子
前端源码:
<%@ Page Language="C#" AutoEventWireup="true" Inherits="DocumentCleanDemo" CodeBehind="DocumentCleanDemo.aspx.cs" %>
<%@ Register Assembly="Leadtools.Web" Namespace="Leadtools.Web.Controls" TagPrefix="ltwf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="robots" content="index, follow" />
<title>Document Clean Demo</title>
<meta name="description" content="" />
<meta name="keywords" content="" />
<link href="Resources/css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="Resources/DocumentCleanButtons.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="container">
<center>
<table cellpadding="0" cellspacing="0" border="0" style="border-color: White; width: 1200px">
<tr>
<td style="border: 0; padding-top: 20px; padding-bottom: 10px; font-size: large;">
<asp:FileUpload ID="FileUpload1" runat="server" Width="500px" Height="25px" />&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />&nbsp;&nbsp;
<aspabel ID="LabMessage1" runat="server" ForeColor="red"/>
</td>
<td></td>
</tr>
<tr>
<td valign="top" style="border: 0">
<ltwf:WebThumbnailViewer ID="WebThumbnailViewer1" runat="server" BorderStyle="Solid"
BorderWidth="1px" Height="160px" Width="800px" ViewerID="WebImageViewer1" VerticalAlignMode="Center"
ThumbStyle-HoverBackColor="#D0D0D0" SelectedThumbStyle-BackColor="DarkRed" SelectedThumbStyle-HoverBackColor="Red"
BackColor="White" />
</td>
<td align="center" valign="top">
<div class="toolbar" style="width:400px; height:160px; padding-top:0; text-align:center; overflow-x:auto;">
<aspataList ID="DataList1" runat="server" EnableViewState="true" Height="100%" Width="400px" RepeatDirection="Horizontal"
onitemcommand="DataList1_ItemCommand">
<ItemTemplate>
<div style=" margin-right:10px;" onmouseover="c=this.style.backgroundColor;this.style.backgroundColor='#FFFFFF';" onmouseout="this.style.backgroundColor=c;">
<asp:ImageButton ID="img_file" CommandName="load_file" runat="server" ImageUrl="/resources/document.png" Width="80px" /><br />
<asp:CheckBox ID="cb_file" runat="server" /><aspabel ID="lab_file" runat="server" Text='<%# Eval("file_name") %>'></aspabel>
</div>
</ItemTemplate>
</aspataList>
</div>
</td>
</tr>
<tr>
<td style="border: 0;">
<div id="infoBar" class="infoBar" style="width: 800px; background-color: Gray; border: 0;">
<div id="zoom" class="section" style="border: 0; width:90%;">
<a id="btnZoomIn" title="Zoom In" style="cursor: pointer"></a>
<a id="btnZoomOut" title="Zoom Out" style="cursor: pointer"></a>
<a id="btnFit" title="Fit" style="cursor: pointer"></a>
<a id="btnFitWidth" title="Fit Width" style="cursor: pointer"></a>
<a id="btnView1to1" title="Full View" style="cursor: pointer"></a>
<a id="btnPan" title="change the mouse pointer to Pan" style="cursor: pointer;"></a>
<a id="btnRotateLeft90" title="Rotate Left 90 degrees" style="cursor: pointer;"></a>
<a id="btnRotateRight" title="Rotate Right 90 degrees" style="cursor: pointer;"></a>
<a id="btnFlipHor" title="Flip Horizontal" style="cursor: pointer;"></a>
<a id="btnFlipVer" title="Flip Vertical" style="cursor: pointer;"></a>
<a id="btnResetImage" title="Reload the original page." style="cursor: pointer;"></a>
<a title="Save File"><asp:ImageButton ID="img_save" runat="server" ImageUrl="/resources/save.png" Height="20px" Width="20px" onclick="img_save_Click" /></a>
<a title="Delete Page"><asp:ImageButton ID="img_del_page" runat="server" ImageUrl="/resources/delete.png" Height="20px" Width="20px" onclick="img_del_page_Click" /></a>
</div>
</div>
</td>
<td style="border: 0;">
<div id="infoBar2" class="infoBar" style="width: 400px; background-color: Gray; border: 0;">
<div class="section" style="border: 0; width:90%;">
<a title="Add Page" style=" margin-left:10px;">
<asp:ImageButton ID="img_add_page" runat="server" ImageUrl="/resources/add.png"
Height="20px" Width="20px" onclick="img_add_page_Click" /></a>
<a title="Delete File" style=" margin-left:5px;">
<asp:ImageButton ID="img_del_file" runat="server"
ImageUrl="/resources/delete.png" Height="20px" Width="20px"
onclick="img_del_file_Click" /></a>
</div>
</div>
</td>
</tr>
<tr>
<td align="center" valign="top" style="border: 0; width: 800px;">
<ltwf:WebImageViewer ID="WebImageViewer1" runat="server" BorderStyle="Solid" BorderWidth="1px"
Height="650px" Width="800px" HorizontalAlignMode="Center" VerticalAlignMode="Center"
ImageFrameSize="1" DropShadowSize="1" />
</td>
<td></td>
</tr>
</table>
</center>
</div>
<div style=" display:none;">
<asp:HiddenField ID="hid_file_path" runat="server" />
</div>
<script type="text/javascript">
function btnResetImage_onclick() {
var index = WebThumbnailViewer1.getSelectedIndex();
var hid_file_path = document.getElementById("<%=hid_file_path.ClientID%>");
if (hid_file_path != null)
WebImageViewer1.OpenImageUrl(hid_file_path.value, index);
}
</script>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Leadtools.Web.Controls;
using Leadtools.Codecs;
using System.IO;
using Leadtools.Forms.DocumentReaders;
public partial class DocumentCleanDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
#if LEADTOOLS_V175_OR_LATER
Support.SetLicense();
#else
Support.Unlock();
#endif // #if LEADTOOLS_V175_OR_LATER
//Check if Kernel has expired
if (Leadtools.RasterSupport.KernelExpired)
{
string msg = "Your license file is missing, invalid or expired. LEADTOOLS will not function. Please contact LEAD Sales for information on obtaining a valid license.";
Page.ClientScript.RegisterStartupScript(this.GetType(), "KernalExpiredMessage", "alert('" + msg + "')", true);
return;
}
//// Load RasterCodecs options from the configuration file
//if (ConfigurationManager.AppSettings["RasterPdfInitialPath"] != null)
//{
// string pdfInitialPath = ConfigurationManager.AppSettings["RasterPdfInitialPath"];
// if (!string.IsNullOrEmpty(pdfInitialPath))
// {
// // Setup default RasterCodecs options, these options are per thread
// // and will be used by any RasterCodecs object created after this
// using (RasterCodecs codecs = new RasterCodecs())
// {
// codecs.Options.Pdf.InitialPath = pdfInitialPath;
// }
// }
//}
WebThumbnailViewer1.InputProfile = Server.MapPath("Resources\\InputProfile.xml");
WebImageViewer1.InputProfile = Server.MapPath("Resources\\InputProfile.xml");
get_upload_file();
}
}
private void get_upload_file()
{
string dirPath = Server.MapPath("~/upload/");
if (Directory.Exists(dirPath))
{
//获得目录信息
DirectoryInfo dir = new DirectoryInfo(dirPath);
//获得目录文件列表
FileInfo[] files = dir.GetFiles("*.*");
string[] fileNames = new string[files.Length];
//临时数据表
DataTable dt = new DataTable();
dt.Columns.Add("file_name");
foreach (FileInfo fileInfo in files)
{
DataRow dr = dt.NewRow();
dr["file_name"] = fileInfo.Name;
dt.Rows.Add(dr);
}
DataList1.DataSource = dt;
DataList1.DataBind();
if (hid_file_path.Value == string.Empty && dt.Rows.Count > 0)
Load_File(@"upload\" + dt.Rows[0][0], true);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
bool fileOK = false;
string path = Server.MapPath("~/upload/");
if (!System.IO.Directory.Exists(path))
System.IO.Directory.CreateDirectory(path);
if (FileUpload1.HasFile)
{
String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg", ".pdf", ".tif", ".tiff", ".doc", ".docx", ".txt", ".ppt", ".pptx", ".jbg", ".cmp", ".xlsx", ".xls" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions)
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
string file_name = DateTime.Now.ToString("yyyyMMddHHmmssfff") + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
FileUpload1.SaveAs(path + file_name);
get_upload_file();
Load_File(@"upload\" + file_name, true);
LabMessage1.Text = "文件上传成功.";
}
catch
{
LabMessage1.Text = "文件上传失败.";
}
}
else
{
LabMessage1.Text = "该类型文件无法上传.";
}
}
static string sel_file = "";
private void Load_File(string file_path, bool is_clear)
{
using (RasterCodecs codecs = new RasterCodecs())
{
//if (file_path.Substring(file_path.LastIndexOf(".") + 1).ToLower() == "txt")
//{
codecs.Options.Txt.Load.Enabled = true;
//codecs.Options.Txt.Load.FontColor = new Leadtools.RasterColor(255, 0, 0);
//codecs.Options.Txt.Load.FontSize = 12;
//codecs.Options.Txt.Load.Bold = true;
//codecs.Options.RasterizeDocument.Load.LeftMargin = 1.00;
//codecs.Options.RasterizeDocument.Load.TopMargin = 1.25;
//codecs.Options.RasterizeDocument.Load.PageWidth = 8.50;
//codecs.Options.RasterizeDocument.Load.PageHeight = 11.00;
//codecs.Options.RasterizeDocument.Load.BottomMargin = 1.00;
//codecs.Options.RasterizeDocument.Load.RightMargin = 1.25;
//}
CodecsImageInfo info = codecs.GetInformation(WebThumbnailViewer1.Page.MapPath(file_path), true);
if (is_clear)
{
hid_file_path.Value = sel_file = file_path;
WebThumbnailViewer1.Clear();
WebImageViewer1.Clear();
}
int page_index = WebThumbnailViewer1.Count;
if (info.TotalPages > 1)
{
string thumbText = string.Empty;
for (int i = 0; i < info.TotalPages; i++)
{
page_index += 1;
thumbText = string.Format("age{0}", page_index);
WebThumbnailViewer1.Add(file_path, i + 1, i + 1, thumbText);
}
}
else
{
page_index += 1;
WebThumbnailViewer1.Add(file_path, 1, 1, "age" + page_index);
}
}
WebThumbnailViewer1.SelectedIndex = 0;
WebImageViewer1.UseDpi = true;
WebImageViewer1.MouseInteractiveMode.LeftButton = ImageViewerMouseInteractiveMode.Pan;
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "load_file")
{
Label lab_file = (Label)e.Item.FindControl("lab_file");
Load_File(@"upload\" + lab_file.Text, true);
}
}
protected void img_del_page_Click(object sender, ImageClickEventArgs e)
{
if (WebThumbnailViewer1.SelectedIndex >= 0)
{
WebThumbnailViewer1.Remove(WebThumbnailViewer1.SelectedIndex, WebThumbnailViewer1.SelectedIndex);
if (sel_file.Substring(sel_file.LastIndexOf(".") + 1).ToLower() == "pdf")
{
using (RasterCodecs codecs = new RasterCodecs())
{
codecs.Options.Pdf.InitialPath = Server.MapPath("~/Third Dlls/");
Leadtools.Pdf.PDFFile pdf_file = new Leadtools.Pdf.PDFFile(WebThumbnailViewer1.Page.MapPath(sel_file));
pdf_file.DeletePages(WebThumbnailViewer1.SelectedIndex + 1, WebThumbnailViewer1.SelectedIndex + 1, WebThumbnailViewer1.Page.MapPath(sel_file));
}
}
else
{
using (RasterCodecs codecs = new RasterCodecs())
{
codecs.DeletePage(WebThumbnailViewer1.Page.MapPath(sel_file), WebThumbnailViewer1.SelectedIndex + 1);
//using (Leadtools.RasterImage image = codecs.Load(WebThumbnailViewer1.Page.MapPath(sel_file)))
//{
// codecs.Save(image, WebThumbnailViewer1.Page.MapPath(sel_file), image.OriginalFormat, 0);
//}
}
//DocumentReaderLoadOptions loadOptions = new DocumentReaderLoadOptions();
//using (DocumentReader reader = DocumentReader.Create(WebThumbnailViewer1.Page.MapPath(sel_file), loadOptions))
//{
// reader.RasterCodecsInstance.DeletePage(WebThumbnailViewer1.Page.MapPath(sel_file), WebThumbnailViewer1.SelectedIndex + 1);
//}
}
Load_File(sel_file, true);
}
}
protected void img_save_Click(object sender, ImageClickEventArgs e)
{
//if (WebThumbnailViewer1.SelectedIndex >= 0)
//{
// using (RasterCodecs codecs = new RasterCodecs())
// {
// ThumbnailInfo info = WebThumbnailViewer1.GetThumbnailInfo(WebThumbnailViewer1.SelectedIndex);
// using (Leadtools.RasterImage image = codecs.Load(WebThumbnailViewer1.Page.MapPath(info.CacheFileName)))
// {
// codecs.Save(image, WebThumbnailViewer1.Page.MapPath(sel_file), image.OriginalFormat, image.BitsPerPixel, 1, 1, info.Page, CodecsSavePageMode.Replace);
// }
// }
//}
if (sel_file.Substring(sel_file.LastIndexOf(".") + 1).ToLower() == "pdf")
{
using (RasterCodecs codecs = new RasterCodecs())
{
codecs.Options.Pdf.InitialPath = Server.MapPath("~/Third Dlls/");
Leadtools.Pdf.PDFFile pdf_file = new Leadtools.Pdf.PDFFile(WebThumbnailViewer1.Page.MapPath(sel_file));
for (int i = 0; i < WebThumbnailViewer1.Count; i++)
{
ThumbnailInfo info = WebThumbnailViewer1.GetThumbnailInfo(i);
if (info.CacheFileName.Length > 0)
{
Leadtools.Pdf.PDFFile pdf = new Leadtools.Pdf.PDFFile(WebThumbnailViewer1.Page.MapPath(info.CacheFileName));
if (i < pdf_file.GetPageCount())
pdf_file.ReplacePagesFrom(i + 1, pdf, 1, 1);
else
pdf_file.InsertPagesFrom(i + 1, pdf, 1, 1);
//using (Leadtools.RasterImage image = codecs.Load(WebThumbnailViewer1.Page.MapPath(info.CacheFileName)))
//{
// if (i < page_count)
// codecs.Save(image, WebThumbnailViewer1.Page.MapPath(sel_file), image.OriginalFormat, image.BitsPerPixel, 1, 1, i + 1, CodecsSavePageMode.Replace);
// else
// codecs.Save(image, WebThumbnailViewer1.Page.MapPath(sel_file), image.OriginalFormat, image.BitsPerPixel, 1, 1, i + 1, CodecsSavePageMode.Append);
//}
}
else
{
Leadtools.Pdf.PDFFile pdf = new Leadtools.Pdf.PDFFile(WebThumbnailViewer1.Page.MapPath(info.Url));
if (i < pdf_file.GetPageCount())
pdf_file.ReplacePagesFrom(i + 1, pdf, 1, 1);
else
pdf_file.InsertPagesFrom(i + 1, pdf, 1, 1);
}
}
}
}
else
{
using (RasterCodecs codecs = new RasterCodecs())
{
CodecsImageInfo img_info = codecs.GetInformation(WebThumbnailViewer1.Page.MapPath(sel_file), true);
for (int i = 0; i < WebThumbnailViewer1.Count; i++)
{
ThumbnailInfo info = WebThumbnailViewer1.GetThumbnailInfo(i);
if (info.CacheFileName.Length > 0)
{
using (Leadtools.RasterImage image = codecs.Load(WebThumbnailViewer1.Page.MapPath(info.CacheFileName)))
{
if (i < img_info.TotalPages)
codecs.Save(image, WebThumbnailViewer1.Page.MapPath(sel_file), image.OriginalFormat, image.BitsPerPixel, 1, 1, info.Page, CodecsSavePageMode.Replace);
else
codecs.Save(image, WebThumbnailViewer1.Page.MapPath(sel_file), image.OriginalFormat, image.BitsPerPixel, 1, 1, info.Page, CodecsSavePageMode.Append);
}
}
else
{
using (Leadtools.RasterImage image = codecs.Load(WebThumbnailViewer1.Page.MapPath(info.Url), 0, CodecsLoadByteOrder.BgrOrGray, info.Page, info.Page))
{
if (i < img_info.TotalPages)
codecs.Save(image, WebThumbnailViewer1.Page.MapPath(sel_file), image.OriginalFormat, image.BitsPerPixel, 1, 1, info.Page, CodecsSavePageMode.Replace);
else
codecs.Save(image, WebThumbnailViewer1.Page.MapPath(sel_file), image.OriginalFormat, image.BitsPerPixel, 1, 1, info.Page, CodecsSavePageMode.Append);
}
}
}
}
}
}
protected void img_add_page_Click(object sender, ImageClickEventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
CheckBox cb = (CheckBox)item.FindControl("cb_file");
if (cb.Checked == true)
{
Label lab_file = (Label)item.FindControl("lab_file");
Load_File(@"upload\" + lab_file.Text, false);
cb.Checked = false;
}
}
}
protected void img_del_file_Click(object sender, ImageClickEventArgs e)
{
int check_num = 0;
foreach (DataListItem item in DataList1.Items)
{
CheckBox cb = (CheckBox)item.FindControl("cb_file");
if (cb.Checked == true)
{
check_num += 1;
Label lab_file = (Label)item.FindControl("lab_file");
string file_path = WebThumbnailViewer1.Page.MapPath(@"upload\" + lab_file.Text);
if (File.Exists(file_path))
{
File.Delete(file_path);
if (@"upload\" + lab_file.Text == sel_file)
hid_file_path.Value = string.Empty;
}
}
}
if (check_num > 0)
get_upload_file();
}
} |