While converting the PDF from excel, the text in the last merged row is getting split between two pages.
Upper half of the text is coming in one page and lower half in next. Please refer to the attached PDF document TextSplitIssue.pdf.
TextSplitIssue.pdf (91.7 KB)
Please find the below sample code which is used to generate the PDF from excel.
internal void GeneratePDF()
{
try
{
Workbook wb = new(Path.Join(Directory.GetCurrentDirectory(), @“ExcelSample\SAMPLE_EXCEL.xlsx”));
Worksheet worksheet = wb.Worksheets[0];
PageSetup pageSetup = worksheet.PageSetup;
pageSetup.PrintTitleRows = "$1:$5";
SetMarginForPdf(wb, 1, 1, 1, 1, 0);
PdfSaveOptions pdfSaveOptions = new()
{
AllColumnsInOnePagePerSheet = true,
OutputBlankPageWhenNothingToPrint = false
};
if (!Directory.Exists(Path.Join(Directory.GetCurrentDirectory(), $"PDFOutput")))
{
Directory.CreateDirectory(Path.Join(Directory.GetCurrentDirectory(), $"PDFOutput"));
}
wb.Save(Path.Join(Directory.GetCurrentDirectory(), $"PDFOutput\\{Guid.NewGuid()}.pdf"), pdfSaveOptions);
}
catch (Exception)
{
throw;
}
}
private void SetMarginForPdf(Workbook excelworkbook, int topMargin, int leftMargin, int rightMargin, int bottomMargin, int sheetNumber)
{
excelworkbook.Worksheets[sheetNumber].PageSetup.PaperSize = PaperSizeType.PaperA4;
excelworkbook.Worksheets[sheetNumber].PageSetup.Zoom = 100;
excelworkbook.Worksheets[sheetNumber].PageSetup.TopMargin = topMargin;
excelworkbook.Worksheets[sheetNumber].PageSetup.LeftMargin = leftMargin;
excelworkbook.Worksheets[sheetNumber].PageSetup.RightMargin = rightMargin;
excelworkbook.Worksheets[sheetNumber].PageSetup.BottomMargin = bottomMargin;
}