2009-08-12 8 views
5

मेरे पास फ्लो डॉक्यूमेंट है जो ब्लॉकयूकॉन्टेनर में आइटम्स कंट्रोल के कारण ऊंचाई में भिन्न होता है। कुछ मामलों में, आइटम कंट्रोल पृष्ठ की ऊंचाई से परे फैली हुई है। यदि आवश्यक हो तो मुद्रण से ठीक पहले पृष्ठ (8.5 "एक्स 11") फिट करने के लिए FlowDocument को स्केल करने का कोई तरीका है?फ़िट करने के लिए डब्ल्यूपीएफ फ़्लो डॉक्यूमेंट स्केल

अभी के रूप में, FlowDocument 'दस्तावेज़' नाम दिया गया है और मुद्रण के लिए विधि मैं उपयोग कर रहा हूँ है:

private void Print_Click(object sender, RoutedEventArgs e) 
    { 

     PrintDialog pd = new PrintDialog(); 
     doc.PageHeight = pd.PrintableAreaHeight; 
     doc.PageWidth = pd.PrintableAreaWidth; 
     doc.ColumnGap = 0; 
     doc.ColumnWidth = pd.PrintableAreaWidth; 
     IDocumentPaginatorSource dps = doc; 
     pd.PrintDocument(dps.DocumentPaginator, "Sheet"); 
    } 
+0

क्या आपको इस समस्या का समाधान मिला? –

+0

नहीं, मुझे अभी भी नहीं मिला है। मुझे आइटम्स कंट्रोल के अंदर एक रैपपनेल बसने और जगह लेनी पड़ी जो कि मैं शुरुआत से ही करना चाहता था। – Johnathan1

उत्तर

3

मैं जानता हूँ कि यह थोड़ा देर हो चुकी है, लेकिन यहाँ समाधान मैं के साथ आया है।

सबसे पहले, हम एक रैपर बनाते हैं जो हमारे लिए दस्तावेज़ पेज जेनरेट करेगा। प्रत्येक पृष्ठ पर इसे वापस करने से पहले एक पैमाने पर परिवर्तन लागू किया जाएगा। ,

private void PrintDocument(PrintDialog pd, FlowDocument document, double scale, string title) 
    { 
     DocumentPaginator dp = ((IDocumentPaginatorSource)document).DocumentPaginator; 
     FittedDocumentPaginator fdp = new FittedDocumentPaginator(dp, scale); 

     pd.PrintDocument(fdp, title); 
    } 

आप रुचि रखते हैं, यहाँ हम कैसे पैमाने निर्धारित है:

public class FittedDocumentPaginator : DocumentPaginator 
{ 
    public DocumentPaginator Base { get; private set; } 
    public double Scale { get; private set; } 
    private readonly ScaleTransform _sTransform; 

    public FittedDocumentPaginator(DocumentPaginator baseDp, double scale) 
    { 
     if (baseDp == null) 
      throw new ArgumentNullException("baseDp"); 

     Base = baseDp; 
     Scale = scale; 
     _sTransform = new ScaleTransform(scale, scale); 
    } 

    public override DocumentPage GetPage(int pageNumber) 
    { 
     var page = Base.GetPage(pageNumber); 
     ((ContainerVisual)page.Visual).Transform = _sTransform; 

     return page; 
    } 

    public override bool IsPageCountValid 
    { 
     get { return Base.IsPageCountValid; } 
    } 

    public override int PageCount 
    { 
     get { return Base.PageCount; } 
    } 

    public override Size PageSize 
    { 
     get { return Base.PageSize; } 
     set { Base.PageSize = value; } 
    } 

    public override IDocumentPaginatorSource Source 
    { 
     get { return Base.Source; } 
    } 
} 

इसका इस्तेमाल करते हुए काफी सरल है। हमारे मामले में, दस्तावेज़ पृष्ठ चौड़ाई से पहले बढ़ाया गया था, लेकिन पृष्ठ की ऊंचाई को समायोजित करने के लिए इसे आसानी से संशोधित किया जा सकता है।

private void Print(FlowDocument document, string title, double documentWidth) 
    { 
     var pd = new PrintDialog(); 

     if (pd.ShowDialog() == true) 
     { 
      // Set the printing margins. 
      Thickness pageMargins = document.PagePadding; 
      document.PagePadding = new Thickness(15.0); 

      // In our case, the document's width is NaN so we have to navigate the visual tree to get the ActualWidth, which is represented by 'documentWidth'. 
      double scale = documentWidth/pd.PrintableAreaWidth; 

      if (scale < 1.0) 
      { 
       // The report fits on the page just fine. Don't scale. 
       scale = 1.0; 
      } 

      double invScale = 1/scale; 

      document.PageHeight = pd.PrintableAreaHeight * scale; 
      document.PageWidth = pd.PrintableAreaWidth * scale; 

      DocumentPaginator dp = ((IDocumentPaginatorSource)document).DocumentPaginator; 
      FittedDocumentPaginator fdp = new FittedDocumentPaginator(dp, invScale); 

      pd.PrintDocument(fdp, title); 

      // Restore the original values so the GUI isn't altered. 
      document.PageHeight = Double.NaN; 
      document.PageWidth = Double.NaN; 
      document.PagePadding = pageMargins; 
     } 
    } 
संबंधित मुद्दे

 संबंधित मुद्दे