如何從Web應用程序生成紙張打印?從網絡應用程序打印
具體而言,我正在考慮更復雜的紙質文件,如文憑,發票和合同。可變數量的頁面,包括框架,表格,標識和頁眉/頁腳。
今天我對某些東西使用了自定義表單和CSS,對其他人使用了iTextSharp(我使用了asp.net和MS-SQL),但是我認爲這兩種方法都非常耗時且很難在不同文檔間保持一致。
有沒有更好的方法去解決它?
如何從Web應用程序生成紙張打印?從網絡應用程序打印
具體而言,我正在考慮更復雜的紙質文件,如文憑,發票和合同。可變數量的頁面,包括框架,表格,標識和頁眉/頁腳。
今天我對某些東西使用了自定義表單和CSS,對其他人使用了iTextSharp(我使用了asp.net和MS-SQL),但是我認爲這兩種方法都非常耗時且很難在不同文檔間保持一致。
有沒有更好的方法去解決它?
使用iTextSharp從頭創建文檔可能非常耗時。作爲替代,您可以通過向其添加表單字段(如果需要)來創建(或重新使用)PDF「模板」文檔。最簡單的方法是使用Adobe Acrobat的完整版本,但也可以使用iTextSharp添加可填寫的表單域。
例如,對於獎項或文憑,您可以查找,創建或修改包含文檔的所有文本,圖形,花哨邊框和字體的PDF,然後爲收件人的名稱添加一個表單域。您可以添加日期,簽名行,獎勵類型等其他字段。
然後,從Web應用程序中使用iTextSharp可以非常容易地填寫表單,將其扁平化並將其傳回給用戶。
在這篇文章的最後是一個完整的ASHX處理程序代碼示例。
另外請記住,iTextSharp(或只是iText)也可用於組合PDF文檔或來自不同文檔的頁面。因此,對於封面或說明頁面具有固定設計但動態生成內容的年度報告,您可以打開封面,爲報告打開模板頁面,在模板的空白區域生成動態內容,打開後頁「樣板」,然後將它們全部合併成單個文檔以返回給用戶。
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Text;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace iTextFormFillerDemo
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DemoForm : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/pdf";
//This line will force the user to either open or save the
//file instead of it appearing on its own page - if you remove it,
//the page will appear in the browser in the same window.
context.Response.AddHeader("Content-Disposition",
"attachment; filename=DemoForm_Filled.pdf");
FillForm(context.Response.OutputStream);
context.Response.End();
}
// Fills the form and pushes it out the output stream.
private void FillForm(System.IO.Stream outputStream)
{
//Need to get the proper directory (dynamic path) for this file.
//This is a filesystem reference, not a web/URL reference...
//The PDF reader reads in the fillable form
PdfReader reader = new PdfReader("C:/DemoForm_Fillable.pdf");
//The PDF stamper creates an editable working copy of the form from the reader
//and associates it with the response output stream
PdfStamper stamper = new PdfStamper(reader, outputStream);
//The PDF has a single "form" consisting of AcroFields
//Note that this is shorthand for writing out
//stamper.AcroFields.SetField(...) for each set
AcroFields form = stamper.AcroFields;
//Set each of the text fields this way: SetField(name, value)
form.SetField("txtFieldName", "Field Value");
form.SetField("txtAnotherFieldName", "AnotherField Value");
//Set the radio button fields using the names and string values:
form.SetField("rbRadioButtons", "Yes"); //or "No"
//Form flattening makes the form non-editable and saveable with the
//form data filled in
stamper.FormFlattening = true;
//Closing the stamper flushes it out the output stream
stamper.Close();
//We're done reading the file
reader.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
恕我直言,因爲如果答案
即使你說你已經嘗試了基於CSS的方法,最好的辦法就是向前來看看在@print媒體類型固定格式打印PDF(http://www.w3.org/TR/CSS2/media.html )。基本上允許您使用不同的樣式表進行打印,而不是屏幕或任何其他(許多)媒體類型。
然後,如果你想自動啓動打印時,要使用像這樣(的JavaScript):
window.print();
當然,你要檢查瀏覽器通過JavaScript實際支持打印,並採取相應的行動:
function doPrint() {
if(!window.print())
{
alert("Your browser does not support direct printing, please select 'Print' from the 'File' menu.");
}
}
但是,如果你堅持認爲CSS不能滿足您的需求,那麼你很可能會需要去實現一個基於PDF的解決方案。雖然,我沒有通過PDF生成網頁打印的經驗。
如果您使用SQL Server,則可以使用報告服務創建報告。用戶可以直接打印或者將其自身導出爲多種不同的格式(您可以控制哪些格式可供用戶使用)。
我在過去曾經使用過這種方法,並且我發現在打印或導出文檔時,它可以更好地控制文檔的外觀。
感謝您的輸入!能夠使用iTextSharp「完成」pdf模板,在適當的應用程序中完成所有棘手的佈局聽起來很完美。如果您確實有一些示例代碼或指向涵蓋此方法的資源的鏈接,我將非常感激。 – Console 2009-01-22 10:00:21