Я сталкиваюсь с проблемой, преобразующей HTML в PDF с использованием пакета htmlrenderer.pdfsharp в .net. < /p>
При преобразовании в PDF некоторые строки таблицы разделены вертикально на страницах. Иногда сам контент также разделен вертикально. Свойство CSS 'Page-Break-Inside: избегать;' , похоже, не работает, как и ожидалось. Свойство с таблицей, TR, TH и TD -элементами.using System;
using System.IO;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace HtmlToPdfIssue
{
class Program
{
static void Main(string[] args)
{
// Define the output PDF file path
string outputPath = "OutputWithSplitTable.pdf";
// Get the HTML content
string htmlContent = GetHtmlContent();
try
{
// Convert HTML to PDF
// Note: The 'size' and 'margins' are adjusted to make it more likely for content to split
PdfDocument pdf = PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4, margin: 10);
// Save the PDF
pdf.Save(outputPath);
Console.WriteLine($"PDF generated successfully at: {Path.GetFullPath(outputPath)}");
Console.WriteLine("Please open the PDF and observe the table row splitting issue.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
Console.ReadKey(); // Keep console open
}
private static string GetHtmlContent()
{
// This HTML is designed to create a long table that will likely span multiple pages.
// We're also applying page-break-inside: avoid; to the table rows, as you've tried.
return @"
HTML to PDF Split Table Issue
body {
font-family: Arial, sans-serif;
font-size: 12px;
margin: 0;
padding: 0;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
page-break-inside: avoid !important;
}
tr {
page-break-inside: avoid !important;
break-inside: avoid !important; /* Also trying break-inside */
}
Table Row Splitting Issue Demonstration
This document demonstrates an issue where table rows and content are split vertically across pages when converting HTML to PDF using HtmlRenderer.PdfSharp, despite `page-break-inside: avoid;`.
Below is a table with many rows, designed to span multiple pages. Observe how rows might be cut off or content within cells is split.
Row #
Description
Details
" +
// Generate many rows to ensure the table spans multiple pages
GenerateTableRows(20) +
@"
End of document.
";
}
private static string GenerateTableRows(int count)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 1; i
Подробнее здесь: https://stackoverflow.com/questions/796 ... plitting-w
Почему не разбивая страница-нарушение: избегайте; Предотвращение строки таблицы и разделения контента при преобразовании ⇐ C#
Место общения программистов C#
1750836800
Anonymous
Я сталкиваюсь с проблемой, преобразующей HTML в PDF с использованием пакета htmlrenderer.pdfsharp в .net. < /p>
При преобразовании в PDF некоторые строки таблицы разделены вертикально на страницах. Иногда сам контент также разделен вертикально. Свойство CSS 'Page-Break-Inside: избегать;' , похоже, не работает, как и ожидалось. Свойство с таблицей, TR, TH и TD -элементами.using System;
using System.IO;
using PdfSharp.Pdf;
using TheArtOfDev.HtmlRenderer.PdfSharp;
namespace HtmlToPdfIssue
{
class Program
{
static void Main(string[] args)
{
// Define the output PDF file path
string outputPath = "OutputWithSplitTable.pdf";
// Get the HTML content
string htmlContent = GetHtmlContent();
try
{
// Convert HTML to PDF
// Note: The 'size' and 'margins' are adjusted to make it more likely for content to split
PdfDocument pdf = PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4, margin: 10);
// Save the PDF
pdf.Save(outputPath);
Console.WriteLine($"PDF generated successfully at: {Path.GetFullPath(outputPath)}");
Console.WriteLine("Please open the PDF and observe the table row splitting issue.");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
Console.ReadKey(); // Keep console open
}
private static string GetHtmlContent()
{
// This HTML is designed to create a long table that will likely span multiple pages.
// We're also applying page-break-inside: avoid; to the table rows, as you've tried.
return @"
HTML to PDF Split Table Issue
body {
font-family: Arial, sans-serif;
font-size: 12px;
margin: 0;
padding: 0;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
page-break-inside: avoid !important;
}
tr {
page-break-inside: avoid !important;
break-inside: avoid !important; /* Also trying break-inside */
}
Table Row Splitting Issue Demonstration
This document demonstrates an issue where table rows and content are split vertically across pages when converting HTML to PDF using HtmlRenderer.PdfSharp, despite `page-break-inside: avoid;`.
Below is a table with many rows, designed to span multiple pages. Observe how rows might be cut off or content within cells is split.
Row #
Description
Details
" +
// Generate many rows to ensure the table spans multiple pages
GenerateTableRows(20) +
@"
End of document.
";
}
private static string GenerateTableRows(int count)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 1; i
Подробнее здесь: [url]https://stackoverflow.com/questions/79664724/why-isnt-page-break-inside-avoid-preventing-table-row-and-content-splitting-w[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия