Top & bottom margins will not be less than half inch

1 Answer 24 Views
PdfProcessing
Darwin
Top achievements
Rank 1
Darwin asked on 24 Jan 2025, 11:03 PM

I am trying to convert HTML to a PDF document with 0.25" margins. The left and right margins are being set correctly, but the top and bottom margins remain 0.5".  My code is below, a screenshot of the generated PDF is attached.

                StringBuilder sb = new StringBuilder();

                foreach (string row in data)
                    sb.AppendLine(row);

                HtmlFormatProvider provider = new HtmlFormatProvider();
                RadFlowDocument html = provider.Import(sb.ToString());

                foreach (Section section in html.Sections)
                {
                    section.PageMargins = new Telerik.Windows.Documents.Primitives.Padding(24, 24, 24, 24); 

                    // Code snippet from function that adds header inserted below

                    Header header = section.Headers.Add();
                    Table table = header.Blocks.AddTable();
                    Border border = new Border(1, BorderStyle.Single, new ThemableColor(Colors.Black));
                    table.Borders = new TableBorders(border);
            
                    TableRow row1 = table.Rows.AddTableRow();
                    row1.Height = new TableRowHeight(HeightType.Exact, 30);
            
                    TableCell Title = row1.Cells.AddTableCell();
                    Paragraph pTitle = Title.Blocks.AddParagraph();
                    Run rTitle = pTitle.Inlines.AddRun(FormName);
                    Title.PreferredWidth = Inches(3.99);
                    Title.Shading.BackgroundColor = gray;
                    Title.VerticalAlignment = VerticalAlignment.Center;
                    rTitle.FontWeight = FontWeights.Bold;
                    rTitle.FontSize = 16;

                    // Call to similar code for adding footer

                }

                PdfFormatProvider pdf = new PdfFormatProvider();
                return pdf.Export(html);

 

Thanks in advance for the help.

Darwin Pinder

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 28 Jan 2025, 11:16 AM

Hello Darwin,

For this specific scenario, you would also have to modify the section.HeaderTopMargin property. I have tested the provided code snippet on my end and can confirm that setting the HeaderTopMargin to "24" produces symmetrical results:

section.HeaderTopMargin = 24;
section.PageMargins = new Telerik.Windows.Documents.Primitives.Padding(24, 24, 24, 24);

You can also do the same for the section.FooterBottomMargin property.

I hope this helps. Let me know if further assistance is required.

Regards,
Yoan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Darwin
Top achievements
Rank 1
commented on 28 Jan 2025, 02:23 PM

Thanks again Yoan. I'm going to put you in for a raise. 

Darwin Pinder

Yoan
Telerik team
commented on 29 Jan 2025, 07:57 AM

Hello Darwin,

Thanks for the positive feedback. Do not hesitate to write back in case you have any additional questions.

Best,

Yoan

Tags
PdfProcessing
Asked by
Darwin
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or