基於Metronic的Bootstrap開發架構經驗總結(9)--實現Web頁面內容的預覽列印和儲存操作

來源:互聯網
上載者:User

標籤:bootstrap metronic web開發架構

在前面介紹了很多篇相關的《Bootstrap開發架構》的系列文章,這些內容基本上覆蓋到了我這個Bootstrap架構的各個主要方面的內容,總體來說基本達到了一個穩定的狀態,隨著時間的推移可以會引入一些更好更新的內容進行完善,本篇繼續這個系列,主要介紹如何?Web頁面內容的預覽列印和儲存操作。

1、Web頁面列印的問題

在此之前,我一般使用比較好用的LODOP來執行列印的操作,這個在我之前有很多文章都有涉及,這個控制項是一個ActiveX的控制項,需要下載安裝後就可以在頁面是進行列印的排版設計,預覽,列印等操作,還是很方便的一個控制項,因此都很適合普通內容的列印,證件的套打等操作。

不過隨著瀏覽器技術的更新,這個外掛程式在Chrome或者FireFox上好像不受支援了,基本上摒棄了這種外掛程式的處理方式了。例如如果我在頁面上需要列印對話方塊裡面的內容,如下所示。

650) this.width=650;" src="http://images2015.cnblogs.com/blog/8867/201601/8867-20160121092543265-1385306461.png" width="726" height="441" style="border:0px;" />

如果按正常使用LODOP的方式來進行處理的話,那麼會得到Chrome瀏覽器的提示,並且這個不管你重新下載安裝、更新LODOP控制項,都會繼續這個錯誤提示的。

650) this.width=650;" src="http://images2015.cnblogs.com/blog/8867/201601/8867-20160121092529172-616069224.png" style="border:0px;" />

這個在一篇《Lodop頁面總提示"未安裝"或"請升級"的可能原因》,以及《Lodop使用者應對Google瀏覽器停用Plugin技術的處理辦法》都有說明,因此我們需要另闢蹊徑來處理這個頁面列印等的處理了。

對於替代方式,這裡就是本篇內容介紹的主題了,我一直喜歡尋找一些比較好的方式的方式來實現自己需要的功能,於是找到了PrintThis的這個外掛程式(https://github.com/jasonday/printThis)以及jquery-print-preview-plugin(https://github.com/etimbo/jquery-print-preview-plugin),對比兩者我比較喜歡第一個的簡潔方便的使用。

2、PrintThis列印外掛程式的使用

有了上面的問題,我們引入一個新的列印方式,也就是JQuery外掛程式來實現我們所需要頁面內容的列印操作。

這個外掛程式的使用非常簡潔方便,首先需要在頁面裡面引入對應的JS檔案,如下所示。

<script src="~/Content/JQueryTools/printThis/printThis.js"></script>

我們再在頁面頂部增加兩個按鈕,如列印和匯出操作,代碼如下所示

<div class="toolbar"> <a href="#" onclick="javascript:Preview();"><img alt="預覽列印" src="~/Content/images/print.gif" /><br />預覽列印</a> <a href="#" onclick="javascript:SaveAs();"><img alt="另存新檔" src="~/Content/images/saveas.gif" /><br />另存新檔</a>         </div>

650) this.width=650;" src="http://images2015.cnblogs.com/blog/8867/201601/8867-20160121093348078-613001538.png" width="595" height="150" style="border:0px;" />

然後我們還需要聲明一個DIV用來放置顯示的Web頁面內容,這樣也方便對它調用進行列印操作。

650) this.width=650;" src="http://images2015.cnblogs.com/blog/8867/201601/8867-20160121093558218-1122789409.png" style="border:0px;" />

我們列印的處理代碼也很簡單,就是直接對層進行列印處理就可以了,可以看到下面的使用代碼非常簡單。

//預覽列印function Preview() {$("#printContent").printThis({debug: false,importCSS: true,importStyle: true,printContainer: true,loadCSS: "/Content/Themes/Default/style.css",pageTitle: "通知公告",removeInline: false,printDelay: 333,header: null,formValues: true});};

列印執行後,IE和Chrome都會彈出一個預覽列印對話方塊,確認是否進行列印的操作。

650) this.width=650;" src="http://images2015.cnblogs.com/blog/8867/201601/8867-20160121094348234-1809082665.png" width="891" height="726" style="border:0px;" />

 

3、頁面內容的儲存操作

有時候,為了方便業務處理,我們一般也可以提供給使用者一個匯出列印內容的操作,如下所示代碼就是把列印的內容匯出到Word裡面給使用者加工等用途。

function SaveAs() {var id = $(‘#ID2‘).val();window.open(‘/Information/ExportWordById?id=‘ + id );}

上面的操作,主要就是調用了MVC的控制器方法進行處理,傳入一個id就可以把內容提取出來,然後把它產生所需的Word內容即可。

這裡後台我們主要利用Apose.Word控制項來進行模板化的文檔產生,具體可以參考一下我前面介紹過的使用方法文章《利用Aspose.Word控制項實現Word文檔的操作》、《利用Aspose.Word控制項和Aspose.Cell控制項,實現Word文檔和Excel文檔的模板化匯出》。

其中我們可以在書籤裡面定義或者查看一些書籤的資訊,如所示。

650) this.width=650;" src="http://images2015.cnblogs.com/blog/8867/201601/8867-20160121095727406-1053270394.png" width="668" height="457" style="border:0px;" />

這樣我們在代碼裡面,就可以擷取資訊並指定這個Word模板了。

            InformationInfo info = BLLFactory<Information>.Instance.FindByID(id);            if (info != null)            {                string template = "~/Content/Template/政策法規模板.doc";                string templateFile = Server.MapPath(template);                Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);

WORD模板的內容,可以使用文本替換方式,如下所示。

SetBookmark(ref doc, "Content", info.Content);

也可以使用書籤BookMark方式查詢替換,如下代碼所示。

Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks[title];if (bookmark != null){bookmark.Text = value;}

對於主體的HTML內容,這需要特殊對待,一般需要使用插入HTML的專用方式進行寫入內容,否則就顯示HTML代碼了,使用專用HTML方法寫入的內容,和我們在網頁上看到的基本沒有什麼差異了。如下代碼所示。

DocumentBuilder builder = new DocumentBuilder(doc);Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["Content"];if (bookmark != null){builder.MoveToBookmark(bookmark.Name);builder.InsertHtml(info.Content);}

整個匯入WORD文檔的方法就是利用這些內容的整合,實現一個標準文檔的產生,這種業務文檔是固定模板的,因此很適合在實際業務中使用,比起使用其他方式自動產生的HTML檔案或者文檔,有更好的可塑性和美觀性。

整個代碼如下所示。

        public FileStreamResult ExportWordById(string id)        {            if (string.IsNullOrEmpty(id)) return null;            InformationInfo info = BLLFactory<Information>.Instance.FindByID(id);            if (info != null)            {                string template = "~/Content/Template/政策法規模板.doc";                string templateFile = Server.MapPath(template);                Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);                #region 使用文本方式替換                //Dictionary<string, string> dictSource = new Dictionary<string, string>();                //dictSource.Add("Title", info.Title);                //dictSource.Add("Content", info.Content);                //dictSource.Add("Editor", info.Editor);                //dictSource.Add("EditTime", info.EditTime.ToString());                //dictSource.Add("SubType", info.SubType);                  //foreach (string name in dictSource.Keys)                //{                //    doc.Range.Replace(name, dictSource[name], true, true);                //}                 #endregion                //使用書籤方式替換                SetBookmark(ref doc, "Title", info.Title);                SetBookmark(ref doc, "Editor", info.Editor);                SetBookmark(ref doc, "EditTime", info.EditTime.ToString());                SetBookmark(ref doc, "SubType", info.SubType);                                //SetBookmark(ref doc, "Content", info.Content);                //對於HTML內容,需要通過InsertHtml方式進行寫入                DocumentBuilder builder = new DocumentBuilder(doc);                Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["Content"];                if (bookmark != null)                {                    builder.MoveToBookmark(bookmark.Name);                    builder.InsertHtml(info.Content);                }                                                doc.Save(System.Web.HttpContext.Current.Response, info.Title, Aspose.Words.ContentDisposition.Attachment,                    Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Doc));                HttpResponseBase response = ControllerContext.HttpContext.Response;                response.Flush();                response.End();                return new FileStreamResult(Response.OutputStream, "application/ms-word");            }            return null;        }        private void SetBookmark(ref Aspose.Words.Document doc, string title, string value)        {            Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks[title];            if (bookmark != null)            {                bookmark.Text = value;            }        }

最後匯出的WORD文檔就是模板化的具體文檔內容了,WORD預覽介面如下所示。

650) this.width=650;" src="http://images2015.cnblogs.com/blog/8867/201601/8867-20160121100925656-2059920.png" style="border:0px;" />


本文出自 “伍華聰的部落格” 部落格,請務必保留此出處http://wuhuacong.blog.51cto.com/1779896/1828737

基於Metronic的Bootstrap開發架構經驗總結(9)--實現Web頁面內容的預覽列印和儲存操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.