Search code examples
c#printingprintdocumentironpdf

IronPdf Print to Margin


I try to print a pdf with IronPdf from html but the result leaves a border. Is there a way to set Fit-To-Page in my PrintDocument?

Here my code:

        public static void PrintDocument(string printer, bool landscape, PdfDocument pdfDocument, Duplexing duplex)
        {
            var printDocument = pdfDocument.GetPrintDocument();

            printDocument.PrinterSettings.PrinterName = printer;
            printDocument.DefaultPageSettings.Landscape = landscape;
            printDocument.PrinterSettings.Duplex = DuplexMapping(duplex);
            printDocument.PrinterSettings.DefaultPageSettings.PaperSize.RawKind = (int)PaperKind.A4;

            printDocument.Print();
        }

Solution

  • Thanks to IronPdf-Support I found a solution:

                // enable javascript 
                var renderer = new IronPdf.HtmlToPdf();
                renderer.PrintOptions.EnableJavaScript = true;
                renderer.PrintOptions.RenderDelay = 500; //milliseconds
                renderer.PrintOptions.CssMediaType = IronPdf.PdfPrintOptions.PdfCssMediaType.Screen;
    
                renderer.PrintOptions.MarginTop = 0;
                renderer.PrintOptions.MarginBottom = 0;
                renderer.PrintOptions.MarginLeft = 0;
                renderer.PrintOptions.MarginRight = 0;
    

    Set Margin to 0.