Comment utiliser les options de rendu
Les options de rendu dans la génération de PDF se réfèrent aux paramètres et configurations qui déterminent comment un document PDF est créé, affiché et imprimé. Ces options peuvent inclure une large gamme de paramètres, tels que le rendu des éléments des champs de formulaire, l'activation de JavaScript, la génération d'une table des matières, l'ajout d'en-têtes et de pieds de page, l'ajustement des marges, la définition de la taille du papier PDF, et plus encore.
La classe ChromePdfRenderer dans IronPDF offre diverses options de rendu qui permettent aux utilisateurs de personnaliser la génération des PDF. Il comprend PaperFit, un gestionnaire qui contrôle la disposition du contenu sur les pages PDF, offrant différents styles de mise en page tels que les mises en page CSS3 réactives ou le flux continu.
Commencez avec IronPDF
Commencez à utiliser IronPDF dans votre projet dès aujourd'hui avec un essai gratuit.
Comment utiliser les options de rendu
- Téléchargez la bibliothèque C# pour utiliser les options de rendu
- Préparez les ressources ou le fichier HTML à convertir en document PDF.
- Instancier la classe ChromePdfRenderer pour rendre le PDF
- Accédez directement à la propriété RenderingOptions pour configurer vos paramètres
- Rendre le PDF dans le format souhaité
Exemple d'options de rendu
Bien que de nombreuses propriétés d'options de rendu soient spécifiquement conçues pour la conversion HTML en PDF, elles peuvent également être utilisées dans d'autres types de conversions PDF. Rendons la syntaxe Markdown en PDF et utilisons les options de rendu pour configurer le résultat PDF.
:path=/static-assets/pdf/content-code-examples/how-to/rendering-options-render.cs
using IronPdf;
// Instantiate Renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Configure rendering options
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
HtmlFragment = "<h1>Header Content</h1>"
};
renderer.RenderingOptions.SetCustomPaperSizeinMilimeters(150, 150);
renderer.RenderingOptions.MarginTop = 0;
// Markdown string
string md = "This is some **bold** and *italic* text.";
// Render from markdown string
PdfDocument pdf = renderer.RenderMarkdownStringAsPdf(md);
// Save the PDF
pdf.SaveAs("renderingOptions.pdf");
Imports IronPdf
' Instantiate Renderer
Private renderer As New ChromePdfRenderer()
' Configure rendering options
renderer.RenderingOptions.PrintHtmlBackgrounds = True
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {.HtmlFragment = "<h1>Header Content</h1>"}
renderer.RenderingOptions.SetCustomPaperSizeinMilimeters(150, 150)
renderer.RenderingOptions.MarginTop = 0
' Markdown string
Dim md As String = "This is some **bold** and *italic* text."
' Render from markdown string
Dim pdf As PdfDocument = renderer.RenderMarkdownStringAsPdf(md)
' Save the PDF
pdf.SaveAs("renderingOptions.pdf")
Toutes les options de rendu
Nous disposons d'options avancées qui définissent les options de rendu du PDF, comme l'ajustement des marges,
l'orientation du papier, le format du papier, etc.
Le tableau ci-dessous illustre les nombreuses options possibles.
Class | ChromePdfRenderer | |
---|---|---|
Description | Used to define PDF print out options, like paper size, DPI, headers and footers | |
Properties / functions | Type | Description |
CustomCookies | Dictionary<string, string> | Custom cookies for the HTML render. Cookies do not persist between renders and must be set each time. |
PaperFit | VirtualPaperLayoutManager | A manager for setting up virtual paper layouts, controlling how content will be laid out on PDF "paper" pages. Includes options for Default Chrome Behavior, Zoomed, Responsive CSS3 Layouts, Scale-To-Page & Continuous Feed style PDF page setups. |
UseMarginsOnHeaderAndFooter | UseMargins | Use margin values from the main document when rendering headers and footers. |
CreatePdfFormsFromHtml | bool | Turns all HTML form elements into editable PDF forms. Default value is true. |
CssMediaType | PdfCssMediaType | Enables Media="screen" CSS Styles and StyleSheets. Default value is PdfCssMediaType.Screen. |
CustomCssUrl | string | Allows a custom CSS style-sheet to be applied to HTML before rendering. May be a local file path or a remote URL. Only applicable when rendering HTML to PDF. |
EnableJavaScript | bool | Enables JavaScript and JSON to be executed before the page is rendered. Ideal for printing from Ajax / Angular Applications. Default value is false. |
EnableMathematicalLaTex | bool | Enables rendering of Mathematical LaTeX Elements. |
Javascript | string | A custom JavaScript string to be executed after all HTML has loaded but before PDF rendering. |
JavascriptMessageListener | StringDelegate | A method callback to be invoked whenever a browser JavaScript console message becomes available. |
FirstPageNumber | int | First page number to be used in PDF Headers and Footers. Default value is 1. |
TableOfContents | TableOfContentsTypes | Generates a table of contents at the location in the HTML document where an element is found with id "ironpdf-toc". |
GrayScale | bool | Outputs a black-and-white PDF. Default value is false. |
TextHeader | ITextHeaderFooter | Sets the footer content for every PDF page as text, supporting 'mail-merge' and automatically turning URLs into hyperlinks. |
TextFooter | ||
HtmlHeader | HtmlHeaderFooter | Sets the header content for every PDF page as HTML. Supports 'mail-merge'. |
HtmlFooter | ||
InputEncoding | Encoding | The input character encoding as a string. Default value is Encoding.UTF8. |
MarginTop | double | Top PDF "paper" margin in millimeters. Set to zero for border-less and commercial printing applications. Default value is 25. |
MarginRight | double | Right PDF "paper" margin in millimeters. Set to zero for border-less and commercial printing applications. Default value is 25. |
MarginBottom | double | Bottom PDF "paper" margin in millimeters. Set to zero for border-less and commercial printing applications. Default value is 25. |
MarginLeft | double | Left PDF "paper" margin in millimeters. Set to zero for border-less and commercial printing applications. Default value is 25. |
PaperOrientation | PdfPaperOrientation | The PDF paper orientation, such as Portrait or Landscape. Default value is Portrait. |
PaperSize | PdfPaperSize | Sets the paper size |
SetCustomPaperSizeinCentimeters | double | Sets the paper size in centimeters. |
SetCustomPaperSizeInInches | Sets the paper size in inches. | |
SetCustomPaperSizeinMilimeters | Sets the paper size in millimeters. | |
SetCustomPaperSizeinPixelsOrPoints | Sets the paper size in screen pixels or printer points. | |
PrintHtmlBackgrounds | Boolean | Indicates whether to print background-colors and images from HTML. Default value is true. |
RequestContext | RequestContexts | Request context for this render, determining isolation of certain resources such as cookies. |
Timeout | Integer | Render timeout in seconds. Default value is 60. |
Title | String | PDF Document Name and Title metadata, useful for mail-merge and automatic file naming in the IronPdf MVC and Razor extensions. |
ForcePaperSize | Boolean | Force page sizes to be exactly what is specified via IronPdf.ChromePdfRenderOptions.PaperSize by resizing the page after generating a PDF from HTML. Helps correct small errors in page size when rendering HTML to PDF. |
WaitFor | WaitFor | A wrapper object that holds configuration for wait-for mechanism for users to wait for certain events before rendering. By default, it will wait for nothing. |