Skip to content

Commit 521a300

Browse files
committed
refactor (outline): Minor refactor on outline, add test.
1 parent c0cd985 commit 521a300

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.IO;
2+
using FluentAssertions;
3+
using PdfSharpCore.Drawing;
4+
using PdfSharpCore.Pdf;
5+
using Xunit;
6+
7+
namespace PdfSharpCore.Test.Outlines
8+
{
9+
public class OutlineTests
10+
{
11+
[Fact]
12+
public void CanCreateDocumentWithOutlines()
13+
{
14+
var document = new PdfDocument();
15+
var font = new XFont("Verdana", 16);
16+
var page = document.AddPage();
17+
var gfx = XGraphics.FromPdfPage(page);
18+
gfx.DrawString("Page 1", font, XBrushes.Black, 20, 50, XStringFormats.Default);
19+
20+
// Create the root bookmark. You can set the style and the color.
21+
var outline = document.Outlines.Add("Root", page, true,
22+
PdfOutlineStyle.Bold, XColors.Red);
23+
24+
// Create some more pages
25+
for (var idx = 2; idx <= 5; idx++)
26+
{
27+
page = document.AddPage();
28+
gfx = XGraphics.FromPdfPage(page);
29+
30+
var text = $"Page {idx}";
31+
gfx.DrawString(text, font, XBrushes.Black, 20, 50, XStringFormats.Default);
32+
33+
// Create a sub bookmark
34+
outline.Outlines.Add(text, page, true);
35+
}
36+
37+
document.Outlines.Count.Should().Be(1);
38+
39+
using var ms = new MemoryStream();
40+
document.Save(ms);
41+
ms.ToArray().Length.Should().BeGreaterThan(1);
42+
}
43+
}
44+
}

PdfSharpCore/Pdf/PdfOutlineCollection.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace PdfSharpCore.Pdf
3939
/// <summary>
4040
/// Represents a collection of outlines.
4141
/// </summary>
42-
public class PdfOutlineCollection : PdfObject, ICollection<PdfOutline>, IList<PdfOutline>
42+
public class PdfOutlineCollection : PdfObject, IList<PdfOutline>
4343
{
4444
/// <summary>
4545
/// Can only be created as part of PdfOutline.
@@ -66,26 +66,20 @@ public bool Remove(PdfOutline item)
6666
/// <summary>
6767
/// Gets the number of entries in this collection.
6868
/// </summary>
69-
public int Count
70-
{
71-
get { return _outlines.Count; }
72-
}
69+
public int Count => _outlines.Count;
7370

7471
/// <summary>
7572
/// Returns false.
7673
/// </summary>
77-
public bool IsReadOnly
78-
{
79-
get { return false; }
80-
}
74+
public bool IsReadOnly => false;
8175

8276
/// <summary>
8377
/// Adds the specified outline.
8478
/// </summary>
8579
public void Add(PdfOutline outline)
8680
{
8781
if (outline == null)
88-
throw new ArgumentNullException("outline");
82+
throw new ArgumentNullException(nameof(outline));
8983

9084
// DestinationPage is optional. PDFsharp does not yet support outlines with action ("/A") instead of destination page ("/DEST")
9185
if (outline.DestinationPage != null && !ReferenceEquals(Owner, outline.DestinationPage.Owner))
@@ -209,9 +203,9 @@ public int IndexOf(PdfOutline item)
209203
public void Insert(int index, PdfOutline outline)
210204
{
211205
if (outline == null)
212-
throw new ArgumentNullException("outline");
206+
throw new ArgumentNullException(nameof(outline));
213207
if (index < 0 || index >= _outlines.Count)
214-
throw new ArgumentOutOfRangeException("index", index, PSSR.OutlineIndexOutOfRange);
208+
throw new ArgumentOutOfRangeException(nameof(index), index, PSSR.OutlineIndexOutOfRange);
215209

216210
AddToOutlinesTree(outline);
217211
_outlines.Insert(index, outline);
@@ -235,15 +229,15 @@ public PdfOutline this[int index]
235229
get
236230
{
237231
if (index < 0 || index >= _outlines.Count)
238-
throw new ArgumentOutOfRangeException("index", index, PSSR.OutlineIndexOutOfRange);
232+
throw new ArgumentOutOfRangeException(nameof(index), index, PSSR.OutlineIndexOutOfRange);
239233
return _outlines[index];
240234
}
241235
set
242236
{
243237
if (index < 0 || index >= _outlines.Count)
244-
throw new ArgumentOutOfRangeException("index", index, PSSR.OutlineIndexOutOfRange);
238+
throw new ArgumentOutOfRangeException(nameof(index), index, PSSR.OutlineIndexOutOfRange);
245239
if (value == null)
246-
throw new ArgumentOutOfRangeException("value", null, PSSR.SetValueMustNotBeNull);
240+
throw new ArgumentOutOfRangeException(nameof(value), null, PSSR.SetValueMustNotBeNull);
247241

248242
AddToOutlinesTree(value);
249243
_outlines[index] = value;
@@ -274,7 +268,7 @@ internal int CountOpen()
274268
void AddToOutlinesTree(PdfOutline outline)
275269
{
276270
if (outline == null)
277-
throw new ArgumentNullException("outline");
271+
throw new ArgumentNullException(nameof(outline));
278272

279273
// DestinationPage is optional. PDFsharp does not yet support outlines with action ("/A") instead of destination page ("/DEST")
280274
if (outline.DestinationPage != null && !ReferenceEquals(Owner, outline.DestinationPage.Owner))
@@ -287,10 +281,10 @@ void AddToOutlinesTree(PdfOutline outline)
287281
//_outlines.Add(outline);
288282
if (!Owner._irefTable.Contains(outline.ObjectID))
289283
Owner._irefTable.Add(outline);
290-
else
291-
{
292-
outline.GetType();
293-
}
284+
// else
285+
// {
286+
// outline.GetType();
287+
// }
294288

295289
//if (outline.Opened)
296290
//{
@@ -306,7 +300,7 @@ void AddToOutlinesTree(PdfOutline outline)
306300
void RemoveFromOutlinesTree(PdfOutline outline)
307301
{
308302
if (outline == null)
309-
throw new ArgumentNullException("outline");
303+
throw new ArgumentNullException(nameof(outline));
310304

311305
// TODO check the parent problems...
312306
//outline.Document = Owner;
@@ -316,7 +310,7 @@ void RemoveFromOutlinesTree(PdfOutline outline)
316310
}
317311

318312
/// <summary>
319-
/// The parent outine of this collection.
313+
/// The parent outline of this collection.
320314
/// </summary>
321315
readonly PdfOutline _parent;
322316

0 commit comments

Comments
 (0)