Skip to content

Commit d551dc5

Browse files
committed
Add metadata to the stamped PDF file
1 parent f568abf commit d551dc5

File tree

8 files changed

+780
-680
lines changed

8 files changed

+780
-680
lines changed

src/iTextSharp.LGPLv2.Core.FunctionalTests/PDFStampingTests.cs

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,92 @@ namespace iTextSharp.LGPLv2.Core.FunctionalTests;
1010
public class StampRequestForm
1111
{
1212
public IList<string> Lines { get; set; }
13+
1314
public float LowerLeftX { get; set; }
15+
1416
public float LowerLeftY { get; set; }
17+
1518
public float UpperRightX { get; set; }
19+
1620
public float UpperRightY { get; set; }
21+
1722
public int RotationDegree { get; set; }
1823
}
1924

2025
[TestClass]
2126
public class PDFStampingTests
2227
{
28+
[TestMethod]
29+
public void Verify_Metadata_Can_Be_Added_To_Stamped_File()
30+
{
31+
var outputFilePath = TestUtils.GetOutputFileName();
32+
33+
using (var reader = new PdfReader(TestUtils.GetPdfsPath(fileName: "sample.pdf")))
34+
{
35+
using var outStream = new FileStream(outputFilePath, FileMode.Create);
36+
using var stamper = new PdfStamper(reader, outStream);
37+
stamper.AddAuthor(author: "Test");
38+
stamper.AddKeywords(keywords: "C#");
39+
stamper.AddTitle(title: "New doc");
40+
stamper.AddSubject(subject: "New doc");
41+
stamper.AddCreator(creator: "MyApp");
42+
43+
stamper.ViewerPreferences = PdfWriter.PageModeUseOutlines;
44+
stamper.SetFullCompression();
45+
}
46+
47+
using (var reader = new PdfReader(outputFilePath))
48+
{
49+
Assert.AreEqual(expected: "Test", reader.Info[key: "Author"]);
50+
Assert.AreEqual(expected: "New doc", reader.Info[key: "Title"]);
51+
}
52+
}
53+
2354
[TestMethod]
2455
public void Verify_Stamped_File_CanBeCreated()
2556
{
26-
using var inputPdfStream = new FileStream(TestUtils.GetPdfsPath("sample.pdf"), FileMode.Open);
57+
using var inputPdfStream = new FileStream(TestUtils.GetPdfsPath(fileName: "sample.pdf"), FileMode.Open);
2758
using var outStream = new FileStream(TestUtils.GetOutputFileName(), FileMode.Create);
2859

2960
using var reader = new PdfReader(inputPdfStream);
3061
using var stamper = new PdfStamper(reader, outStream);
31-
var dc = stamper.GetOverContent(1);
62+
var dc = stamper.GetOverContent(pageNum: 1);
63+
3264
AddWaterMark(dc, new StampRequestForm
33-
{
34-
Lines = new[]
35-
{
36-
"This is a sample stamp",
37-
DateTime.Now.ToShortDateString(),
38-
"https://github.com/VahidN",
39-
},
40-
// position the stamp text near the top right.
41-
LowerLeftX = 402,
42-
LowerLeftY = 600,
43-
UpperRightX = 575,
44-
UpperRightY = 900,
45-
});
65+
{
66+
Lines = new[]
67+
{
68+
"This is a sample stamp", DateTime.Now.ToShortDateString(), "https://github.com/VahidN"
69+
},
70+
71+
// position the stamp text near the top right.
72+
LowerLeftX = 402,
73+
LowerLeftY = 600,
74+
UpperRightX = 575,
75+
UpperRightY = 900
76+
});
4677

4778
Assert.IsNotNull(outStream);
4879
}
4980

5081
private void AddWaterMark(PdfContentByte dc, StampRequestForm stampRequest)
5182
{
5283
dc.SaveState();
84+
5385
dc.SetGState(new PdfGState
54-
{
55-
FillOpacity = 0.61f,
56-
StrokeOpacity = 0.61f,
57-
});
86+
{
87+
FillOpacity = 0.61f,
88+
StrokeOpacity = 0.61f
89+
});
90+
5891
dc.SetColorFill(BaseColor.Red);
5992
dc.BeginText();
60-
dc.SetFontAndSize(BaseFont.CreateFont(), 7);
93+
dc.SetFontAndSize(BaseFont.CreateFont(), size: 7);
6194
var x = (stampRequest.LowerLeftX + stampRequest.UpperRightX) / 2;
6295
var y = (stampRequest.LowerLeftY + stampRequest.UpperRightY) / 2;
6396

6497
var verticalSpaceBetweenLines = 15;
98+
6599
foreach (var line in stampRequest.Lines)
66100
{
67101
dc.ShowTextAligned(Element.ALIGN_CENTER, line, x, y, stampRequest.RotationDegree);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dotnet test -p:RunAnalyzers=false
2+
pause

src/iTextSharp.LGPLv2.Core/iTextSharp.LGPLv2.Core.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<Description>iTextSharp.LGPLv2.Core is an unofficial port of the last LGPL version of the iTextSharp (V4.1.6) to .NET Core.</Description>
4-
<VersionPrefix>3.5.1</VersionPrefix>
4+
<VersionPrefix>3.6.0</VersionPrefix>
55
<Authors>Vahid Nasiri</Authors>
66
<TargetFrameworks>net9.0;net8.0;net7.0;net6.0;netstandard2.0;net462;</TargetFrameworks>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -57,11 +57,11 @@
5757
<PrivateAssets>all</PrivateAssets>
5858
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
5959
</PackageReference>
60-
<PackageReference Include="Meziantou.Analyzer" Version="2.0.184">
60+
<PackageReference Include="Meziantou.Analyzer" Version="2.0.186">
6161
<PrivateAssets>all</PrivateAssets>
6262
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
6363
</PackageReference>
64-
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.4.0.108396">
64+
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.5.0.109200">
6565
<PrivateAssets>all</PrivateAssets>
6666
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
6767
</PackageReference>

src/iTextSharp.LGPLv2.Core/iTextSharp/text/Document.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,11 @@ public void Dispose()
520520
/// Adds the current date and time to a Document.
521521
/// </summary>
522522
/// <returns>true if successful, false otherwise</returns>
523-
public bool AddCreationDate()
523+
public bool AddCreationDate(DateTime? dateTime = null)
524524
=> Add(new Meta(Element.CREATIONDATE,
525-
DateTime.Now.ToString(format: "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture)));
525+
dateTime.HasValue
526+
? dateTime.Value.ToString(format: "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture)
527+
: DateTime.Now.ToString(format: "ddd MMM dd HH:mm:ss zzz yyyy", CultureInfo.InvariantCulture)));
526528

527529
/// <summary>
528530
/// Adds the creator to a Document.
@@ -556,7 +558,7 @@ public bool AddCreationDate()
556558
/// Adds the producer to a Document.
557559
/// </summary>
558560
/// <returns>true if successful, false otherwise</returns>
559-
public bool AddProducer() => Add(new Meta(Element.PRODUCER, Version));
561+
public bool AddProducer(string producer = null) => Add(new Meta(Element.PRODUCER, producer ?? Version));
560562

561563
/// <summary>
562564
/// Adds the subject to a Document.

src/iTextSharp.LGPLv2.Core/iTextSharp/text/ElementTags.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public static class ElementTags
6666
/// <summary> attribute of the root tag </summary>
6767
public const string AUTHOR = "author";
6868

69+
/// <summary> attribute of the root tag </summary>
70+
public const string CREATOR = "creator";
71+
6972
/// <summary>
7073
/// a possible list attribute
7174
/// </summary>

src/iTextSharp.LGPLv2.Core/iTextSharp/text/pdf/PdfDocument.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3681,12 +3681,6 @@ internal void AddCreationDate()
36813681
/// </summary>
36823682
internal void Addkey(string key, string value)
36833683
{
3684-
if (key.Equals(value: "Producer", StringComparison.Ordinal) ||
3685-
key.Equals(value: "CreationDate", StringComparison.Ordinal))
3686-
{
3687-
return;
3688-
}
3689-
36903684
Put(new PdfName(key), new PdfString(value, TEXT_UNICODE));
36913685
}
36923686

0 commit comments

Comments
 (0)