2011-05-29 87 views
11

我有一個生成PDF的ASP.NET Web應用程序。我正在使用iTextSharp。會發生什麼是您點擊一個按鈕並下載。我的僱主希望能夠點擊按鈕並使用打印對話框打開它。生成自動打印的PDF

+0

我很確定這是不可能的。我建議不要生成PDF,而是觸發'窗口的網頁。print()'只要它被加載。 – 2011-05-29 13:34:14

+0

@Samir問題在於它確實需要成爲pdf,因爲它是由斑馬打印機打印的標籤。 – 2011-05-29 13:39:25

+0

@Joe Tyman:它可以成爲一個圖像嗎?一個PNG什麼的?你可以把它放在網頁上。 – 2011-05-29 13:42:56

回答

11

方法1:使用您的PDF文件中嵌入的JavaScript 你可以嘗試用javascript調用this.print(false)(你可以用new PdfAction(PdfAction.PRINTDIALOG)這個)創建iText PDFAction對象,並將其與你的PDF文件的OpenAction event關聯。

在iText的Java中的代碼應該是這樣的:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("file.pdf")); 
... 
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG); 
writer.setOpenAction(action); 
... 

它不應該在C#太diferent。

作爲便箋,通過在文檔類中將屬性「自動打印」設置爲TRUE(正常的免責聲明適用),Amyuni PDF Creator .Net也是可能的。

acPDFCreatorLib.Initialize(); 
acPDFCreatorLib.SetLicenseKey("Amyuni Tech.", "07EFCDA00...BC4FB9CFD"); 
Amyuni.PDFCreator.IacDocument document = pdfCreator1.Document; 

// Open a PDF document from file 
System.IO.FileStream file1 = new System.IO.FileStream("test_input.pdf", FileMode.Open, FileAccess.Read, FileShare.Read); 

IacDocument document = new IacDocument(null); 

if (document.Open(file1, "")) 
{ 
    //Set AutoPrint 
    document.Attribute("AutoPrint").Value = true; 

    //Save the document 
    System.IO.FileStream file2 = new System.IO.FileStream("test_output.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write); 
    document.Save(file2, Amyuni.PDFCreator.IacFileSaveOption.acFileSaveView); 
} 

// Disposing the document before closing the stream clears out any data structures used by the Document object 
document.Dispose(); 

file1.Close(); 

// terminate library to free resources 
acPDFCreatorLib.Terminate(); 

這種方式需要一個閱讀器會照顧印刷要打開的PDF文件,並將其優點是,如果該文件保存在本地,每次該文件之後打開上就會顯示出缺點打印對話框。

方法2:使用瀏覽器中的javascript與顯示文件的閱讀器進行通信。
我發現this SO問題這個其他方法可能值得嘗試:

<html> 
<script language="javascript"> 
timerID = setTimeout("exPDF.print();", 1000); 
</script> 
<body> 
<object id="exPDF" type="application/pdf" data="111.pdf" width="100%" height="500"/> 
</body> 
</html> 

的想法是使用JavaScript在瀏覽器中指示PDF閱讀器來打印文件。這種方法適用於嵌入在HTML頁面中的PDF文件。

+0

我會在週二(長週末)嘗試這個,看看是否有效。代碼與C#沒有什麼不同。 – 2011-05-29 14:11:53

+0

工作正常!我只是添加了PDFAction,它效果很好。 – 2011-05-31 14:17:35

+0

我很高興它的工作!請注意,只有在安裝在客戶端的PDF閱讀器上啓用javascript後,此方法纔有效。 – yms 2011-05-31 14:37:34

3

正如yms所述,您可以生成一個帶有JavaScript或「Named」PDF操作的PDF,該文件在打開文檔時顯示Print對話框。我們已經在文章Create an Auto-Print PDF中使用我們的產品Gnostice PDFOne .NET進行了演示。我猜你可以在iText中做同樣的事情。如果Adobe Reader在瀏覽器中註冊爲PDF插件,那麼這兩個選項都可以使用。

HTML Javascript選項似乎只適用於IE。

免責聲明:我爲Gnostice工作。

4

這個網站的其他解決方案...我用這個解決方案和工作帶來極大的

我從Crystal報表PDF文件流,我添加openaction與pdfsharp

鏈接:http://www.vo1dmain.info/pdfsharp-howto-inject-javascript-into-pdf-autoprinting-functionality#comments

public static MemoryStream AddAutoPrint(Stream pdfStream, bool ShowPrintDialog = true, int NumCopies = 1) 
{ 
    PdfSharp.Pdf.PdfDocument doc = PdfSharp.Pdf.IO.PdfReader.Open(pdfStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import); 
    PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument(); 

    for (int idx = 0; idx < doc.PageCount; idx++) 
    { 
    PdfSharp.Pdf.PdfPage p = doc.Pages[idx]; 
    outputDocument.AddPage(p); 
    } 

    outputDocument.Info.Author = "author name"; 

    string JSScript = string.Empty; 
    JSScript += "var pp = this.getPrintParams(); "; 

    if(NumCopies > 0) 
    { 
    JSScript += "pp.NumCopies = " + NumCopies.ToString() + "; "; 
    } 

    if(!ShowPrintDialog) 
    { 
    JSScript += "pp.interactive = pp.constants.interactionLevel.automatic; "; 
    } 


    JSScript += "this.print({printParams: pp}); "; 


    PdfSharp.Pdf.PdfDictionary dictJS = new PdfSharp.Pdf.PdfDictionary(); 
    dictJS.Elements["/S"] = new PdfSharp.Pdf.PdfName("/JavaScript"); 
    //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "print(true);"); 
    //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "this.print({bUI: false, bSilent: true, bShrinkToFit: true});"); 
    //dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, "var pp = this.getPrintParams(); pp.NumCopies = 2; pp.interactive = pp.constants.interactionLevel.automatic; this.print({printParams: pp});"); 
    dictJS.Elements["/JS"] = new PdfSharp.Pdf.PdfStringObject(outputDocument, JSScript); 


    outputDocument.Internals.AddObject(dictJS); 

    PdfSharp.Pdf.PdfDictionary dict = new PdfSharp.Pdf.PdfDictionary(); 
    PdfSharp.Pdf.PdfArray a = new PdfSharp.Pdf.PdfArray(); 
    dict.Elements["/Names"] = a; 
    a.Elements.Add(new PdfSharp.Pdf.PdfString("EmbeddedJS")); 
    a.Elements.Add(PdfSharp.Pdf.Advanced.PdfInternals.GetReference(dictJS)); 

    outputDocument.Internals.AddObject(dict); 

    PdfSharp.Pdf.PdfDictionary group = new PdfSharp.Pdf.PdfDictionary(); 
    group.Elements["/JavaScript"] = PdfSharp.Pdf.Advanced.PdfInternals.GetReference(dict); 

    outputDocument.Internals.Catalog.Elements["/Names"] = group; 

    MemoryStream ms = new MemoryStream(); 

    outputDocument.Save(ms, false); 

    return ms; 
} 
0

如何瞭解舊時尚OLE?它仍然支持大多數我知道的所有文檔層...在C#中,我通常做這樣的事情..其中PDF,RTF,DOC,XLS ...無所謂......他們都打印..

public void HandlePresentation(string fullFilePath, bool viewOnScreen, bool autoPrint) 
{ 
    ProcessStartInfo info = new ProcessStartInfo(fullFilePath); 
    if (autoPrint) 
    { 
     info.Verb = "Print"; 
     info.WindowStyle = ProcessWindowStyle.Hidden; // not normally required 
     Process.Start(info); 
     info.Verb = string.Empty; 
    } 

    if (viewOnScreen) 
    { 
     info.WindowStyle = ProcessWindowStyle.Normal; 
     Process.Start(info); 
    } 
} 
+0

原始問題指定了「ASP.NET Web應用程序」。 Ole在這種情況下沒有幫助,因爲您無法在網頁上訪問它。 – yms 2018-01-26 12:19:06