使用PdfiumViewer更改用于打印PDF文件的HardMarginX

问题描述 投票:0回答:1

我使用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。它是只读属性,因此我无法更改。因此,当我打印时,纸张始终留有一定的余量。所以无论如何我都可以解决这个问题。感谢阅读

c# itext pdfium
1个回答
1
投票
        PaperSize paperSize = new PaperSize("Test", 315, 300);
        paperSize.RawKind = (int)PaperKind.Custom;

使用此代码

© www.soinside.com 2019 - 2024. All rights reserved.