Skip to content

Commit

Permalink
Added extra settings to the PdfWriteDefines (#1202)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Jun 19, 2022
1 parent 349b190 commit 6fe1393
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Magick.NET/Formats/Pdf/PdfWriteDefines.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Collections.Generic;
using ImageMagick.Defines;

Expand All @@ -24,11 +25,36 @@ public PdfWriteDefines()
/// </summary>
public string? Author { get; set; }

/// <summary>
/// Gets or sets the creation time of the pdf document (pdf:create-epoch).
/// </summary>
public DateTime? CreationTime { get; set; }

/// <summary>
/// Gets or sets the creator of the pdf document (pdf:creator).
/// </summary>
public string? Creator { get; set; }

/// <summary>
/// Gets or sets the keywords of the pdf document (pdf:keywords).
/// </summary>
public string? Keywords { get; set; }

/// <summary>
/// Gets or sets the modification time of the pdf document (pdf:modify-epoch).
/// </summary>
public DateTime? ModificationTime { get; set; }

/// <summary>
/// Gets or sets the producer of the pdf document (pdf:producer).
/// </summary>
public string? Producer { get; set; }

/// <summary>
/// Gets or sets the subject of the pdf document (pdf:subject).
/// </summary>
public string? Subject { get; set; }

/// <summary>
/// Gets or sets the title of the pdf document (pdf:title).
/// </summary>
Expand All @@ -44,12 +70,38 @@ public override IEnumerable<IDefine> Defines
if (Author?.Length > 0)
yield return CreateDefine("author", Author);

if (CreationTime is not null)
yield return CreateDefine("create-epoch", ToUnixTimeSeconds(CreationTime.Value));

if (Creator?.Length > 0)
yield return CreateDefine("creator", Creator);

if (Keywords?.Length > 0)
yield return CreateDefine("keywords", Keywords);

if (ModificationTime is not null)
yield return CreateDefine("modify-epoch", ToUnixTimeSeconds(ModificationTime.Value));

if (Producer?.Length > 0)
yield return CreateDefine("producer", Producer);

if (Subject?.Length > 0)
yield return CreateDefine("subject", Subject);

if (Title?.Length > 0)
yield return CreateDefine("title", Title);
}
}

private static long ToUnixTimeSeconds(DateTime value)
{
var dateTimeOffset = (DateTimeOffset)value.ToUniversalTime();
#if NET20
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return (long)(dateTimeOffset - epoch).TotalSeconds;
#else
return dateTimeOffset.ToUnixTimeSeconds();
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ public void ShouldNotSetAnyDefine()
image.Settings.SetDefines(new PdfWriteDefines());

Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "author"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "create-epoch"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "creator"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "keywords"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "modify-epoch"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "producer"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "subject"));
Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "title"));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using System;
using ImageMagick;
using ImageMagick.Formats;
using Xunit;

namespace Magick.NET.Tests
{
public partial class PdfWriteDefinesTests
{
public class TheCreationTimeProperty
{
[Fact]
public void ShouldSetTheDefineWhenValueIsSet()
{
using (var image = new MagickImage(MagickColors.Magenta, 1, 1))
{
image.Settings.SetDefines(new PdfWriteDefines
{
CreationTime = new DateTime(1990, 1, 2, 3, 4, 5),
});

Assert.Equal("631245845", image.Settings.GetDefine(MagickFormat.Pdf, "create-epoch"));
}
}

[Fact]
public void ShouldNotSetTheDefineWhenValueIsNotSet()
{
using (var image = new MagickImage())
{
image.Settings.SetDefines(new PdfWriteDefines
{
CreationTime = null,
});

Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "create-epoch"));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using ImageMagick;
using ImageMagick.Formats;
using Xunit;

namespace Magick.NET.Tests
{
public partial class PdfWriteDefinesTests
{
public class TheCreatorProperty
{
[Fact]
public void ShouldSetTheDefineWhenValueIsSet()
{
using (var image = new MagickImage(MagickColors.Magenta, 1, 1))
{
image.Settings.SetDefines(new PdfWriteDefines
{
Creator = "magick",
});

Assert.Equal("magick", image.Settings.GetDefine(MagickFormat.Pdf, "creator"));
}
}

[Fact]
public void ShouldNotSetTheDefineWhenValueIsNotSet()
{
using (var image = new MagickImage())
{
image.Settings.SetDefines(new PdfWriteDefines
{
Creator = null,
});

Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "creator"));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using ImageMagick;
using ImageMagick.Formats;
using Xunit;

namespace Magick.NET.Tests
{
public partial class PdfWriteDefinesTests
{
public class TheKeywordsProperty
{
[Fact]
public void ShouldSetTheDefineWhenValueIsSet()
{
using (var image = new MagickImage(MagickColors.Magenta, 1, 1))
{
image.Settings.SetDefines(new PdfWriteDefines
{
Keywords = "magick",
});

Assert.Equal("magick", image.Settings.GetDefine(MagickFormat.Pdf, "keywords"));
}
}

[Fact]
public void ShouldNotSetTheDefineWhenValueIsNotSet()
{
using (var image = new MagickImage())
{
image.Settings.SetDefines(new PdfWriteDefines
{
Keywords = null,
});

Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "keywords"));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using System;
using ImageMagick;
using ImageMagick.Formats;
using Xunit;

namespace Magick.NET.Tests
{
public partial class PdfWriteDefinesTests
{
public class TheModificationTimeProperty
{
[Fact]
public void ShouldSetTheDefineWhenValueIsSet()
{
using (var image = new MagickImage(MagickColors.Magenta, 1, 1))
{
image.Settings.SetDefines(new PdfWriteDefines
{
ModificationTime = new DateTime(2000, 1, 2, 3, 4, 5),
});

Assert.Equal("946778645", image.Settings.GetDefine(MagickFormat.Pdf, "modify-epoch"));
}
}

[Fact]
public void ShouldNotSetTheDefineWhenValueIsNotSet()
{
using (var image = new MagickImage())
{
image.Settings.SetDefines(new PdfWriteDefines
{
ModificationTime = null,
});

Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "modify-epoch"));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using ImageMagick;
using ImageMagick.Formats;
using Xunit;

namespace Magick.NET.Tests
{
public partial class PdfWriteDefinesTests
{
public class TheSubjectProperty
{
[Fact]
public void ShouldSetTheDefineWhenValueIsSet()
{
using (var image = new MagickImage(MagickColors.Magenta, 1, 1))
{
image.Settings.SetDefines(new PdfWriteDefines
{
Subject = "magick",
});

Assert.Equal("magick", image.Settings.GetDefine(MagickFormat.Pdf, "subject"));
}
}

[Fact]
public void ShouldNotSetTheDefineWhenValueIsNotSet()
{
using (var image = new MagickImage())
{
image.Settings.SetDefines(new PdfWriteDefines
{
Subject = null,
});

Assert.Null(image.Settings.GetDefine(MagickFormat.Pdf, "subject"));
}
}
}
}
}

0 comments on commit 6fe1393

Please sign in to comment.