using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lskj.AutoUpdate;
using System.IO;
using Lskj.Business;
using Lskj.Core;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Lskj.Business.Impl;
using System.Drawing.Printing;
using Lskj.Util;
namespace Lskj.Control
{
public partial class FrmSelectDownload : BaseForm
{
public string FileName = string.Empty;
public string URL = string.Empty;
public bool isBatchDownLoad = false;
public bool isprompt = true;
public bool isMultiple = false;//是否为忽略多选
public bool isOpenvisble = false;//是否可以直接打开
public string ModelId= string.Empty;//菜单名称
public string MenuName = string.Empty;//右键名称
public string MenuId = string.Empty;//菜单ID
public string SavePath = string.Empty;//下载路径(浏览器dll,多次下载,记录第一次路径,后续外部拿到后在默认赋值)
///
/// 右键多条下载是否显示
///
public bool RightDisplay = true;
///
/// 路径
///
public string DownloadPath = string.Empty;
public FrmSelectDownload()
{
InitializeComponent();
}
///
/// 初始加载
///
///
///
private void FrmSelectDownload_Load(object sender, EventArgs e)
{
try
{
if (!isBatchDownLoad && !isMultiple) this.Opacity = 100D;
if (isOpenvisble) this.btnOpen.Visible = false;
this.labUrl.EditText = URL;
this.labName.EditText = FileName;
string FOLDER_NAME = isBatchDownLoad ? ConstModel.Batch_FOLDER_NAME : ConstModel.TEMP_FOLDER_NAME;
this.labAddress.EditText = Path.Combine(CommonUnitity.SystemBinUrl, FOLDER_NAME, FileName);//设置默认路径
//上一次选中的地址
string address=IniHelper.Read("LastSavedAddress");
if (!string.IsNullOrWhiteSpace(address))
{
this.labAddress.EditText = Path.Combine(address, FileName);//设置默认路径
}
if (isBatchDownLoad) this.btnDownload.PerformClick();
if (isMultiple) this.btnDownload.PerformClick();
if (SystemInfo.Instance.HiddenUrl)
{
this.labUrl.Visible = false;
this.btnPreview.Location = new Point(this.btnPreview.Location.X, this.labName.Location.Y-5);
this.labAddress.Location = new Point(this.labAddress.Location.X, this.labName.Location.Y);
this.labName.Location = new Point(this.labName.Location.X, this.labUrl.Location.Y);
this.groupBox1.Height = this.labAddress.Location.Y + 30;
this.btnOpen.Location = new Point(this.btnOpen.Location.X, this.groupBox1.Location.Y+ this.groupBox1.Height+5);
this.btnDownload.Location = new Point(this.btnDownload.Location.X, this.groupBox1.Location.Y + this.groupBox1.Height + 5);
this.btnClose.Location = new Point(this.btnClose.Location.X, this.groupBox1.Location.Y + this.groupBox1.Height + 5);
this.Height = this.btnClose.Location.Y + this.btnClose.Height + 40;
}
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show(Message, ex.Message);
}
Lskj.Control.Model.AutoSizeChange.ControllInitializeSize(this);
}
///
/// 点击预览筛选文件位置
///
///
///
private void btnPreview_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = FileName;
saveFileDialog.Title = "下载模板文件到";
saveFileDialog.RestoreDirectory = true;
//点了保存按钮进入
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string localFilePath = labAddress.EditText = System.IO.Path.GetFullPath(saveFileDialog.FileName);//获得文件路径,含文件名
labName.EditText = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//获取文件名,不带路径
//记录保存的路径
IniHelper.Write("LastSavedAddress", localFilePath.Substring(0,localFilePath.LastIndexOf("\\")));
}
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("筛选文件位置出错!" + Message);
}
}
///
/// 点击下载附件
///
///
///
private void btnDownload_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(labUrl.EditText))
{
MessageUtil.Show("请填下载的地址!");
return;
}
if (string.IsNullOrEmpty(labName.EditText))
{
MessageUtil.Show("请填写下载的文件名称!");
return;
}
if (string.IsNullOrEmpty(labAddress.EditText))
{
MessageUtil.Show("请选择下载路径!");
return;
}
FrmDownload frm = new FrmDownload(labUrl.EditText, labName.EditText, labAddress.EditText,false, isprompt);
frm.isBatchPrind = isBatchDownLoad;
if (frm.ShowDialog() == DialogResult.OK)
{
LogUtil.WriteDebug(ModelId, "执行下载", "右键编号:"+ MenuId + " ,右键名称:"+ MenuName , "网址:"+ labUrl.EditText + " ,下载名称:" + labName.EditText+ " ,下载路径:"+ labAddress.EditText);
frm.Dispose();
if (isBatchDownLoad)
{
PrintFile(labAddress.EditText);
File.Delete(labAddress.EditText);
}
this.DialogResult = DialogResult.OK;
this.Close();
}
}
catch (Exception ex)
{
this.Close();
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("下载附件出错!" + Message);
}
}
///
/// 直接打开下载附件
///
///
///
private void btnOpen_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(labUrl.EditText))
{
MessageUtil.Show("请填下载的地址!");
return;
}
if (string.IsNullOrEmpty(labName.EditText))
{
MessageUtil.Show("请填写下载的文件名称!");
return;
}
if (string.IsNullOrEmpty(labAddress.EditText))
{
MessageUtil.Show("请选择下载路径!");
return;
}
FrmDownload frm = new FrmDownload(labUrl.EditText, labName.EditText, labAddress.EditText, true);
if (frm.ShowDialog() == DialogResult.OK)
{
this.Close();
}
}
catch (Exception ex)
{
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("直接打开下载附件出错!" + Message);
}
}
///
/// 关闭下载界面
///
///
///
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void PrintFile(string filePath)
{
bool isPic = false;//是否为图片
Stream fs = null;
Image img = null;
try
{
fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
img = Image.FromFile(filePath);
isPic = true;
}
catch (Exception)
{
isPic = false;
}
finally
{
if (fs != null)
fs.Close();
if (img != null)
img.Dispose();
}
if (isPic)
{
PrintDocument docToPrint = new PrintDocument();
docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PicToPrint_PrintPage);
docToPrint.DefaultPageSettings.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize { Width = 583, Height = 827, PaperName = "A5" };
PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
docToPrint.Print();
}
else
{
if (Path.GetExtension(filePath).Equals(".doc", StringComparison.CurrentCultureIgnoreCase) || Path.GetExtension(filePath).Equals(".docx", StringComparison.CurrentCultureIgnoreCase))//word文档
{
Spire.Doc.Document doc = new Spire.Doc.Document();
doc.LoadFromFile(filePath);
Spire.Doc.Section sec = doc.Sections[0];
sec.PageSetup.PageSize = Spire.Doc.Documents.PageSize.A5;
PrintDocument printDoc = doc.PrintDocument;
printDoc.Print();
}
else if (Path.GetExtension(filePath).Equals(".xls", StringComparison.CurrentCultureIgnoreCase) || Path.GetExtension(filePath).Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase))//excel文档
{
Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
workbook.LoadFromFile(filePath);
foreach (Spire.Xls.Worksheet worksheet in workbook.Worksheets)
{
worksheet.PageSetup.PaperSize = Spire.Xls.PaperSizeType.PaperA5;
worksheet.PageSetup.FitToPagesTall = 0;
worksheet.PageSetup.FitToPagesWide = 1;
}
PrintDocument printDoc = workbook.PrintDocument;
printDoc.Print();
}
else if (Path.GetExtension(filePath).Equals(".pdf", StringComparison.CurrentCultureIgnoreCase))
{
Spire.Pdf.PdfDocument pdfDocument = new Spire.Pdf.PdfDocument();
pdfDocument.LoadFromFile(filePath);
pdfDocument.Print();
}
else if (Path.GetExtension(filePath).Equals(".txt", StringComparison.CurrentCultureIgnoreCase))
{
Spire.Doc.Document doc = new Spire.Doc.Document();
doc.LoadFromFile(filePath);
Spire.Doc.Section sec = doc.Sections[0];
sec.PageSetup.PageSize = Spire.Doc.Documents.PageSize.A5;
PrintDocument printDoc = doc.PrintDocument;
printDoc.Print();
}
}
}
private void PicToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (!string.IsNullOrEmpty(this.labAddress.EditText))
{
//图片抗锯齿
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Stream fs = new FileStream(this.labAddress.EditText, FileMode.Open, FileAccess.Read);
System.Drawing.Image i = System.Drawing.Image.FromStream(fs);//转换为 IMAGE
fs.Close();
Rectangle m = e.MarginBounds;
if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
{
m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
}
else
{
m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
}
e.Graphics.DrawImage(i, m);
i.Dispose();
e.HasMorePages = false;
}
}
public void ClickDownload()
{
try
{
if (string.IsNullOrEmpty(URL))
{
MessageUtil.Show("请填下载的地址!");
return;
}
if (string.IsNullOrEmpty(FileName))
{
MessageUtil.Show("请填写下载的文件名称!");
return;
}
if (string.IsNullOrEmpty(DownloadPath))
{
MessageUtil.Show("请选择下载路径!");
return;
}
FrmDownload frm = new FrmDownload(URL, FileName, DownloadPath);
frm.RightDisplay = RightDisplay;
if (frm.ShowDialog() == DialogResult.OK)
{
frm.Dispose();
if (isBatchDownLoad)
{
PrintFile(labAddress.EditText);
File.Delete(labAddress.EditText);
}
this.Close();
}
}
catch (Exception ex)
{
this.Close();
string Message = ErrorMessage.PromptErrorMessage(ex);
MessageUtil.Show("下载附件出错!" + Message);
}
}
}
}