2013年11月29日 星期五

[ASP.NET]點選按鈕直接列印PDF內容

Notice : IE Only,如果是其他瀏覽器請點上一頁

前言

做IT的不外乎就是要面對公司內部千奇百怪的需求,有時使用者會得寸進尺,今天接到的需求是要在網頁上點按鈕,直接列印PDF內容,"跳出下載視窗打開文件列印太慢了" , "直接用瀏覽器開點列印還要選擇太麻煩了" !@#$@#%#…
--碎碎唸分格線--
以下就實做懶人列印PDF,點一下就可以去列表機等了:
aspx
<object id="Object1" name="obj_pdf" type="application/pdf" width="1" height="1">
     <param name='SRC' value='<%= FileName %>' />
</object>
<!-- 點按鈕觸發列印-->
<input type="button" value="Print PDF" onclick="document.obj_pdf.printAll()" />

aspx.cs
public static string FileName = "";
protected void Page_Load(object sender, EventArgs e)
{
     //檔案不能寫死,故針對實際情況給檔名、路徑
     FileName = "test.pdf";
}

點選後就會跳出提醒視窗,如果下次不想顯示可直接打勾

28fa7b3a-0429-4980-932d-f657fdcebb9f

--

Reference

http://forums.adobe.com/thread/976153

http://www.codeproject.com/Tips/689325/Send-PDF-files-directly-to-client-printer-without

[MS-SQL]SQL自訂函數回傳Table

以往寫T-SQL函數通常都Return一個值,但今天學到也可以回傳Table的資料,底下寫一個範例,針對傳入不同的條件,回傳不同的Table:

CREATE FUNCTION [dbo].[Test_Function]
(@ID int)
RETURNS @TABLE TABLE (
ID varchar(25),
Number varchar(25)
)
AS
BEGIN

IF @ID = 1
begin
INSERT INTO @TABLE (ID,Number)
select 'ID1','ID Number 1'
end
else
BEGIN
INSERT INTO @TABLE (ID,Number)
select 'ID2','ID Number 2'
end

RETURN
END

如果Table欄位太多,定義上麻煩可以參考我之前寫的文章,


使用TempTable的方式: http://kyleshen616.blogspot.tw/2013/10/ms-sqltemp-tableifelse-if_29.html

[C#]使用FileSystemWatcher偵測資料夾是否有新增檔案,並移動該檔案

回答論壇問題順便寫了簡單的Sample Code,順便做個小記錄 :P
 FileSystemWatcher可以在某個資料夾有檔案異動時,觸發事件,然後可以針對這些檔案做這些事,直覺就想到我在匯入相機的照片時,檔名都不是我想要的,所以就寫個小程式讓我再copy檔案時,能直接搬移這些檔案並更名。
FileSystemWatcher共用以下事件可用(圖片來自MSDN)
image
本例使用Created來實現,首先新增一個Windows Form專案,並在初始化後撰寫以下Code:
public Form1()
{
     InitializeComponent();
     //設定針測的檔案類型,如不指定可設定*.*
     FileSystemWatcher watch = new FileSystemWatcher(OriginAlbum, "*.jpeg");
     //開啟監聽
     watch.EnableRaisingEvents = true;
     //是否連子資料夾都要偵測
     watch.IncludeSubdirectories = true;
     //新增時觸發事件
     watch.Created += watch_Created;
}

void watch_Created(object sender, FileSystemEventArgs e)
{
     string DirectPath = e.FullPath;
     //TODO 實做抓取檔案,並移動更名檔案
}


e.FullPath就可以接到該目錄,就可以實做一些事

完整程式碼:

string OriginAlbum = @"D:\temp2"; //針測的資料夾
string NewAlbum = @"D:\Album"; //搬移後的資料夾

public Form1()
{
     InitializeComponent();
     //設定針測的檔案類型,如不指定可設定*.*
     FileSystemWatcher watch = new FileSystemWatcher(OriginAlbum, "*.jpeg");
     //開啟監聽
     watch.EnableRaisingEvents = true;
     //是否連子資料夾都要偵測
     watch.IncludeSubdirectories = true;
     //新增時觸發事件
     watch.Created += watch_Created;
}

void watch_Created(object sender, FileSystemEventArgs e)
{
     string DirectPath = e.FullPath;
     //如果偵測到為檔案,則依路徑對此資料夾做檔案處理
     if (IsFilePath(DirectPath))
     {
          string fileFormat = "{0}-{1}.jpeg"; //檔名格式
          string FileDate = DateTime.Today.ToShortDateString();
          string CreateAlbum = NewAlbum + @"\" + FileDate;

          string DirectoryName = Path.GetDirectoryName(e.FullPath); //該檔案的資料夾名稱
          var files = Directory.GetFiles(DirectoryName, @"*.*"); //取得資料夾所有檔案

          //如果該資料夾存在檔案,則刪除檔案
          if (System.IO.Directory.Exists(CreateAlbum))
          {
               DeleteAllFile(CreateAlbum);
          }
          else
          {
               System.IO.Directory.CreateDirectory(CreateAlbum);
          }

          int i = 0;
          foreach (string f in files)
          {
               Console.WriteLine("task:"+f);
               //依順序改檔名
               System.IO.Directory.CreateDirectory(DirectoryName);
               string fileCount = (++i).ToString("D3");
               string newFileName = System.IO.Path.Combine(CreateAlbum, string.Format(fileFormat, FileDate, fileCount));
               //搬移ReName後的檔案
               System.IO.File.Move(f, newFileName);
          }
     }
    
}

--

Reference

http://msdn.microsoft.com/zh-tw/library/system.io.filesystemwatcher.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

http://www.dotblogs.com.tw/dotnetfactory/archive/2008/04/10/2780.aspx

http://www.dotblogs.com.tw/yc421206/archive/2009/01/18/6861.aspx

http://www.dotblogs.com.tw/jaigi/archive/2013/08/21/115097.aspx

2013年11月12日 星期二

[JQuery Plugin]三步驟製作全景相片撥放器

基本介紹

功能敘述:全景相片在自定義的div裡面自動撥放,並可控制左右方向
套件名稱:Panoramas
JQuery版本:1.10.1

實做

Step 1 載入JQuery & Plugin
http://www.openstudio.fr/jquery.panorama/js/jquery.panorama.js
<script src="../../Scripts/jquery-1.10.1.min.js"></script>
<script src="jquery.panorama.js"></script>
Step 2 載入 CSS
http://www.openstudio.fr/jquery.panorama/css/jquery.panorama.css
<link href="jquery.panorama.css" rel="stylesheet" />
Step 3 Html加入img
<img src="pic.jpg" class="panorama" width="1084" height="375" />

 

運行結果

JSFIDDLE
1 
--
小編mur mur : 決定以後玩到好玩的JQuery套件都不囉嗦直接附上程式碼~