2013-04-17 135 views

回答

0

我想通了。您需要考慮打印機的dpi。

獲取打印機的默認頁面設置:

 PrinterSettings ps = new PrinterSettings(); 
     ps.PrinterName = printerName; 
     this.defaultPageSettings = ps.DefaultPageSettings; 

構建了DeviceInfo的Xml與信息:

private string BuildDeviceInfo() 
    { 
     StringBuilder returnValue; 
     System.Xml.XmlWriter writer; 

     returnValue = new StringBuilder(1024); 
     writer = System.Xml.XmlWriter.Create(returnValue); 

     writer.WriteStartElement("DeviceInfo"); 
     writer.WriteElementString("OutputFormat", "EMF"); 

     if (defaultPageSettings != null) 
     { 
      // DPI will keep the output from scaling in weird ways 
      writer.WriteElementString("PrintDpiX", defaultPageSettings.PrinterResolution.X.ToString()); 
      writer.WriteElementString("PrintDpiY", defaultPageSettings.PrinterResolution.Y.ToString()); 

      writer.WriteElementString("PageWidth", (defaultPageSettings.PaperSize.Width/100m).ToString("F2") + "in"); 
      writer.WriteElementString("PageHeight", (defaultPageSettings.PaperSize.Height/100m).ToString("F2") + "in"); 
     } 
     writer.Close(); 

     return returnValue.ToString(); 
    } 
+0

工作的?可能不會... – RolandoCC

0

這爲我工作。我正在從WPF應用打印到STAR TSP100收據打印機。當我自己設置頁面大小頁邊距時,頁面大小非常大,但是當我計算最小頁邊距和頁面大小以及從默認打印機設置中設置dpi時,收據打印正確。

我用這個要弄清楚的最低按金:(H/T http://www.dreamincode.net/forums/topic/135864-printing-with-minimum-margins-specified-by-the-printer/

Dim minimumMarginLeft, minimumMarginRight, minimumMarginTop, 
minimumMarginBottom As Single 

minimumMarginLeft = PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Left 

minimumMarginRight = PrintDialog1.PrinterSettings.DefaultPageSettings.PaperSize.Width - _ 
            PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Right 

minimumMarginTop = PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Top 

minimumMarginBottom = PrintDialog1.PrinterSettings.DefaultPageSettings.PaperSize.Height - _ 
             PrintDialog1.PrinterSettings.DefaultPageSettings.PrintableArea.Bottom 


-I figured this out. You need to account for the printer dpi. 

-Get the Printer Default Page Settings: 

     PrinterSettings ps = new PrinterSettings(); 
     ps.PrinterName = printerName; 
     this.defaultPageSettings = ps.DefaultPageSettings; The Build the DeviceInfo Xml with that info: 

    private string BuildDeviceInfo() 
    { 
     StringBuilder returnValue; 
     System.Xml.XmlWriter writer; 

     returnValue = new StringBuilder(1024); 
     writer = System.Xml.XmlWriter.Create(returnValue); 

     writer.WriteStartElement("DeviceInfo"); 
     writer.WriteElementString("OutputFormat", "EMF"); 

     if (defaultPageSettings != null) 
     { 
      // DPI will keep the output from scaling in weird ways 
      writer.WriteElementString("PrintDpiX", defaultPageSettings.PrinterResolution.X.ToString()); 
      writer.WriteElementString("PrintDpiY", defaultPageSettings.PrinterResolution.Y.ToString()); 

      writer.WriteElementString("PageWidth", (defaultPageSettings.PaperSize.Width/100m).ToString("F2") + "in"); 
      writer.WriteElementString("PageHeight", (defaultPageSettings.PaperSize.Height/100m).ToString("F2") + "in"); 
     } 
     writer.Close(); 

     return returnValue.ToString(); 
    }