0

我正在使用 LocalReport 打印一张 3 英寸的收据,其中的代码来自“演练:打印没有预览的本地报告

有些打印机要求 DeviceInfo PageWidth 为 8.5 英寸才能正常工作,有些打印机需要 3.0 英寸。似乎报告正在被拉伸以填充错误大小的页面。我尝试调整报告纸张大小和打印机纸张大小,但似乎无法获得正确的组合。

有人经历过吗?

4

2 回答 2

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();
    }
于 2013-05-07T09:05:08.290 回答
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();
    }
于 2015-04-10T11:42:26.970 回答