Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void MeasuredWidth_MatchesRenderedWidth_ForKernedText()

private static double GetMeasuredWidth(PdfPageSettings settings, ExcelRangeBase range)
{
var catalog = new PdfCatalog();
var catalog = new EPPlusToPdfWriter();
var cells = catalog.GetCellCollectionFromRange(settings, range);
return cells[range.Start.Row, range.Start.Column].TotalTextLength;
}
Expand All @@ -134,7 +134,7 @@ private static byte[] RenderToBytes(PdfPageSettings settings, ExcelWorksheet she
{
using (var stream = new MemoryStream())
{
new PdfCatalog(settings, sheet).Save(stream);
new EPPlusToPdfWriter(settings, sheet).Save(stream);
return stream.ToArray();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void MultipleSubstitution_Roboto_ReproSheet()
// rather than silently writing an unreadable file.
using (var stream = new MemoryStream())
{
new PdfCatalog(settings, sheet).Save(stream);
new EPPlusToPdfWriter(settings, sheet).Save(stream);
AssertLooksLikePdf(stream.ToArray());
}

Expand Down
30 changes: 21 additions & 9 deletions src/EPPlus.Export.Pdf.Tests/PdfTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ protected void SaveAsPdf(ExcelWorkbook wb, string pdfFileName, params ExcelRange

protected void SaveAsPdf(byte[] pdfBytes, string pdfFileName)
{
if(!Directory.Exists(_worksheetPath))
if (!Directory.Exists(_worksheetPath))
{
Assert.Inconclusive("Pdf Path not available in this environment");
}
try
{
File.WriteAllBytes(Path.Combine(_pdfPath, pdfFileName), pdfBytes);
}
catch(Exception ex)
catch (Exception ex)
{
Assert.Inconclusive("Could not write pdf file: " + ex.Message);
}

}

/// <summary>
Expand All @@ -56,8 +56,16 @@ protected void SaveAsPdf(byte[] pdfBytes, string pdfFileName)
/// </summary>
protected void SaveAsPdf(ExcelWorksheet sheet, string pdfFileName, PdfPageSettings settings)
{
string path = GetPdfFilePath(pdfFileName);
new PdfCatalog(settings, sheet).Save(path);
if (!pdfFileName.ToLower().EndsWith(".pdf"))
{
pdfFileName += ".pdf";
}
if (Directory.Exists(_pdfPath) == false)
{
Directory.CreateDirectory(_pdfPath);
}
var path = Path.Combine(_pdfPath, pdfFileName);
new EPPlusToPdfWriter(settings, sheet).Save(path);
}

/// <summary>
Expand All @@ -66,8 +74,12 @@ protected void SaveAsPdf(ExcelWorksheet sheet, string pdfFileName, PdfPageSettin
/// </summary>
protected void SaveAsPdf(ExcelWorkbook wb, string pdfFileName, PdfPageSettings settings)
{
string path = GetPdfFilePath(pdfFileName);
new PdfCatalog(settings, wb).Save(path);
if (!pdfFileName.ToLower().EndsWith(".pdf"))
{
pdfFileName += ".pdf";
}
var path = Path.Combine(_pdfPath, pdfFileName);
new EPPlusToPdfWriter(settings, wb).Save(path);
}

/// <summary>
Expand All @@ -78,9 +90,9 @@ protected void SaveAsPdf(ExcelWorkbook wb, string pdfFileName, PdfPageSettings s
{
string path = GetPdfFilePath(pdfFileName);
if (ranges.Count() > 1)
new PdfCatalog(settings, ranges).Save(path);
new EPPlusToPdfWriter(settings, ranges).Save(path);
else
new PdfCatalog(settings, ranges[0]).Save(path);
new EPPlusToPdfWriter(settings, ranges[0]).Save(path);
}
private static string GetPdfFilePath(string pdfFileName)
{
Expand Down
190 changes: 184 additions & 6 deletions src/EPPlus.Export.Pdf.Tests/PdfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public void SaveWorksheetToStreamViaCatalogTest()
var ws = p.Workbook.Worksheets[0];
var pageSettings = new PdfPageSettings(ws.Workbook.RenderContext.FontEngine);
using var ms = new MemoryStream();
var pdfCatalog = new PdfCatalog(pageSettings, ws);
var pdfCatalog = new EPPlusToPdfWriter(pageSettings, ws);
pdfCatalog.Save(ms);
AssertLooksLikePdf(ms.ToArray());
}
Expand Down Expand Up @@ -674,7 +674,7 @@ public void WorkbookTests(string sourceFile, string outputPath, string wsName)
pageSettings.ShowGridLines = false;
pageSettings.ShowHeadings = false;

var pdfCatalog = new PdfCatalog(pageSettings, ws);
var pdfCatalog = new EPPlusToPdfWriter(pageSettings, ws);
pdfCatalog.Save(outputPath);

}
Expand Down Expand Up @@ -813,7 +813,7 @@ public void EachWorksheetUsesItsOwnOrientation()
byte[] pdf;
using (var ms = new MemoryStream())
{
var pdfCatalog = new PdfCatalog(settings, package.Workbook);
var pdfCatalog = new EPPlusToPdfWriter(settings, package.Workbook);
pdfCatalog.Save(ms);
pdf = ms.ToArray();
}
Expand Down Expand Up @@ -880,7 +880,7 @@ public void EachWorksheetUsesItsOwnPaperSize()
byte[] pdf;
using (var ms = new MemoryStream())
{
var pdfCatalog = new PdfCatalog(settings, package.Workbook);
var pdfCatalog = new EPPlusToPdfWriter(settings, package.Workbook);
pdfCatalog.Save(ms);
pdf = ms.ToArray();
}
Expand Down Expand Up @@ -978,7 +978,7 @@ public void headerFooterImage()
{
using var p = OpenTemplatePackage("EPPlus Sample 3.xlsx");
var ws = p.Workbook.Worksheets[0];
string path = _pdfPath + "EPPlus Sample 3.pdf";
string path = _pdfPath + "EPPlus Sample 3 hf1.pdf";
ws.SaveAsPdf(path);
}

Expand All @@ -987,7 +987,7 @@ public void headerFooterImage2()
{
using var p = CreateWorkbook();
var ws = p.Workbook.Worksheets[0];
string path = _pdfPath + "EPPlus Sample 4.pdf";
string path = _pdfPath + "EPPlus Sample 3 hf2.pdf";
ws.SaveAsPdf(path);
}
public ExcelPackage CreateWorkbook()
Expand Down Expand Up @@ -1129,6 +1129,184 @@ public void PngTransparentTest()
}

[TestMethod]
public void ScalingTests()
{
using var p = OpenTemplatePackage("PDFTest.xlsx");
var ws = p.Workbook.Worksheets[0];
ws.PrinterSettings.Scale = 50;
string path = _pdfPath + "ScalingTest1.pdf";
ws.SaveAsPdf(path);
}

[TestMethod]
public void ScalingTestFitToPage()
{
using var p = OpenTemplatePackage("PDFTest.xlsx");
var ws = p.Workbook.Worksheets[0];
ws.PrinterSettings.RepeatRows = new ExcelAddress("1:2");
ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToWidth = 2;
ws.PrinterSettings.FitToHeight = 3;
string path = _pdfPath + "ScalingTest2.pdf";
ws.SaveAsPdf(path);
}


private static int CountPdfPages(string path)
{
var text = Encoding.ASCII.GetString(File.ReadAllBytes(path));
return Regex.Matches(text, @"/Type\s*/Page\b").Count;
}

private static ExcelWorksheet AddGrid(ExcelPackage p, int rows, int cols, string name = "Sheet1")
{
var ws = p.Workbook.Worksheets.Add(name);
for (int r = 1; r <= rows; r++)
for (int c = 1; c <= cols; c++)
ws.Cells[r, c].Value = $"R{r}C{c}";
ws.PrinterSettings.PaperSize = ePaperSize.A4;
ws.PrinterSettings.Orientation = eOrientation.Portrait;
return ws;
}

private static int SaveAndCount(ExcelWorksheet ws, string name)
{
string path = _pdfPath + name + ".pdf";
ws.SaveAsPdf(path);
return CountPdfPages(path);
}

[TestMethod]
public void Percentage_ChangesPageCount()
{
using var p = new ExcelPackage();
var ws = AddGrid(p, rows: 120, cols: 20); // multi-page both directions

ws.PrinterSettings.Scale = 100; int at100 = SaveAndCount(ws, "pct_100");
ws.PrinterSettings.Scale = 50; int at50 = SaveAndCount(ws, "pct_50");
ws.PrinterSettings.Scale = 200; int at200 = SaveAndCount(ws, "pct_200");

Assert.IsTrue(at100 > 1, "sheet should be multi-page at 100%");
Assert.IsTrue(at50 < at100, $"50% ({at50}) should use fewer pages than 100% ({at100})");
Assert.IsTrue(at200 > at100, $"200% ({at200}) should use more pages than 100% ({at100})");
}

[TestMethod]
public void Fit_WidthBudget_Exact()
{
using var p = new ExcelPackage();
var ws = AddGrid(p, rows: 3, cols: 60);
Assert.IsTrue(SaveAndCount(ws, "fitw_base") > 2, "sheet should span >2 page-columns at 100%");

ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToHeight = 0;
ws.PrinterSettings.FitToWidth = 1;
Assert.AreEqual(1, SaveAndCount(ws, "fitw_1"));

ws.PrinterSettings.FitToWidth = 2;
Assert.AreEqual(2, SaveAndCount(ws, "fitw_2"));
}

[TestMethod]
public void Fit_HeightBudget_Exact()
{
using var p = new ExcelPackage();
var ws = AddGrid(p, rows: 300, cols: 3);
Assert.IsTrue(SaveAndCount(ws, "fith_base") > 3);

ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToWidth = 0;
ws.PrinterSettings.FitToHeight = 3;
Assert.AreEqual(3, SaveAndCount(ws, "fith_3"));

ws.PrinterSettings.FitToHeight = 1;
Assert.AreEqual(1, SaveAndCount(ws, "fith_1"));
}

[TestMethod]
public void Fit_OneByOne_SinglePage()
{
using var p = new ExcelPackage();
var ws = AddGrid(p, rows: 200, cols: 30);
ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToWidth = 1;
ws.PrinterSettings.FitToHeight = 1;
Assert.AreEqual(1, SaveAndCount(ws, "fit_1x1"));
}

[TestMethod]
public void Fit_DoesNotEnlargeSmallSheet()
{
using var p = new ExcelPackage();
var ws = AddGrid(p, rows: 3, cols: 3);
ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToWidth = 2;
ws.PrinterSettings.FitToHeight = 2;
Assert.AreEqual(1, SaveAndCount(ws, "fit_small"));
}

[TestMethod]
public void Fit_IgnoresManualPageBreaks()
{
using var p = new ExcelPackage();
var ws = AddGrid(p, rows: 300, cols: 3);
ws.Row(150).PageBreak = true;
ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToWidth = 0;
ws.PrinterSettings.FitToHeight = 2;
Assert.AreEqual(2, SaveAndCount(ws, "fit_break"));
}

[TestMethod]
public void Fit_WithHeadings_HonorsBudget()
{
using var p = new ExcelPackage();
var ws = AddGrid(p, rows: 300, cols: 3);
ws.PrinterSettings.ShowHeaders = true;
ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToWidth = 0;
ws.PrinterSettings.FitToHeight = 3;
Assert.AreEqual(3, SaveAndCount(ws, "fit_headings"));
}

[TestMethod]
public void Fit_WithPrintTitles_HonorsBudget()
{
using var p = new ExcelPackage();
var ws = AddGrid(p, rows: 300, cols: 3);
ws.PrinterSettings.RepeatRows = new ExcelAddress("1:2");
ws.PrinterSettings.FitToPage = true;
ws.PrinterSettings.FitToWidth = 0;
ws.PrinterSettings.FitToHeight = 3;
Assert.AreEqual(3, SaveAndCount(ws, "fit_titles"));
}

[TestMethod]
public void MultiSheet_PerSheetScaling_IsIsolated()
{
using var p = new ExcelPackage();
var plain = AddGrid(p, rows: 300, cols: 3, name: "Plain");
var scaled = AddGrid(p, rows: 300, cols: 3, name: "Scaled");

int plainAlone = SaveAndCount(plain, "multi_plain");
int scaledUnscaled = SaveAndCount(scaled, "multi_scaled_off");

scaled.PrinterSettings.FitToPage = true;
scaled.PrinterSettings.FitToWidth = 1;
scaled.PrinterSettings.FitToHeight = 1;
int scaledAlone = SaveAndCount(scaled, "multi_scaled_on");

Assert.AreEqual(1, scaledAlone, "fit 1x1 should put the scaled sheet on one page");
Assert.IsTrue(scaledUnscaled > scaledAlone, "scaling should actually reduce the scaled sheet");

string combinedPath = _pdfPath + "multi_combined.pdf";
p.Workbook.SaveAsPdf(combinedPath);
int combined = CountPdfPages(combinedPath);

Assert.AreEqual(plainAlone + scaledAlone, combined,
$"combined ({combined}) should equal plain ({plainAlone}) + scaled ({scaledAlone}) - " +
"per-sheet scaling must stay isolated");
}
public void TableDxfStyleTest()
{
using var p = OpenTemplatePackage("TableDxfStylePdf1.xlsx");
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus.Export.Pdf.Tests/VariationSequenceReproTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void VariationSequence_BizUdGothic_ReproSheet()
// rather than silently writing an unreadable file.
using (var stream = new MemoryStream())
{
new PdfCatalog(settings, sheet).Save(stream);
new EPPlusToPdfWriter(settings, sheet).Save(stream);
AssertLooksLikePdf(stream.ToArray());
}

Expand Down
12 changes: 10 additions & 2 deletions src/EPPlus.Export.Pdf/DocumentObjects/PdfContentStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public void AddCommand(string command)
commands.Add(command);
}

public void AddContentScale(double scale, double anchorX, double anchorY)
{
if (scale == 1d) return;
double e = (1d - scale) * anchorX;
double f = (1d - scale) * anchorY;
commands.Add($"{scale.ToPdfString()} 0 0 {scale.ToPdfString()} {e.ToPdfString()} {f.ToPdfString()} cm");
}

public void AddCellLayout(PdfCellLayout cell, string label)
{
if (cell.Size.X <= 0d || cell.Size.Y <= 0d) return;
Expand Down Expand Up @@ -486,8 +494,8 @@ public void AddMarginClipping(PdfPageLayout pageLayout, PdfPageSettings pageSett
left = System.Math.Min(left, System.Math.Min(line.X1, line.X2));
right = System.Math.Max(right, System.Math.Max(line.X1, line.X2));
}
right = System.Math.Min(right, pageSettings.PageSize.WidthPu);
bottom = System.Math.Max(bottom, 0d);
//right = System.Math.Min(right, pageSettings.PageSize.WidthPu);
//bottom = System.Math.Max(bottom, 0d);
var pad = GridLine.Width * 4;
var x = left + pl.HeadingWidth + pl.PrintTitleWidth - pad;
var y = bottom - pad;
Expand Down
Loading
Loading