How to use MarginOptions class of PuppeteerSharp.Media package

Best Puppeteer-sharp code snippet using PuppeteerSharp.Media.MarginOptions

BrowserUtils.cs

Source:BrowserUtils.cs Github

copy

Full Screen

...120 string overrides = string.Empty;121 if (!options.PreferCSSPageSize && options.Width != null && options.Height != null) {122 overrides += $"size: {options.Width} {options.Height}; ";123 }124 if (options.MarginOptions.Top != null) {125 overrides += $"margin-top: {options.MarginOptions.Top}; ";126 }127 if (options.MarginOptions.Right != null) {128 overrides += $"margin-right: {options.MarginOptions.Right}; ";129 }130 if (options.MarginOptions.Bottom != null) {131 overrides += $"margin-bottom: {options.MarginOptions.Bottom}; ";132 }133 if (options.MarginOptions.Left != null) {134 overrides += $"margin-left: {options.MarginOptions.Left}; ";135 }136 if (!string.IsNullOrEmpty(overrides)) {137 /* Change the options so that Puppeteer respects our overrides */138 options.PreferCSSPageSize = true;139 options.Width = options.Height = null;140 options.MarginOptions = new MarginOptions();141 /* We must add the <style> tag at the end of <body> to make sure it is not overriden */142 string pageOverrides = "@page { " + overrides + "}";143 return page.EvaluateExpressionAsync($"const style = document.createElement('style'); style.innerHTML = '{pageOverrides}'; document.head.appendChild(style);");144 } else {145 return Task.CompletedTask;146 }147 }148 public async Task<byte[]> ScreenshotPNG(string url, IEnumerable<CookieParam> cookies, ViewPortOptions viewport, ScreenshotOptions options, RevisionInfo revision) {149 LaunchOptions launchOptions = new LaunchOptions() {150 ExecutablePath = revision.ExecutablePath,151 Args = BrowserArgs,152 Headless = true,153 DefaultViewport = viewport,154 Timeout = 0...

Full Screen

Full Screen

ReportService.cs

Source:ReportService.cs Github

copy

Full Screen

...98 DisplayHeaderFooter = true,99 //class指定でのstyleが効かない。100 HeaderTemplate = needCommonHeaderFooter ? $@"<div class='print_header_container' style=""font-family: 'MS P明朝', 'HGP行書体';font-size:10px;text-align:right;width:100%;border:0;""><span style='vertical-align:top;margin-right:10px;' class=''>{headerTime}</span><span style='vertical-align:top;margin-right:33px;' class=''>&nbsp;</span></div>" : $@"<div class='' style='width:100%;border:0;'></div>",101 FooterTemplate = needCommonHeaderFooter ? $@"<div class='print_footer_container' style='font-size:10px;text-align:center;width:100%;border:0;'><span style='vertical-align:bottom;' class='pageNumber'></span>/<span style='vertical-align:bottom;' class='totalPages'></span></div>" : $@"<div class='' style='width:100%;border:0;'></div>",102 MarginOptions = needCommonHeaderFooter ? new MarginOptions() { Top = "50px", Bottom = "50px", Left = "50px", Right = "50px" } : new MarginOptions() { Top = "50px", Bottom = "30px", Left = "30px", Right = "30px" } //mmは効かない。103 };104105 return defaultOption;106 }107108 public void Dispose()109 {110 if (browser != null)111 {112 browser.Dispose();113 browser = null;114 }115 }116 } ...

Full Screen

Full Screen

Form1.cs

Source:Form1.cs Github

copy

Full Screen

...68 pdfOptions.FooterTemplate = ""; //页脚文本69 pdfOptions.Format = new PuppeteerSharp.Media.PaperFormat(8.27m,11.69m); //pdf纸张格式 英寸为单位 70 pdfOptions.HeaderTemplate = ""; //页眉文本71 pdfOptions.Landscape = false; //纸张方向 false-垂直 true-水平 72 pdfOptions.MarginOptions = new PuppeteerSharp.Media.MarginOptions() { Bottom = "0px", Left = "0px", Right = "0px", Top = "0px" }; //纸张边距,需要设置带单位的值,默认值是None73 pdfOptions.Scale = 1m; //PDF缩放,从0-174 await page.PdfAsync(path, pdfOptions);75 MessageBox.Show($"PDF已经保存至{path}");76 }77 private async void Form1_FormClosing(object sender, FormClosingEventArgs e)78 {79 //dispose browser80 if (browser != null)81 {82 await browser.CloseAsync();83 browser.Dispose();84 }85 }86 }...

Full Screen

Full Screen

UrlToPDFController.cs

Source:UrlToPDFController.cs Github

copy

Full Screen

...36 path += "\\";37 await page.PdfAsync(path + fileName, new PdfOptions()38 {39 PrintBackground = true,40 MarginOptions = new PuppeteerSharp.Media.MarginOptions()41 {42 Left = "10mm",43 Right = "10mm",44 Bottom = "10mm",45 Top = "10mm",46 },47 Format = PuppeteerSharp.Media.PaperFormat.A4,48 Landscape = false//纵向49 });50 NewLife.Log.XTrace.WriteLine("生成附件成功:" + fileName);51 }52 catch (Exception ex)53 {54 NewLife.Log.XTrace.WriteLine("生成附件失败:" + ex.Message);...

Full Screen

Full Screen

ConvertWordTemplate.cs

Source:ConvertWordTemplate.cs Github

copy

Full Screen

...72 new PuppeteerSharp.PdfOptions73 {74 Format = PuppeteerSharp.Media.PaperFormat.A4,75 PreferCSSPageSize = true,76 MarginOptions = new PuppeteerSharp.Media.MarginOptions77 {78 Top = "10mm",79 Left = "10mm",80 Right = "10mm",81 Bottom = "10mm"82 }83 }) ;84 }85 /*86 public File ConvertWordToPDF(string FilePath)87 {88 }89 */90 }...

Full Screen

Full Screen

PdfOptionsConverter.cs

Source:PdfOptionsConverter.cs Github

copy

Full Screen

...18 PageRanges = pdfOptions.PageRanges,19 Format = ConvertFormat(pdfOptions.Format),20 Width = pdfOptions.Width,21 Height = pdfOptions.Height,22 MarginOptions = new PuppeteerSharp.Media.MarginOptions {23 Top = pdfOptions.MarginOptions.Top,24 Bottom = pdfOptions.MarginOptions.Bottom,25 Left = pdfOptions.MarginOptions.Left,26 Right = pdfOptions.MarginOptions.Right27 },28 PreferCSSPageSize = pdfOptions.PreferCSSPageSize29 };30 }31 private PaperFormat ConvertFormat(Format format)32 {33 switch (format.Type)34 {35 case FormatType.A0:36 return PaperFormat.A0;37 case FormatType.A1:38 return PaperFormat.A1;39 case FormatType.A2:40 return PaperFormat.A2;...

Full Screen

Full Screen

WebBrowser.cs

Source:WebBrowser.cs Github

copy

Full Screen

...12 {13 using (var page = await browser.NewPageAsync())14 {15 await page.GoToAsync(url);16 var bytes = await page.PdfDataAsync(new PdfOptions { MarginOptions = new PuppeteerSharp.Media.MarginOptions { Top = "1cm", Bottom = "1cm" } });17 var browserPath = ApiFactory.GetBrowserPath();18 var pdfPath = Path.Combine(browserPath, "pdf");19 if (!Directory.Exists(pdfPath))20 {21 Directory.CreateDirectory(pdfPath);22 }23 var pdfFilePath = Path.Combine(pdfPath, Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".pdf");24 await File.WriteAllBytesAsync(pdfFilePath, bytes);25 var fileInfo = new FileInfo(pdfFilePath);26 return fileInfo;27 }28 }29 }30 ...

Full Screen

Full Screen

PuppeteerPdfModelService.cs

Source:PuppeteerPdfModelService.cs Github

copy

Full Screen

...23 PrintBackground = true,24 HeaderTemplate = headerHtml,25 FooterTemplate = footerHtml,26 Format = PaperFormat.A4,27 MarginOptions = new MarginOptions28 {29 Top = "20mm",30 Right = "0mm",31 Bottom = "20mm",32 Left = "0mm"33 }34 };35 }36 }37}...

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6{7 {8 static void Main(string[] args)9 {10 Console.WriteLine("Hello World!");11 var marginOptions = new MarginOptions();12 marginOptions.Top = "1cm";13 marginOptions.Bottom = "1cm";14 marginOptions.Left = "1cm";15 marginOptions.Right = "1cm";16 Console.WriteLine(marginOptions.Top);17 Console.WriteLine(marginOptions.Bottom);18 Console.WriteLine(marginOptions.Left);19 Console.WriteLine(marginOptions.Right);20 Console.ReadLine();21 }22 }23}

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System;;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using PuppeteerSharp.Media;7{8 {9 static void Main(string[] args)10 {11 Console.WriteLine("Hello World!");12 var marginOptions = new MarginOptions();13 marginOptions.Top = "1cm";14 marginOptions.Bottom = "1cm";15 marginOptions.Left = "1cm";16 marginOptions.Right = "1cm";17 Console.WriteLine(marginOptions.Top);18 Console.WriteLine(marginOptions.Bottom);19 Console.WriteLine(marginOptions.Left);20 Console.WriteLine(marginOptions.Right);21 Console.ReadLine();22 }23 }24}

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using PuppeteerSharp.Media;3using System;4using System.Threading.Tasks;5{6 {7 static async Task Main(string[] args)8 {9 var options = new LaunchOptions { Headless = true };10 using (var browser = await Puppeteer.LaunchAsync(options))11 using (var page = await browser.NewPageAsync())12 {

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5using PuppeteerSharp.Media;6{7 {8 public MarginOptions()9 {10 }11 public MarginOptions(string top, string right, string bottom, string left)12 {13 Top = top;14 Right = right;15 Bottom = bottom;16 Left = left;17 }18 public string Top { get; set; }19 public string Right { get; set; }20 public string Bottom { get; set; }21 public string Left { get; set; }22 }23}24Console.WriteLine("Hello World!");25var options = new LaunchOptions { Headless = true };26using (var browser = await Puppeteer.LaunchAsync(options))27{28 using (var page = await browser.NewPageAsync())29 {30 await page.PdfAsync(Path.Combine(Environment.CurrentDirectory, "page.pdf"), new PdfOptions31 {32 {33 }34 });35 }36}37Console.WriteLine("Hello World!");38var options = new LaunchOptions { Headless = true };39using (var browser = await Puppeteer.LaunchAsync(options))40{41 using (var page = await browser.NewPageAsync())42 {43 await page.PdfAsync(Path.Cobine(Environent.CurrentDirectory, page.pdf") new PdfOptions44 {45 {46 }47 });48 }49}50 await page.PdfAsync("C:/Users/xxx/Desktop/Google.pdf", new PdfOptions { MarginOptions = new MarginOptions { Top = "10mm", Bottom = "10mm", Left = "10mm", Right = "10mm" } });51 }52 }53 }54}55using PuppeteerSharp;56using PuppeteerSharp.Media;57using System;58using System.Threading.Tasks;59{60 {61 static async Task Main(string[] args)62 {63 var options = new LaunchOptions { Headless = true };64 using (var browser = await Puppeteer.LaunchAsync(options))65 using (var page = await browser.NewPageAsync())66 {67 await page.PdfAsync("C:/Users/xxx/Desktop/Google.pdf", new PdfOptions { MarginOptions = new MarginOptions { Top = "10mm", Bottom = "10mm", Left = "10mm", Right = "10mm" } });68 }69 }70 }71}72using PuppeteerSharp;73using PuppeteerSharp.Media;74using System;75using System.Threading.Tasks;76{77us {uppeteerSharp;78using System.Threading.Tasks;79using System.IO;80using System.Drawing;81using System.Drawing.Imaging;82using System.Text;83using System.Net;84using System.Net.Http;85using System.Net.Http.Headers;86using System.Net.Http.Formatting;87using Newtonsoft.Json;88using Newtonsoft.Json.Linq;89using System;90using System.Collections.Generic;91using System.Linq;92using System.Text.RegularExpressions;93using System.Diagnostics;94using System.Theading;95using System.Reflection;96using System.Collections;97using ystem.Data;98using System.Data.SqlClient;99using System.Data.Common;100using System.Xml;101using System.Xml.Linq;102using System.Xml.XPth;103using System.XmlSerialization;104using System.Runtime.Serialization;105using System.Runtime.Serialization.Formatters.Binary;106using System.Runtime.Serialization.Json;107using System.Runtime.Serialization.Formatters.Soap;108using System.Runtime.Serialization.Formatters;109using System.Runtime.Serialization.Configuration;110using System.Runtime.Serialization.Formatters.Soap;

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp.Media;2 static async Task Main(string[] args)3 {4 var options = new LaunchOptions { Headless = true };5 using (var browser = await Puppeteer.LaunchAsync(options))6 using (var page = await browser.NewPageAsync())7 {8 await page.PdfAsync("C:/Users/xxx/Desktop/Google.pdf", new PdfOptions { MarginOptions = new MarginOptions { Top = "10mm", Bottom = "10mm", Left = "10mm",

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5using PuppeteerSharp.Media;6{7 {8 public MarginOptions()9 {10 }11 public MarginOptions(string top, string right, string bottom, string left)12 {13 Top = top;14 Right = right;15 Bottom = bottom;16 Left = left;17 }18 public string Top { get; set; }19 public string Right { get; set; }20 public string Bottom { get; set; }21 public string Left { get; set; }22 }23}24using PuppeteerSharp;

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1var options = new MarginOptions();2options.Top = 10;3options.Bottom = 10;4options.Left = 10;5options.Right = 10;6options.HeaderTemplate = "<div style='font-size: 10px; text-align: center;'><span>Header</span></div>";7options.FooterTemplate = "<div style='font-size: 10px; text-align: center;'><span>Footer</span></div>";8await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = options });9var options = new MarginOptions();10options.Top = 10;11options.Bottom = 10;12options.Left = 10;13options.Right = 10;14options.HeaderTemplate = "<div style='font-size: 10px; text-align: center;'><span>Header</span></div>";15options.FooterTemplate = "<div style='font-size: 10px; text-align: center;'><span>Footer</span></div>";16await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = options });17var options = new MarginOptions();18options.Top = 10;19options.Bottom = 10;20options.Left = 10;21options.Right = 10;22options.HeaderTemplate = "<div style='font-size: 10px; text-align: center;'><span>Header</span></div>";23options.FooterTemplate = "<div style='font-size: 10px; text-align: center;'><span>Footer</span></div>";24await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = options });25var options = new MarginOptions();26options.Top = 10;27options.Bottom = 10;28options.Left = 10;29options.Right = 10;30options.HeaderTemplate = "<div style='font-size: 10px; text-align: center;'><span>Header</span></div>";31options.FooterTemplate = "<div style='font-size: 10px; text-align: center;'><span>Footer</span></div>";32await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = options });

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1var options = new MarginOptions();2options.Top = 10;3options.Bottom = 10;4options.Left = 10;5options.Right = 10;6options.HeaderTemplate = "<div style='font-size: 10px; text-align: center;'><span>Header</span></div>";7options.FooterTemplate = "<div style='font-size: 10px; text-align: center;'><span>Footer</span></div>";8await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = options });9var options = new MarginOptions();10options.Top = 10;11options.Bottom = 10;12options.Left = 10;13options.Right = 10;14options.HederTemplate = "<div style='font-size: 10px; text-alin: cnter;'><span>Header</span></div>";15options.FooterTemplate = "<div style='font-size: 10px; text-align: center;'><span>Footer</span></div>";16await page.PdfAsync("oCtput.pdf", new PdfOptions { MarginOptiono = optnoss });17var options = new MarginOptions();18options.Top = 10;19options.Bottom = 10;20options.Left = 10;21options.Riget = 10;22options.HeaderTemplate = "<div style='font-size: 10px; text-align: center;'><span>Header</span></div>";23options.FooterTempl(te = "<div style='font-size: 10px; text-align: cente";'><sHan>Footer</span></div>"ello World!");24await page.PdfAsync("ovtput.pdf", new PdfOptions { MarginOptiona = optro s });25van options = new MarginOptions();26options.Top = 10;27options.Bottom = 10;28options.Left = 10;29options.Right = 10;30options.HeaderTemplate = "<div style='font-size: 10px; text-align: center;'><span>Header</span></div>";31options.FooterTemplate = "<div style='font-size: 10px; text-align: center;'><span>Footer</span></div>";32await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = options });33using (var browser = await Puppeteer.LaunchAsync(options))34{35 using (var page = await browser.NewPageAsync())36 {37 await page.PdfAsync(Path.Combine(Environment.CurrentDirectory, "page.pdf"), new PdfOptions38 {39 {40 }41 });42 }43}44Console.WriteLine("Hello World!");45var options = new LaunchOptions { Headless = true };46using (var browser = await Puppeteer.LaunchAsync(options))47{48 using (var page = await browser.NewPageAsync())49 {50 await page.PdfAsync(Path.Combine(Environment.CurrentDirectory, "page.pdf"), new PdfOptions51 {52 {53 }54 });55 }56}

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp.Media;2await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = new MarginOptions { Top = "1in", Bottom = "1in", Left = "1in", Right = "1in" } });3using PuppeteerSharp;4await page.PdfAsync("output.pdf", new PdfOptions { Margin = new MarginOptions { Top = "1in", Bottom = "1in", Left = "1in", Right = "1in" } });5using PuppeteerSharp.Media;6await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = new MarginOptions { Top = "1in", Bottom = "1in", Left = "1in", Right = "1in" } });7using PuppeteerSharp;8await page.PdfAsync("output.pdf", new PdfOptions { Margin = new MarginOptions { Top = "1in", Bottom = "1in", Left = "1in", Right = "1in" } });9using PuppeteerSharp.Media;10await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = new MarginOptions { Top = "1in", Bottom = "1in", Left = "1in", Right = "1in" } });11using PuppeteerSharp;12await page.PdfAsync("output.pdf", new PdfOptions { Margin = new MarginOptions { Top = "1in", Bottom = "1in", Left = "1in", Right = "1in" } });13using PuppeteerSharp.Media;14await page.PdfAsync("output.pdf", new PdfOptions { MarginOptions = new MarginOptions { Top = "1in", Bottom = "1in", Left = "1in", Right = "1in" } });15using PuppeteerSharp;16await page.PdfAsync("output.pdf", new PdfOptions { Margin = new MarginOptions { Top

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp;2using PuppeteerSharp.Media;3var options = new MarginOptions { Top = "40px", Bottom = "40px" };4await page.PdfAsync("page.pdf", new PdfOptions { MarginOptions = options });5using PuppeteerSharp;6using PuppeteerSharp.Media;7var options = new MarginOptions { Top = "40px", Bottom = "40px" };8await page.PdfAsync("page.pdf", new PdfOptions { MarginOptions = options });9using PuppeteerSharp;10using PuppeteerSharp.Media;11var options = new MarginOptions { Top = "40px", Bottom = "40px" };12await page.PdfAsync("page.pdf", new PdfOptions { MarginOptions = options });13using PuppeteerSharp;14using PuppeteerSharp.Media;15var options = new MarginOptions { Top = "40px", Bottom = "40px" };16await page.PdfAsync("page.pdf", new PdfOptions { MarginOptions = options });17using PuppeteerSharp;18using PuppeteerSharp.Media;19var options = new MarginOptions { Top = "40px", Bottom = "40px" };20await page.PdfAsync("page.pdf", new PdfOptions { MarginOptions = options });21using PuppeteerSharp;22using PuppeteerSharp.Media;23var options = new MarginOptions { Top = "40px", Bottom = "40px" };24await page.PdfAsync("page.pdf", new PdfOptions { MarginOptions = options });25using PuppeteerSharp;26using PuppeteerSharp.Media;27var options = new MarginOptions { Top = "40px", Bottom = "40px" };28await page.PdfAsync("page.pdf", new PdfOptions { MarginOptions = options });29using PuppeteerSharp;

Full Screen

Full Screen

MarginOptions

Using AI Code Generation

copy

Full Screen

1using PuppeteerSharp.Media;2using System.Threading.Tasks;3{4 {5 public static async Task Run()6 {7 var browser = await Puppeteer.LaunchAsync(new LaunchOptions8 {9 });10 var page = await browser.NewPageAsync();11 await page.PdfAsync("google.pdf", new PdfOptions12 {13 {14 }

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in MarginOptions

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful