2010-03-22 14 views
12

Tôi đang cố tạo một tài liệu XPS từ một điều khiển WPF. Công việc in ấn cho đến nay, nhưng tôi không thể tìm thấy cách để tạo XPS ở chế độ ngang.WPF đến XPS theo hướng ngang

Mã của tôi để tạo ra các tập tin XPS, chủ yếu là lấy từ một trang SO

public FixedDocument ReturnFixedDoc() 
    { 
     FixedDocument fixedDoc = new FixedDocument(); 
     PageContent pageContent = new PageContent(); 
     FixedPage fixedPage = new FixedPage(); 

     var ctrl = new controlToPrint(); 

     //Create first page of document 
     fixedPage.Children.Add(ctrl); 
     ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage); 
     fixedDoc.Pages.Add(pageContent); 
     //Create any other required pages here 

     return fixedDoc; 
    } 


    public void SaveCurrentDocument() 
    { 
     // Configure save file dialog box 
     Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); 
     dlg.FileName = "MyReport"; // Default file name 
     dlg.DefaultExt = ".xps"; // Default file extension 
     dlg.Filter = "XPS Documents (.xps)|*.xps"; // Filter files by extension 

     // Show save file dialog box 
     Nullable<bool> result = dlg.ShowDialog(); 

     // Process save file dialog box results 
     if (result == true) 
     { 
      // Save document 
      string filename = dlg.FileName; 

      FixedDocument doc = ReturnFixedDoc(); 
      XpsDocument xpsd = new XpsDocument(filename, FileAccess.Write); 
      System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd); 
      xw.Write(doc); 
      xpsd.Close(); 
     } 
    } 

Any help is appreciated. thiết lập kích thước của FixedPage của bạn trong ReturnFixedDoc

Trả lời

12

Hãy thử:

// hard coded for A4 
fixedPage.Width = 11.69 * 96; 
fixedPage.Height = 8.27 * 96; 

Những con số ở dạng (inch) x (dots per inch). 96 là DPI của WPF. Tôi đã sử dụng kích thước của trang A4.

+0

Cảm ơn bạn, điều này dường như thực hiện thủ thuật. Tôi đã sử dụng "RotateTransform mới (90)" để xoay điều khiển nhưng thay đổi kích thước trang thành kích thước phù hợp là tốt hơn :-) – Felix