Skip to content

Commit 6033e93

Browse files
committed
Merge branch 'master' into EnhanceEncryption
2 parents 0f5aeaf + 2828a90 commit 6033e93

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+4258
-111
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using FluentAssertions;
3+
using PdfSharpCore.Pdf.Content.Objects;
4+
using Xunit;
5+
6+
namespace PdfSharpCore.Test.Pdfs.Content.Objects
7+
{
8+
public class CNameTests
9+
{
10+
[Theory]
11+
[InlineData("/Foo")]
12+
public void SetNameTests(string name)
13+
{
14+
var cName = new CName
15+
{
16+
Name = name
17+
};
18+
19+
cName.Name.Should().Be(name);
20+
}
21+
22+
[Fact]
23+
public void SetNameNullThrowsException()
24+
{
25+
Action act = () => new CName
26+
{
27+
Name = null
28+
};
29+
act.Should().Throw<ArgumentNullException>();
30+
}
31+
32+
[Theory]
33+
[InlineData("Foo")]
34+
[InlineData("")]
35+
public void SetNameWithoutPrefixThrowsException(string name)
36+
{
37+
Action act = () => new CName
38+
{
39+
Name = name
40+
};
41+
act.Should().Throw<ArgumentException>();
42+
}
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using FluentAssertions;
3+
using PdfSharpCore.Pdf;
4+
using Xunit;
5+
6+
namespace PdfSharpCore.Test.Pdfs
7+
{
8+
public class PdfDateTests
9+
{
10+
// format for pdf date is generally D:YYYYMMDDHHmmSSOHH'mm'
11+
12+
[Fact]
13+
public void ParseDateString_WithTimezoneOffset()
14+
{
15+
var pdfDate = new PdfDate("D:19981223195200-02'00'");
16+
var expectedDateWithOffset = new DateTimeOffset(new DateTime(1998, 12, 23, 19, 52, 0), new TimeSpan(-2, 0, 0));
17+
pdfDate.Value.ToUniversalTime().Should().Be(expectedDateWithOffset.UtcDateTime);
18+
}
19+
20+
[Fact]
21+
public void ParseDateString_WithNoOffset()
22+
{
23+
var pdfDate = new PdfDate("D:19981223195200Z");
24+
var expectedDateWithOffset = new DateTimeOffset(new DateTime(1998, 12, 23, 19, 52, 0), new TimeSpan(0, 0, 0));
25+
pdfDate.Value.ToUniversalTime().Should().Be(expectedDateWithOffset.UtcDateTime);
26+
}
27+
}
28+
}

PdfSharpCore/Pdf.Content.Objects/CObjects.cs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#region PDFsharp - A .NET library for processing PDF
2+
23
//
34
// Authors:
45
// Stefan Lange
@@ -25,6 +26,7 @@
2526
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2627
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2728
// DEALINGS IN THE SOFTWARE.
29+
2830
#endregion
2931

3032
using System;
@@ -35,7 +37,7 @@
3537
using System.IO;
3638
using System.Text;
3739

38-
namespace PdfSharpCore.Pdf.Content.Objects // TODO: split into single files
40+
namespace PdfSharpCore.Pdf.Content.Objects // TODO: split into single files
3941
{
4042
/// <summary>
4143
/// Base class for all PDF content stream objects.
@@ -46,7 +48,8 @@ public abstract class CObject : ICloneable
4648
/// Initializes a new instance of the <see cref="CObject"/> class.
4749
/// </summary>
4850
protected CObject()
49-
{ }
51+
{
52+
}
5053

5154
/// <summary>
5255
/// Creates a new object that is a copy of the current instance.
@@ -109,6 +112,7 @@ public string Text
109112
get { return _text; }
110113
set { _text = value; }
111114
}
115+
112116
string _text;
113117

114118
/// <summary>
@@ -129,7 +133,7 @@ internal override void WriteObject(ContentWriter writer)
129133
/// Represents a sequence of objects in a PDF content stream.
130134
/// </summary>
131135
[DebuggerDisplay("(count={Count})")]
132-
public class CSequence : CObject, IList<CObject> // , ICollection<CObject>, IEnumerable<CObject>
136+
public class CSequence : CObject, IList<CObject> // , ICollection<CObject>, IEnumerable<CObject>
133137
{
134138
/// <summary>
135139
/// Creates a new object that is a copy of the current instance.
@@ -250,6 +254,7 @@ public CObject this[int index]
250254
get { return (CObject)_items[index]; }
251255
set { _items[index] = value; }
252256
}
257+
253258
#endregion
254259

255260
#region ICollection Members
@@ -362,14 +367,8 @@ void IList<CObject>.RemoveAt(int index)
362367

363368
CObject IList<CObject>.this[int index]
364369
{
365-
get
366-
{
367-
throw new NotImplementedException();
368-
}
369-
set
370-
{
371-
throw new NotImplementedException();
372-
}
370+
get { throw new NotImplementedException(); }
371+
set { throw new NotImplementedException(); }
373372
}
374373

375374
#endregion
@@ -484,6 +483,7 @@ public int Value
484483
get { return _value; }
485484
set { _value = value; }
486485
}
486+
487487
int _value;
488488

489489
/// <summary>
@@ -531,6 +531,7 @@ public double Value
531531
get { return _value; }
532532
set { _value = value; }
533533
}
534+
534535
double _value;
535536

536537
/// <summary>
@@ -611,6 +612,7 @@ public string Value
611612
get { return _value; }
612613
set { _value = value; }
613614
}
615+
614616
string _value;
615617

616618
/// <summary>
@@ -621,6 +623,7 @@ public CStringType CStringType
621623
get { return _cStringType; }
622624
set { _cStringType = value; }
623625
}
626+
624627
CStringType _cStringType;
625628

626629
/// <summary>
@@ -687,6 +690,7 @@ public override string ToString()
687690
break;
688691
}
689692
}
693+
690694
s.Append(')');
691695
break;
692696

@@ -710,6 +714,7 @@ public override string ToString()
710714
default:
711715
throw new ArgumentOutOfRangeException();
712716
}
717+
713718
return s.ToString();
714719
}
715720

@@ -725,12 +730,14 @@ internal override void WriteObject(ContentWriter writer)
725730
[DebuggerDisplay("({Name})")]
726731
public class CName : CObject
727732
{
733+
private const string NamePrefix = "/";
734+
728735
/// <summary>
729736
/// Initializes a new instance of the <see cref="CName"/> class.
730737
/// </summary>
731738
public CName()
732739
{
733-
_name = "/";
740+
_name = NamePrefix;
734741
}
735742

736743
/// <summary>
@@ -760,21 +767,24 @@ protected override CObject Copy()
760767
}
761768

762769
/// <summary>
763-
/// Gets or sets the name. Names must start with a slash.
770+
/// Gets or sets the content stream name. Names must start with a slash.
764771
/// </summary>
772+
/// <exception cref="ArgumentNullException"></exception>
773+
/// <exception cref="ArgumentException">If <paramref name="value"/> does not start with a forward slash</exception>
765774
public string Name
766775
{
767-
get { return _name; }
776+
get => _name;
768777
set
769778
{
770-
if (String.IsNullOrEmpty(_name))
771-
throw new ArgumentNullException("name");
772-
if (_name[0] != '/')
773-
throw new ArgumentException(PSSR.NameMustStartWithSlash);
779+
if (string.IsNullOrEmpty(value))
780+
throw new ArgumentNullException(nameof(value));
781+
if (!value.StartsWith(NamePrefix))
782+
throw new ArgumentException(PSSR.NameMustStartWithSlash, nameof(value));
774783
_name = value;
775784
}
776785
}
777-
string _name;
786+
787+
private string _name;
778788

779789
/// <summary>
780790
/// Returns a string that represents the current value.
@@ -837,7 +847,8 @@ public class COperator : CObject
837847
/// Initializes a new instance of the <see cref="COperator"/> class.
838848
/// </summary>
839849
protected COperator()
840-
{ }
850+
{
851+
}
841852

842853
internal COperator(OpCode opcode)
843854
{
@@ -878,6 +889,7 @@ public CSequence Operands
878889
{
879890
get { return _seqence ?? (_seqence = new CSequence()); }
880891
}
892+
881893
CSequence _seqence;
882894

883895
/// <summary>
@@ -887,6 +899,7 @@ public OpCode OpCode
887899
{
888900
get { return _opcode; }
889901
}
902+
890903
readonly OpCode _opcode;
891904

892905

@@ -903,13 +916,14 @@ public override string ToString()
903916

904917
internal override void WriteObject(ContentWriter writer)
905918
{
906-
int count = _seqence != null ? _seqence.Count : 0;
919+
int count = _seqence?.Count ?? 0;
907920
for (int idx = 0; idx < count; idx++)
908921
{
909922
// ReSharper disable once PossibleNullReferenceException because the loop is not entered if _sequence is null
910923
_seqence[idx].WriteObject(writer);
911924
}
925+
912926
writer.WriteLineRaw(ToString());
913927
}
914928
}
915-
}
929+
}

PdfSharpCore/Pdf.IO/Parser.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,35 +1388,44 @@ private PdfTrailer ReadXRefStream(PdfCrossReferenceTable xrefTable)
13881388
/// <summary>
13891389
/// Parses a PDF date string.
13901390
/// </summary>
1391+
/// <remarks>
1392+
/// Format is
1393+
/// YYYY Year MM month DD day (01-31) HH hour (00-23) mm minute (00-59) ss second (00.59)
1394+
/// O is the relationship of local time to Universal Time (UT), denoted by one of the characters +, -, or Z (see below)
1395+
/// HH followed by ' is the absolute value of the offset from UT in hours (00-23)
1396+
/// mm followed by ' is the absolute value of the offset from UT in minutes (00-59)
1397+
/// For example, December 23, 1998, at 7:52 PM, U.S.Pacific Standard Time, is represented by the string,
1398+
/// D:19981223195200-08'00'
1399+
/// </remarks>
1400+
13911401
internal static DateTime ParseDateTime(string date, DateTime errorValue) // TODO: TryParseDateTime
13921402
{
13931403
DateTime datetime = errorValue;
13941404
try
13951405
{
13961406
if (date.StartsWith("D:"))
13971407
{
1398-
// Format is
13991408
// D:YYYYMMDDHHmmSSOHH'mm'
14001409
// ^2 ^10 ^16 ^20
14011410
int length = date.Length;
14021411
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, hh = 0, mm = 0;
14031412
char o = 'Z';
14041413
if (length >= 10)
14051414
{
1406-
year = Int32.Parse(date.Substring(2, 4));
1407-
month = Int32.Parse(date.Substring(6, 2));
1408-
day = Int32.Parse(date.Substring(8, 2));
1415+
year = int.Parse(date.Substring(2, 4));
1416+
month = int.Parse(date.Substring(6, 2));
1417+
day = int.Parse(date.Substring(8, 2));
14091418
if (length >= 16)
14101419
{
1411-
hour = Int32.Parse(date.Substring(10, 2));
1412-
minute = Int32.Parse(date.Substring(12, 2));
1413-
second = Int32.Parse(date.Substring(14, 2));
1420+
hour = int.Parse(date.Substring(10, 2));
1421+
minute = int.Parse(date.Substring(12, 2));
1422+
second = int.Parse(date.Substring(14, 2));
14141423
if (length >= 23)
14151424
{
14161425
if ((o = date[16]) != 'Z')
14171426
{
1418-
hh = Int32.Parse(date.Substring(17, 2));
1419-
mm = Int32.Parse(date.Substring(20, 2));
1427+
hh = int.Parse(date.Substring(17, 2));
1428+
mm = int.Parse(date.Substring(20, 2));
14201429
}
14211430
}
14221431
}
@@ -1433,7 +1442,7 @@ internal static DateTime ParseDateTime(string date, DateTime errorValue) // TOD
14331442
datetime = datetime.Subtract(ts);
14341443
}
14351444
// Now that we converted datetime to UTC, mark it as UTC.
1436-
DateTime.SpecifyKind(datetime, DateTimeKind.Utc);
1445+
datetime = DateTime.SpecifyKind(datetime, DateTimeKind.Utc);
14371446
}
14381447
else
14391448
{

PdfSharpCore/Pdf/PdfDate.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ namespace PdfSharpCore.Pdf
3939
[DebuggerDisplay("({Value})")]
4040
public sealed class PdfDate : PdfItem
4141
{
42-
/// <summary>
43-
/// Initializes a new instance of the <see cref="PdfDate"/> class.
44-
/// </summary>
45-
public PdfDate()
46-
{ }
47-
4842
/// <summary>
4943
/// Initializes a new instance of the <see cref="PdfDate"/> class.
5044
/// </summary>
@@ -64,20 +58,19 @@ public PdfDate(DateTime value)
6458
/// <summary>
6559
/// Gets the value as DateTime.
6660
/// </summary>
67-
public DateTime Value
68-
{
61+
public DateTime Value =>
6962
// This class must behave like a value type. Therefore it cannot be changed (like System.String).
70-
get { return _value; }
71-
}
72-
DateTime _value;
63+
_value;
64+
65+
readonly DateTime _value;
7366

7467
/// <summary>
7568
/// Returns the value in the PDF date format.
7669
/// </summary>
7770
public override string ToString()
7871
{
79-
string delta = _value.ToString("zzz").Replace(':', '\'');
80-
return String.Format("D:{0:yyyyMMddHHmmss}{1}'", _value, delta);
72+
var delta = _value.ToString("zzz").Replace(':', '\'');
73+
return $"D:{_value:yyyyMMddHHmmss}{delta}'";
8174
}
8275

8376
/// <summary>

0 commit comments

Comments
 (0)