0

我使用 pdfiumViewer 打印由 itextshape 创建的标签,我在 stackoverflow 中找到了此代码,它适用于 A4 纸,但对于像我的标签这样的自定义纸张,问题将会发生。这是代码:

public static void PrintPDF(string printer, string paperName, string filename, int copies, bool isduplex = false, bool isHorizontal = false, bool printLabel = false)
    {
        try
        {    // Create the printer settings for our printer
            var printerSettings = new PrinterSettings
            {
                PrinterName = printer,
                Copies = (short)copies,
                Duplex = Duplex.Simplex,

            };

            if (isduplex && printerSettings.CanDuplex && isHorizontal)
            {
                printerSettings.Duplex = Duplex.Horizontal;
            }

            if (isduplex && printerSettings.CanDuplex && isHorizontal == false)
            {
                printerSettings.Duplex = Duplex.Vertical;
            }
            // Create our page settings for the paper size selected
            var pageSettings = new PageSettings(printerSettings)
            {
            };


            if (printLabel == true)
            {
                PaperSize paper = new PaperSize("label", 460, 260);
                pageSettings.PaperSize = paper;
                pageSettings.Margins = new Margins(0, 0, 0, 0);
            }
            else
            {
                foreach (PaperSize paperSize in printerSettings.PaperSizes)
                {
                    if (paperSize.PaperName == paperName)
                    {
                        pageSettings.PaperSize = paperSize;
                        break;
                    }
                }

            }

            // Now print the PDF document
            if (printerSettings.IsValid)
            {
                using (var document = PdfiumViewer.PdfDocument.Load(filename))
                {
                    using (var printDocument = document.CreatePrintDocument(PdfiumViewer.PdfPrintMode.CutMargin))
                    {
                        printDocument.PrintController = new StandardPrintController();
                        printDocument.OriginAtMargins = true;
                        printDocument.PrinterSettings = printerSettings;
                        printDocument.DefaultPageSettings = pageSettings;
                        printDocument.Print();
                    }
                }
            }
        }
        catch 
        {
            throw;
        }
    }

问题是 HardMarginX 总是 20。它是只读属性,所以我无法更改。所以当我打印时,纸张总是留有一些空间。所以无论如何我可以解决这个问题。感谢阅读

4

1 回答 1

1
        PaperSize paperSize = new PaperSize("Test", 315, 300);
        paperSize.RawKind = (int)PaperKind.Custom;

使用此代码

于 2020-02-15T05:14:19.720 回答