I faced such a problem, when converting from Excel to PDF, automatic page breaks are not transferred (examples in the test.rar attachment)
I tried setting manual breaks in the place I wanted, but they only applied if IsFitToPage = false.
but at the same time, another problem arises at the scale I need, a few more automatic breaks are added, due to the fact that the content does not fit,
otherwise I have to zoom out in my example to 16%, which does not quite suit me
I am using the following code
- Code: Select all
public class XlsConvertSettings
{
public List<SheetConvertSettings> Sheets { get; set; } = new List<SheetConvertSettings>();
}
public class SheetConvertSettings
{
public string SheetName { get; set; }
public List<string> HPageBreakRanges { get; set; }
public List<string> VPageBreakRanges { get; set; }
public int Scale { get; set; }
public int TempScale { get; set; }
public bool IsConsvertToPdf { get; set; }
}
...
private float KSize { get; set; } = 0f;
private void SetPageBreaks(Workbook workbook, XlsConvertSettings settings)
{
for (int i = 0; i < workbook.Worksheets.Count; i++)
{
var sheet = workbook.Worksheets[i];
var sheetSettings = settings.Sheets.FirstOrDefault(s => string.Equals(sheet.Name, s.SheetName, StringComparison.InvariantCultureIgnoreCase)) ?? new SheetConvertSettings();
if (sheetSettings.HPageBreakRanges.Any() && sheetSettings.VPageBreakRanges.Any()) continue;
workbook.Worksheets[i].PageSetup.PrintArea = string.Empty;
sheetSettings.VPageBreakRanges.ForEach(adress => workbook.Worksheets[i].VPageBreaks.Add(sheet.Range[adress]));
sheetSettings.HPageBreakRanges.ForEach(adress => workbook.Worksheets[i].HPageBreaks.Add(sheet.Range[adress]));
if (sheet.PageSetup.IsFitToPage) continue;
workbook.Worksheets[i].PageSetup.Zoom = sheetSettings.IsConsvertToPdf ? sheetSettings.TempScale : sheetSettings.Scale;
KSize = (sheetSettings.Scale > sheetSettings.TempScale) ? (sheetSettings.Scale / sheetSettings.TempScale) : KSize;
}
}
I also tried to transfer the generated workbook to PDfDocument, and there I try to correct the situation through the page size, while I deliberately passed the TempScale scale value 16 so that only one manual page break would be applied
- Code: Select all
private PdfDocument AddContentIntoDocument(byte[] content, PdfDocument document)
{
PdfDocument pdfDoc = new();
pdfDoc.LoadFromBytes(content);
foreach (PdfPageBase page in pdfDoc.Pages)
{
var size = KSize > 0f ? (page.Size/KSize) : page.Size;
PdfPageBase p = document.Pages.Add(size, new Spire.Pdf.Graphics.PdfMargins(0));
page.CreateTemplate().Draw(p, 0, 0);
}
return document;
}
...
private MemoryStream ConvertPdfFormatFromXls(Workbook workbook, string resultFileName)
{
using (var stream = new MemoryStream())
{
workbook.SaveToStream(stream, Spire.Xls.FileFormat.PDF);
PdfDocument pdfDocument = new();
pdfDocument = AddContentIntoDocument(stream.ToArray(), pdfDocument);
using (var pdfStream = new MemoryStream())
{
pdfDocument.SaveToStream(pdfStream);
return pdfStream;
}
}
}
but unfortunately this did not help, I will be grateful to you for the solution proposed to me,
taking into account the fact that in my example the scale I want is preserved, and the set manual page breaks are carried over when converting to pdf
Best Regards, Stanislav Shevyakov