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
5 changes: 0 additions & 5 deletions src/EPPlus.Export.Pdf/DocumentObjects/PdfBorderRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public PdfBorderRenderer(PdfCellBorderLayout cell)

public void RenderBorder(PdfContentStream contentStream)
{
contentStream.AddCommand($"% Border Start: {Name}");
contentStream.AddCommand("q");
if (Top != null) RenderBorder(contentStream, Top);
if (Bottom != null) RenderBorder(contentStream, Bottom);
Expand All @@ -60,12 +59,10 @@ public void RenderBorder(PdfContentStream contentStream)
if (DiagonalUp != null) RenderBorder(contentStream, DiagonalUp);
if (DiagonalDown != null) RenderBorder(contentStream, DiagonalDown);
contentStream.AddCommand("Q");
contentStream.AddCommand($"% Border End: {Name}");
}

private void RenderBorder(PdfContentStream contentStream, PdfCellBorderData border)
{
contentStream.AddCommand($"% Border Type Start: {border.LineType.ToString()}");
double x1 = X, y1 = Y, x2 = X, y2 = Y;
switch (border.LineType)
{
Expand Down Expand Up @@ -132,7 +129,6 @@ private void RenderBorder(PdfContentStream contentStream, PdfCellBorderData bord
contentStream.AddCommand($"{x1.ToPdfStringF4()} {y1.ToPdfStringF4()} m");
contentStream.AddCommand($"{x2.ToPdfStringF4()} {y2.ToPdfStringF4()} l");
contentStream.AddCommand("S");
contentStream.AddCommand($"% Border Type End: {border.LineType.ToString()}");
return;
}
switch (border.BorderStyle)
Expand Down Expand Up @@ -182,7 +178,6 @@ private void RenderBorder(PdfContentStream contentStream, PdfCellBorderData bord
contentStream.AddCommand($"{x1.ToPdfStringF4()} {y1.ToPdfStringF4()} m");
contentStream.AddCommand($"{x2.ToPdfStringF4()} {y2.ToPdfStringF4()} l");
contentStream.AddCommand("S");
contentStream.AddCommand($"% Border Type End: {border.LineType.ToString()}");
}

private void DrawBasicBorder(PdfContentStream contentStream, PdfCellBorderData border, double width, string dash)
Expand Down
19 changes: 0 additions & 19 deletions src/EPPlus.Export.Pdf/DocumentObjects/PdfContentStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,25 @@ public void AddCellLayout(PdfCellLayout cell, string label)
if (cell.Size.X <= 0d || cell.Size.Y <= 0d) return;
if (cell.CellFillData.GradientFillData != null && cell.CellFillData.PatternStyle != ExcelFillStyle.Solid)
{
commands.Add($"% Pattern Start: {cell.Name}");
commands.Add("q");
commands.Add("/Pattern cs");
commands.Add($"/{label} scn");
commands.Add($"{cell.LocalPosition.X.ToPdfString()} {cell.LocalPosition.Y.ToPdfString()} {cell.Size.X.ToPdfString()} {cell.Size.Y.ToPdfString()} re");
commands.Add("f");
commands.Add("Q");
commands.Add($"% Pattern End: {cell.Name}");
}
else if (cell.CellFillData.BackgroundColor != Color.Empty && cell.CellFillData.PatternStyle == ExcelFillStyle.Solid)
{
commands.Add($"% Solid Fill Start: {cell.Name}");
commands.Add("q");
commands.Add($"{GridLine.HalfWidth.ToPdfString()} w");
commands.Add(cell.CellFillData.BackgroundColor.ToFillCommand());
commands.Add(cell.CellFillData.enhanceGridLine ? Color.Black.ToStrokeCommand() : cell.CellFillData.BackgroundColor.ToStrokeCommand());
commands.Add($"{cell.LocalPosition.X.ToPdfString()} {cell.LocalPosition.Y.ToPdfString()} {cell.Size.X.ToPdfString()} {cell.Size.Y.ToPdfString()} re");
commands.Add("B");
commands.Add("Q");
commands.Add($"% Solid Fill End: {cell.Name}");
}
else if (cell.CellFillData.PatternStyle != ExcelFillStyle.None)
{
commands.Add($"% Pattern Start: {cell.Name}");
if (cell.CellFillData.BackgroundColor != Color.Empty)
{
commands.Add("q");
Expand All @@ -94,14 +89,12 @@ public void AddCellLayout(PdfCellLayout cell, string label)
commands.Add("B");
commands.Add("Q");
}

commands.Add("q");
commands.Add("/Pattern cs");
commands.Add($"/{label} scn");
commands.Add($"{cell.LocalPosition.X.ToPdfString()} {cell.LocalPosition.Y.ToPdfString()} {cell.Size.X.ToPdfString()} {cell.Size.Y.ToPdfString()} re");
commands.Add("f");
commands.Add("Q");
commands.Add($"% Pattern End: {cell.Name}");
}
}

Expand Down Expand Up @@ -372,12 +365,10 @@ private static ushort GetUnitsPerEm(ushort[] fontUnitsPerEm, byte fontId, ushort

public void AddCellContentLayout(PdfCellContentLayout cell, PdfDictionaries dictionaries, PdfPageSettings pageSettings)
{
commands.Add($"% Content Start: {cell.Name}");
commands.Add("q");
if (cell.Clip) AddClipping(cell);
AddText(cell, cell.LocalPosition, cell.CellAlignmentData.TextRotation, dictionaries, pageSettings);
commands.Add("Q");
commands.Add($"% Content End: {cell.Name}");
}

private void AddClipping(PdfCellContentLayout cell)
Expand All @@ -389,8 +380,6 @@ public void AddInnerGridLines(Transform pageLayout)
{
if (pageLayout is not PdfPageLayout pl) return;
if (pl.isCommentsPage) return;

commands.Add($"% Gridlines Start");
commands.Add("q");
commands.Add($"{GridLine.Width.ToPdfString()} w");
commands.Add(Color.Black.ToFillCommand());
Expand All @@ -413,24 +402,20 @@ public void AddInnerGridLines(Transform pageLayout)
}
commands.Add("f");
commands.Add("Q");
commands.Add($"% Gridlines End");
}

public void AddImage(string label, double x, double y, double width, double height)
{
commands.Add($"% Image Start: {label}");
commands.Add("q");
commands.Add($"{width.ToPdfString()} 0 0 {height.ToPdfString()} {x.ToPdfString()} {y.ToPdfString()} cm");
commands.Add($"/{label} Do");
commands.Add("Q");
commands.Add($"% Image End: {label}");
}
public void AddPrintTitleGridLines(Transform pageLayout)
{
if (pageLayout is not PdfPageLayout pl) return;
if (pl.isCommentsPage) return;

commands.Add($"% Print Title Gridlines Start");
commands.Add("q");
commands.Add($"{GridLine.Width.ToPdfString()} w");
commands.Add(Color.Black.ToFillCommand());
Expand All @@ -453,15 +438,13 @@ public void AddPrintTitleGridLines(Transform pageLayout)
}
commands.Add("f");
commands.Add("Q");
commands.Add($"% Print Title Gridlines End");
}

public void AddOuterGridBorder(Transform pageLayout)
{
if (pageLayout is not PdfPageLayout pl) return;
if (pl.isCommentsPage) return;

commands.Add($"% Gridlines Border Start");
commands.Add("q");
commands.Add("1.0 w");
commands.Add("2 J");
Expand All @@ -474,14 +457,12 @@ public void AddOuterGridBorder(Transform pageLayout)
}
commands.Add("S");
commands.Add("Q");
commands.Add($"% Gridlines Border End");
}

public void AddMarginClipping(PdfPageLayout pageLayout, PdfPageSettings pageSettings)
{
if (pageLayout is not PdfPageLayout pl) return;
if (pageLayout.isCommentsPage) return;
commands.Add($"% Margin Clip Start");
if (pl.BorderLines.Count == 0) return;
double top = double.MinValue;
double bottom = double.MaxValue;
Expand Down
19 changes: 8 additions & 11 deletions src/EPPlus.Export.Pdf/ExcelPdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace EPPlus.Export.Pdf
/// </summary>
internal class ExcelPdf
{
private PdfDocumentSettings _documentSettings;
private PdfDocumentSettings _documentSettings;
private PdfDictionaries _dictionaries;
internal List<PdfObject> _document = new List<PdfObject>();
private string _debugString;
Expand Down Expand Up @@ -184,13 +184,13 @@ private void AddContent(PdfPageLayout pageLayout, PdfPage page)
var cells = pageLayout.ChildObjects.Where(t =>
(t is PdfCellLayout || t is PdfCellContentLayout || t is PdfCellBorderLayout) &&
!(t is PdfCellLayout cc && (cc.IsHeading || cc.IsPrintTitle)) &&
!(t is PdfCellContentLayout ccl && (ccl.IsHeaderFooter || ccl.IsHeading || ccl.IsPrintTitle)) &&
!(t is PdfCellContentLayout ccl && (ccl.IsHeaderFooter || ccl.IsHeading || ccl.IsPrintTitle || ccl.IsAddedContent)) &&
!(t is PdfCellBorderLayout cbl && cbl.IsPrintTitle)).ToList();
var headerFooterLayouts = pageLayout.ChildObjects.OfType<PdfCellContentLayout>().Where(t => t.IsHeaderFooter);
var AdditionalContent = pageLayout.ChildObjects.OfType<PdfCellContentLayout>().Where(t => t.IsAddedContent && !(pageSettings.AdditionalContent != "null"));
var headingLayouts = pageLayout.ChildObjects.Where(t => (t is PdfCellLayout cl && cl.IsHeading) || (t is PdfCellContentLayout ccl && ccl.IsHeading));
var printTitleLayouts = pageLayout.ChildObjects.Where(t => (t is PdfCellLayout pl && pl.IsPrintTitle) || (t is PdfCellContentLayout pcl && pcl.IsPrintTitle) || (t is PdfCellBorderLayout pbl && pbl.IsPrintTitle));
var contentStream = new PdfContentStream(_document.Count + 1);
contentStream.AddCommand($"% {pageLayout.Name} start");
//Start page content clipping rectangle.
contentStream.AddCommand("q");
contentStream.AddContentScale(contentScale, scaleAnchorX, scaleAnchorY);
Expand All @@ -201,17 +201,14 @@ private void AddContent(PdfPageLayout pageLayout, PdfPage page)
}
foreach (PdfCellLayout fill in cells.OfType<PdfCellLayout>())
{
contentStream.AddCommand($"% CELL FILL : {fill.Name}");
contentStream.AddCellLayout(fill, GetPatternLabel(fill));
}
foreach (PdfCellContentLayout content in cells.OfType<PdfCellContentLayout>())
{
contentStream.AddCommand($"% CELL TEXT : {content.Name}");
contentStream.AddCellContentLayout(content, _dictionaries, pageSettings);
}
foreach (PdfCellBorderLayout border in cells.OfType<PdfCellBorderLayout>())
{
contentStream.AddCommand($"% CELL BORDER : {border.Name}");
contentStream.AddBorderLayout(border);
}
foreach (PdfImageLayout image in pageLayout.ChildObjects.OfType<PdfImageLayout>())
Expand All @@ -223,13 +220,11 @@ private void AddContent(PdfPageLayout pageLayout, PdfPage page)
}
//Close the clipping rectangle.
contentStream.AddCommand("Q");
contentStream.AddCommand($"% Margin Clip End");
//Add headings
contentStream.AddCommand("q");
contentStream.AddContentScale(contentScale, scaleAnchorX, scaleAnchorY);
foreach (var heading in headingLayouts)
{
contentStream.AddCommand($"% HEADING : {heading.Name}");
switch (heading)
{
case PdfCellLayout layout:
Expand Down Expand Up @@ -268,7 +263,6 @@ private void AddContent(PdfPageLayout pageLayout, PdfPage page)
contentStream.AddContentScale(contentScale, scaleAnchorX, scaleAnchorY);
foreach (var titleCell in printTitleLayouts)
{
contentStream.AddCommand($"% PRINT TITLE : {titleCell.Name}");
switch (titleCell)
{
case PdfCellLayout layout:
Expand All @@ -280,9 +274,12 @@ private void AddContent(PdfPageLayout pageLayout, PdfPage page)
}
}
contentStream.AddCommand("Q");
foreach (var content in AdditionalContent)
{
contentStream.AddCellContentLayout(content, _dictionaries, pageSettings);
}
_document.Add(contentStream);
page.contentObjectNumbers.Add(contentStream.objectNumber);
contentStream.AddCommand($"% {pageLayout.Name} end");
}

//Add Info
Expand Down Expand Up @@ -337,7 +334,7 @@ internal void CreatePdf(PdfDocumentSettings documentSettings, PdfDictionaries di
//Create Page and Content
for (int i = 0; i < layout.ChildObjects.Count; i++)
{
var pageLayout = (PdfPageLayout)layout.ChildObjects[i];
var pageLayout = (PdfPageLayout)layout.ChildObjects[i];
var page = AddPage(2, new List<int>(), pageLayout.Settings);
AddContent(pageLayout, page);
pages.pageObjectNumbers.Add(page.objectNumber);
Expand Down
28 changes: 15 additions & 13 deletions src/EPPlus.Export.Pdf/Layout/PdfCellContentLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ internal class PdfCellContentLayout : Transform
public bool Clip;
public Rect Clipping;
public bool IsHeaderFooter;
public bool IsAddedContent;
public bool IsHeading;
public bool IsPrintTitle;
public TextLayoutEngine textLayoutEngine;
Expand All @@ -40,7 +41,7 @@ internal class PdfCellContentLayout : Transform

public List<PdfShapedText> ShapedTexts { get; set; }

public PdfCellContentLayout(PdfPageSettings pageSettings, PdfDictionaries dictionaries, PdfCellBase cell, MergedCellDrawInfo mergedCellInfo, double x, double y, double width, double height, double scaleX = 1, double scaleY = 1, double rotation = 0, Transform parent = null)
public PdfCellContentLayout(PdfPageSettings pageSettings, PdfDictionaries dictionaries, PdfCellBase cell, double x, double y, double width, double height, double scaleX = 1, double scaleY = 1, double rotation = 0, Transform parent = null)
: base(x, y-height, width, height, scaleX, scaleY, rotation, parent)
{
Z = 2;
Expand Down Expand Up @@ -100,8 +101,21 @@ private double CalculateHorizontalAlignment(string text, double textLength, doub
double newX = x;
switch (CellAlignmentData.HorizontalAlignment)
{
case ExcelHorizontalAlignment.Left:
newX = x + padding;
break;
case ExcelHorizontalAlignment.Center:
case ExcelHorizontalAlignment.CenterContinuous:
newX = x + (width - textLength) / 2d;
break;
case ExcelHorizontalAlignment.Right:
newX = x + (width - textLength) - padding;
break;
case ExcelHorizontalAlignment.Distributed:
case ExcelHorizontalAlignment.Justify:
case ExcelHorizontalAlignment.Fill:
case ExcelHorizontalAlignment.General:
default:
if (double.TryParse(text, out double value))
{
newX = x + (width - textLength) - padding;
Expand All @@ -111,18 +125,6 @@ private double CalculateHorizontalAlignment(string text, double textLength, doub
newX = x + padding;
}
break;
case ExcelHorizontalAlignment.Left:
case ExcelHorizontalAlignment.Justify:
case ExcelHorizontalAlignment.Distributed:
newX = x + padding;
break;
case ExcelHorizontalAlignment.Center:
case ExcelHorizontalAlignment.CenterContinuous:
newX = x + (width - textLength) / 2d;
break;
case ExcelHorizontalAlignment.Right:
newX = x + (width - textLength) - padding;
break;
}
return newX;
}
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus.Export.Pdf/Layout/PdfHeaderFooter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal enum HeaderFooterAlignment
internal enum HeaderFooterSection
{
Header = 0,
Footer = 1
Footer = 1,
}

internal class PdfHeaderFooter
Expand Down
4 changes: 3 additions & 1 deletion src/EPPlus.Export.Pdf/Settings/PdfPageSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Date Author Change
*************************************************************************************************/
using EPPlus.Export.Pdf.Settings.PdfPageSizes;
using EPPlus.Fonts.OpenType;
using OfficeOpenXml;
using OfficeOpenXml.Interfaces.Fonts;
using System.Collections.Generic;

Expand Down Expand Up @@ -241,6 +240,8 @@ internal double ContentScale
internal PdfContentBounds ContentBounds = new PdfContentBounds(PdfMargins.Normal, PdfPageSize.A4);
internal string defaultFontName = "";

internal string AdditionalContent;

//DEBUG
internal bool Debug = false;
internal bool PrintAsText = false;
Expand Down Expand Up @@ -270,6 +271,7 @@ internal PdfPageSettings CloneForSheet()
c.GposFeatures = GposFeatures;
c.Debug = Debug;
c.PrintAsText = PrintAsText;
c.AdditionalContent = AdditionalContent;
return c;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/EPPlus/Export/PdfExport/Data/PageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal struct Page
public double PrintTitleHeight;
public PdfCellCollection Map;
public PdfHeaderFooterCollection HeaderFooters;
public PdfAdditionalContent AddedContent;
public Dictionary<string, MergedCellDrawInfo> MergedCells;
public List<ImageDrawInfo> Images;
public List<PrintTitleCellDraw> PrintTitleCells;
Expand Down
1 change: 1 addition & 0 deletions src/EPPlus/Export/PdfExport/Data/PdfWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal class PdfWorksheet
public List<PdfRange> Ranges = null;
public PdfRange CommentsAndNotes;
public PdfHeaderFooterCollection HeaderFooters = null;
public PdfAdditionalContent AdditionalContent = null;
public double ZeroCharWidth;
public int ToRow;
public int PrintTitleRowFrom = -1;
Expand Down
Loading
Loading