|
@@ -10,6 +10,34 @@ using static System.String;
|
|
|
|
|
|
|
|
namespace QuestPDF.Fluent
|
|
namespace QuestPDF.Fluent
|
|
|
{
|
|
{
|
|
|
|
|
+ public class TextSpanDescriptor
|
|
|
|
|
+ {
|
|
|
|
|
+ internal TextStyle TextStyle { get; }
|
|
|
|
|
+
|
|
|
|
|
+ internal TextSpanDescriptor(TextStyle textStyle)
|
|
|
|
|
+ {
|
|
|
|
|
+ TextStyle = textStyle;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public delegate string PageNumberFormatter(int? pageNumber);
|
|
|
|
|
+
|
|
|
|
|
+ public class TextPageNumberDescriptor : TextSpanDescriptor
|
|
|
|
|
+ {
|
|
|
|
|
+ internal PageNumberFormatter FormatFunction { get; private set; } = x => (x ?? 123).ToString();
|
|
|
|
|
+
|
|
|
|
|
+ internal TextPageNumberDescriptor(TextStyle textStyle) : base(textStyle)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public TextPageNumberDescriptor Format(PageNumberFormatter formatter)
|
|
|
|
|
+ {
|
|
|
|
|
+ FormatFunction = formatter ?? FormatFunction;
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public class TextDescriptor
|
|
public class TextDescriptor
|
|
|
{
|
|
{
|
|
|
private ICollection<TextBlock> TextBlocks { get; } = new List<TextBlock>();
|
|
private ICollection<TextBlock> TextBlocks { get; } = new List<TextBlock>();
|
|
@@ -22,6 +50,11 @@ namespace QuestPDF.Fluent
|
|
|
DefaultStyle = style;
|
|
DefaultStyle = style;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public void DefaultTextStyle(Func<TextStyle, TextStyle> style)
|
|
|
|
|
+ {
|
|
|
|
|
+ DefaultStyle = style(TextStyle.Default);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public void AlignLeft()
|
|
public void AlignLeft()
|
|
|
{
|
|
{
|
|
|
Alignment = HorizontalAlignment.Left;
|
|
Alignment = HorizontalAlignment.Left;
|
|
@@ -50,12 +83,15 @@ namespace QuestPDF.Fluent
|
|
|
TextBlocks.Last().Items.Add(item);
|
|
TextBlocks.Last().Items.Add(item);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void Span(string? text, TextStyle? style = null)
|
|
|
|
|
|
|
+ [Obsolete("This element has been renamed since version 2022.3. Please use the overload that returns a TextSpanDescriptor object which allows to specify text style.")]
|
|
|
|
|
+ public void Span(string? text, TextStyle style)
|
|
|
{
|
|
{
|
|
|
- if (IsNullOrEmpty(text))
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- style ??= TextStyle.Default;
|
|
|
|
|
|
|
+ Span(text).Style(style);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public TextSpanDescriptor Span(string? text)
|
|
|
|
|
+ {
|
|
|
|
|
+ var style = DefaultStyle.Clone();
|
|
|
|
|
|
|
|
var items = text
|
|
var items = text
|
|
|
.Replace("\r", string.Empty)
|
|
.Replace("\r", string.Empty)
|
|
@@ -77,85 +113,123 @@ namespace QuestPDF.Fluent
|
|
|
})
|
|
})
|
|
|
.ToList()
|
|
.ToList()
|
|
|
.ForEach(TextBlocks.Add);
|
|
.ForEach(TextBlocks.Add);
|
|
|
|
|
+
|
|
|
|
|
+ return new TextSpanDescriptor(style);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void Line(string? text, TextStyle? style = null)
|
|
|
|
|
|
|
+ public TextSpanDescriptor Line(string? text)
|
|
|
{
|
|
{
|
|
|
text ??= string.Empty;
|
|
text ??= string.Empty;
|
|
|
- Span(text + Environment.NewLine, style);
|
|
|
|
|
|
|
+ return Span(text + Environment.NewLine);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void EmptyLine()
|
|
|
|
|
|
|
+ public TextSpanDescriptor EmptyLine()
|
|
|
{
|
|
{
|
|
|
- Span(Environment.NewLine);
|
|
|
|
|
|
|
+ return Span(Environment.NewLine);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- private void PageNumber(string slotName, TextStyle? style = null)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private TextPageNumberDescriptor PageNumber(Func<IPageContext, int?> pageNumber)
|
|
|
{
|
|
{
|
|
|
- if (IsNullOrEmpty(slotName))
|
|
|
|
|
- throw new ArgumentException(nameof(slotName));
|
|
|
|
|
-
|
|
|
|
|
- style ??= TextStyle.Default;
|
|
|
|
|
|
|
+ var style = DefaultStyle.Clone();
|
|
|
|
|
+ var descriptor = new TextPageNumberDescriptor(DefaultStyle);
|
|
|
|
|
|
|
|
- AddItemToLastTextBlock(new TextBlockPageNumber()
|
|
|
|
|
|
|
+ AddItemToLastTextBlock(new TextBlockPageNumber
|
|
|
{
|
|
{
|
|
|
- Style = style,
|
|
|
|
|
- SlotName = slotName
|
|
|
|
|
|
|
+ Source = context => descriptor.FormatFunction(pageNumber(context)),
|
|
|
|
|
+ Style = style
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ return descriptor;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public TextPageNumberDescriptor CurrentPageNumber()
|
|
|
|
|
+ {
|
|
|
|
|
+ return PageNumber(x => x.CurrentPage);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void CurrentPageNumber(TextStyle? style = null)
|
|
|
|
|
|
|
+ public TextPageNumberDescriptor TotalPages()
|
|
|
{
|
|
{
|
|
|
- PageNumber(PageContext.CurrentPageSlot, style);
|
|
|
|
|
|
|
+ return PageNumber(x => x.GetLocation(PageContext.DocumentLocation)?.Length);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ [Obsolete("This element has been renamed since version 2022.3. Please use the BeginPageNumberOfSection method.")]
|
|
|
|
|
+ public void PageNumberOfLocation(string locationName, TextStyle? style = null)
|
|
|
|
|
+ {
|
|
|
|
|
+ BeginPageNumberOfSection(locationName).Style(style);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void TotalPages(TextStyle? style = null)
|
|
|
|
|
|
|
+ public TextPageNumberDescriptor BeginPageNumberOfSection(string locationName)
|
|
|
{
|
|
{
|
|
|
- PageNumber(PageContext.TotalPagesSlot, style);
|
|
|
|
|
|
|
+ return PageNumber(x => x.GetLocation(locationName)?.PageStart);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void PageNumberOfLocation(string locationName, TextStyle? style = null)
|
|
|
|
|
|
|
+ public TextPageNumberDescriptor EndPageNumberOfSection(string locationName)
|
|
|
{
|
|
{
|
|
|
- if (IsNullOrEmpty(locationName))
|
|
|
|
|
- throw new ArgumentException(nameof(locationName));
|
|
|
|
|
-
|
|
|
|
|
- PageNumber(locationName, style);
|
|
|
|
|
|
|
+ return PageNumber(x => x.GetLocation(locationName)?.PageEnd);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void InternalLocation(string? text, string locationName, TextStyle? style = null)
|
|
|
|
|
|
|
+ public TextPageNumberDescriptor PageNumberWithinSection(string locationName)
|
|
|
{
|
|
{
|
|
|
- if (IsNullOrEmpty(text))
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
- if (IsNullOrEmpty(locationName))
|
|
|
|
|
- throw new ArgumentException(nameof(locationName));
|
|
|
|
|
|
|
+ return PageNumber(x => x.CurrentPage + 1 - x.GetLocation(locationName)?.PageEnd);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public TextPageNumberDescriptor TotalPagesWithinSection(string locationName)
|
|
|
|
|
+ {
|
|
|
|
|
+ return PageNumber(x => x.GetLocation(locationName)?.Length);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public TextSpanDescriptor SectionLink(string? text, string sectionName)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (IsNullOrEmpty(sectionName))
|
|
|
|
|
+ throw new ArgumentException(nameof(sectionName));
|
|
|
|
|
|
|
|
- style ??= TextStyle.Default;
|
|
|
|
|
|
|
+ var style = DefaultStyle.Clone();
|
|
|
|
|
+ var descriptor = new TextSpanDescriptor(style);
|
|
|
|
|
|
|
|
- AddItemToLastTextBlock(new TextBlockInternalLink
|
|
|
|
|
|
|
+ if (IsNullOrEmpty(text))
|
|
|
|
|
+ return descriptor;
|
|
|
|
|
+
|
|
|
|
|
+ AddItemToLastTextBlock(new TextBlockSectionlLink
|
|
|
{
|
|
{
|
|
|
Style = style,
|
|
Style = style,
|
|
|
Text = text,
|
|
Text = text,
|
|
|
- LocationName = locationName
|
|
|
|
|
|
|
+ SectionName = sectionName
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ return descriptor;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void ExternalLocation(string? text, string url, TextStyle? style = null)
|
|
|
|
|
|
|
+ [Obsolete("This element has been renamed since version 2022.3. Please use the SectionLink method.")]
|
|
|
|
|
+ public void InternalLocation(string? text, string locationName, TextStyle? style = null)
|
|
|
|
|
+ {
|
|
|
|
|
+ SectionLink(text, locationName).Style(style);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public TextSpanDescriptor Hyperlink(string? text, string url)
|
|
|
{
|
|
{
|
|
|
- if (IsNullOrEmpty(text))
|
|
|
|
|
- return;
|
|
|
|
|
-
|
|
|
|
|
if (IsNullOrEmpty(url))
|
|
if (IsNullOrEmpty(url))
|
|
|
throw new ArgumentException(nameof(url));
|
|
throw new ArgumentException(nameof(url));
|
|
|
|
|
+
|
|
|
|
|
+ var style = DefaultStyle.Clone();
|
|
|
|
|
+ var descriptor = new TextSpanDescriptor(style);
|
|
|
|
|
+
|
|
|
|
|
+ if (IsNullOrEmpty(text))
|
|
|
|
|
+ return descriptor;
|
|
|
|
|
|
|
|
- style ??= TextStyle.Default;
|
|
|
|
|
-
|
|
|
|
|
- AddItemToLastTextBlock(new TextBlockExternalLink
|
|
|
|
|
|
|
+ AddItemToLastTextBlock(new TextBlockHyperlink
|
|
|
{
|
|
{
|
|
|
Style = style,
|
|
Style = style,
|
|
|
Text = text,
|
|
Text = text,
|
|
|
Url = url
|
|
Url = url
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ return descriptor;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ [Obsolete("This element has been renamed since version 2022.3. Please use the Hyperlink method.")]
|
|
|
|
|
+ public void ExternalLocation(string? text, string url, TextStyle? style = null)
|
|
|
|
|
+ {
|
|
|
|
|
+ Hyperlink(text, url).Style(style);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public IContainer Element()
|
|
public IContainer Element()
|
|
@@ -197,9 +271,17 @@ namespace QuestPDF.Fluent
|
|
|
descriptor.Compose(element);
|
|
descriptor.Compose(element);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static void Text(this IContainer element, object? text, TextStyle? style = null)
|
|
|
|
|
|
|
+ [Obsolete("This element has been renamed since version 2022.3. Please use the overload that returns a TextSpanDescriptor object which allows to specify text style.")]
|
|
|
|
|
+ public static void Text(this IContainer element, object? text, TextStyle style)
|
|
|
|
|
+ {
|
|
|
|
|
+ element.Text(text).Style(style);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static TextSpanDescriptor Text(this IContainer element, object? text)
|
|
|
{
|
|
{
|
|
|
- element.Text(x => x.Span(text?.ToString(), style));
|
|
|
|
|
|
|
+ var descriptor = (TextSpanDescriptor) null;
|
|
|
|
|
+ element.Text(x => descriptor = x.Span(text?.ToString()));
|
|
|
|
|
+ return descriptor;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|