258 lines
9.6 KiB
C#
258 lines
9.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Drawing.Printing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Lskj.MyControl
|
|
{
|
|
public partial class FrmSelectDownload : Form
|
|
{
|
|
public string FileName = string.Empty;
|
|
public string URL = string.Empty;
|
|
public bool isBatchDownLoad = false;
|
|
public FrmSelectDownload()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
/// <summary>
|
|
/// 初始加载
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void FrmSelectDownload_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (!isBatchDownLoad) this.Opacity = 100D;
|
|
this.labUrl.Text = URL;
|
|
this.labName.Text = FileName;
|
|
string FOLDER_NAME = isBatchDownLoad ? "BatchPrintFile" : "TempFolder";
|
|
this.labAddress.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FOLDER_NAME, FileName);//设置默认路径
|
|
if (isBatchDownLoad) this.btnDownload.PerformClick();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 点击预览筛选文件位置
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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.Text = System.IO.Path.GetFullPath(saveFileDialog.FileName);//获得文件路径,含文件名
|
|
labName.Text = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//获取文件名,不带路径
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// 点击下载附件
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnDownload_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(labUrl.Text))
|
|
{
|
|
MessageBox.Show("请填下载的地址!");
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(labName.Text))
|
|
{
|
|
MessageBox.Show("请填写下载的文件名称!");
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(labAddress.Text))
|
|
{
|
|
MessageBox.Show("请选择下载路径!");
|
|
return;
|
|
}
|
|
FrmDownload frm = new FrmDownload(labUrl.Text, labName.Text, labAddress.Text);
|
|
frm.isBatchPrind = isBatchDownLoad;
|
|
|
|
if (frm.ShowDialog() == DialogResult.OK)
|
|
{
|
|
frm.Dispose();
|
|
if (isBatchDownLoad)
|
|
{
|
|
PrintFile(labAddress.Text);
|
|
File.Delete(labAddress.Text);
|
|
}
|
|
this.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.Close();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 直接打开下载附件
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void btnOpen_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(labUrl.Text))
|
|
{
|
|
MessageBox.Show("请填下载的地址!");
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(labName.Text))
|
|
{
|
|
MessageBox.Show("请填写下载的文件名称!");
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(labAddress.Text))
|
|
{
|
|
MessageBox.Show("请选择下载路径!");
|
|
return;
|
|
}
|
|
//FrmDownload frm = new FrmDownload(labUrl.Text, labName.Text, labAddress.Text, true);
|
|
//if (frm.ShowDialog() == DialogResult.OK)
|
|
//{
|
|
// this.Close();
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// 关闭下载界面
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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.Text))
|
|
{
|
|
//图片抗锯齿
|
|
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
|
Stream fs = new FileStream(this.labAddress.Text, 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;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|