2008-12-05 13 views
32

मैं डब्ल्यूपीएफ में डेमो ऐप बना रहा हूं, जो मेरे लिए नया है। मैं वर्तमान में फ़्लो डॉक्यूमेंट में टेक्स्ट प्रदर्शित कर रहा हूं, और इसे प्रिंट करने की आवश्यकता है।एक डब्ल्यूपीएफ फ्लो डॉक्यूमेंट प्रिंटिंग

कोड मैं दिखता उपयोग कर रहा हूँ इस तरह:

 PrintDialog pd = new PrintDialog(); 
     fd.PageHeight = pd.PrintableAreaHeight; 
     fd.PageWidth = pd.PrintableAreaWidth; 
     fd.PagePadding = new Thickness(50); 
     fd.ColumnGap = 0; 
     fd.ColumnWidth = pd.PrintableAreaWidth; 

     IDocumentPaginatorSource dps = fd; 
     pd.PrintDocument(dps.DocumentPaginator, "flow doc"); 

fd मेरी FlowDocument है, और अब मैं प्रिंट विकल्प निर्दिष्ट करने के लिए डिफ़ॉल्ट प्रिंटर का उपयोग कर रहा बजाय अनुमति देता है उपयोगकर्ता के लिए। यह ठीक काम करता है, सिवाय इसके कि दस्तावेज़ प्रिंट के बाद, स्क्रीन पर प्रदर्शित फ्लो डॉक्यूमेंट को प्रिंटिंग के लिए निर्दिष्ट सेटिंग्स का उपयोग करने के लिए बदल दिया गया है।

मैं प्रिंट करने के बाद सबकुछ मैन्युअल रूप से रीसेट करके इसे ठीक कर सकता हूं, लेकिन क्या यह सबसे अच्छा तरीका है? क्या मैं इसे मुद्रित करने से पहले फ़्लो डॉक्यूमेंट की प्रतिलिपि बनाना चाहिए? या क्या कोई और दृष्टिकोण है जिस पर मुझे विचार करना चाहिए?

+8

आपका प्रश्न मेरा जवाब था। धन्यवाद! – BrokeMyLegBiking

उत्तर

35

हाँ, यह मुद्रण से पहले FlowDocument की एक प्रतिलिपि बनाने। ऐसा इसलिए है क्योंकि अंकन और मार्जिन अलग होंगे। यह मेरे लिए काम करता है।

private void DoThePrint(System.Windows.Documents.FlowDocument document) 
    { 
     // Clone the source document's content into a new FlowDocument. 
     // This is because the pagination for the printer needs to be 
     // done differently than the pagination for the displayed page. 
     // We print the copy, rather that the original FlowDocument. 
     System.IO.MemoryStream s = new System.IO.MemoryStream(); 
     TextRange source = new TextRange(document.ContentStart, document.ContentEnd); 
     source.Save(s, DataFormats.Xaml); 
     FlowDocument copy = new FlowDocument(); 
     TextRange dest = new TextRange(copy.ContentStart, copy.ContentEnd); 
     dest.Load(s, DataFormats.Xaml); 

     // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog, 
     // and allowing the user to select a printer. 

     // get information about the dimensions of the seleted printer+media. 
     System.Printing.PrintDocumentImageableArea ia = null; 
     System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia); 

     if (docWriter != null && ia != null) 
     { 
      DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator; 

      // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. 
      paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight); 
      Thickness t = new Thickness(72); // copy.PagePadding; 
      copy.PagePadding = new Thickness(
          Math.Max(ia.OriginWidth, t.Left), 
           Math.Max(ia.OriginHeight, t.Top), 
           Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right), 
           Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom)); 

      copy.ColumnWidth = double.PositiveInfinity; 
      //copy.PageWidth = 528; // allow the page to be the natural with of the output device 

      // Send content to the printer. 
      docWriter.Write(paginator); 
     } 

    } 
+4

यह केवल टेक्स्ट प्रिंट करने लगता है, आप ब्लॉकयूकॉन्टेनर को कैसे प्रिंट करते हैं? – Beaker

+0

@ बेकर इस समाधान को देखें जो आपको छवियों और अन्य BlockUIcontainer प्रिंट करने की अनुमति देता है: http://stackoverflow.com/a/18088609/1243372 –

7

आप नीचे दिए गए यूआरएल से कोड का उपयोग कर सकते हैं, यह एक निश्चित दस्तावेज़ में फ्लो दस्तावेज़ को लपेटता है और प्रिंट करता है कि, बड़ा फायदा यह है कि आप इसे मार्जिन, हेडर और पाद लेख जोड़ने के लिए उपयोग कर सकते हैं।

http://blogs.msdn.com/fyuan/archive/2007/03/10/convert-xaml-flow-document-to-xps-with-style-multiple-page-page-size-header-margin.aspx

0

मैं भी एक प्रवाह दस्तावेज़ बंद एक WPF रिपोर्ट उत्पन्न कर रहा हूँ, लेकिन मैं जानबूझ कर एक प्रिंट पूर्वावलोकन स्क्रीन के रूप में प्रवाह दस्तावेज़ उपयोग कर रहा हूँ। मैं चाहता हूं कि मार्जिन एक जैसा हो। आप how I did this here पढ़ सकते हैं।

आपके परिदृश्य में मैं सोच रहा हूं कि पूरे प्रवाह दस्तावेज़ की बजाय केवल अपनी सेटिंग्स की एक प्रति क्यों न बनाएं। यदि आप दस्तावेज़ को वापस मूल स्थिति में वापस करना चाहते हैं तो आप सेटिंग्स को दोबारा लागू कर सकते हैं।

+0

लिंक टूटा हुआ: --( – TravisWhidden

+0

मैंने WayBackMachine का उपयोग करके इस उत्तर में लिंक तय कर दिया है। http://www.archive.org/index.php – Dennis

0

दोनों पाठ और गैर-पाठ दृश्यों के साथ निम्न काम करता है:

//Clone the source document 
var str = XamlWriter.Save(FlowDoc); 
var stringReader = new System.IO.StringReader(str); 
var xmlReader = XmlReader.Create(stringReader); 
var CloneDoc = XamlReader.Load(xmlReader) as FlowDocument; 

//Now print using PrintDialog 
var pd = new PrintDialog(); 

if (pd.ShowDialog().Value) 
{ 
    CloneDoc.PageHeight = pd.PrintableAreaHeight; 
    CloneDoc.PageWidth = pd.PrintableAreaWidth; 
    IDocumentPaginatorSource idocument = CloneDoc as IDocumentPaginatorSource; 

    pd.PrintDocument(idocument.DocumentPaginator, "Printing FlowDocument"); 
} 
संबंधित मुद्दे