Skip to content

Commit 9666c7f

Browse files
committed
Use unicode for encoding document metadata
1 parent 2828a90 commit 9666c7f

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

PdfSharpCore.Test/CreateSimplePDF.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ public void CreateTestPdf()
3737
ValidateFileIsPdf(outName);
3838
}
3939

40+
[Fact]
41+
public void CreateTestPdfWithUnicodeMetadata()
42+
{
43+
const string data = "English, Ελληνικά, 漢語";
44+
45+
var document = new PdfDocument();
46+
document.Info.Title = data;
47+
document.Info.Subject = data;
48+
document.Info.Author = data;
49+
50+
using var ms = new MemoryStream();
51+
document.AddPage();
52+
document.Save(ms);
53+
ms.Position = 0;
54+
55+
PdfDocument generatedDocument = Pdf.IO.PdfReader.Open(ms);
56+
57+
Assert.Equal(data, generatedDocument.Info.Title);
58+
Assert.Equal(data, generatedDocument.Info.Subject);
59+
Assert.Equal(data, generatedDocument.Info.Author);
60+
}
61+
4062
[Fact]
4163
public void CreateTestPdfWithImage()
4264
{

PdfSharpCore/Pdf/PdfDictionary.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,14 @@ public void SetString(string key, string value)
586586
this[key] = new PdfString(value);
587587
}
588588

589+
/// <summary>
590+
/// Sets the entry to a string value with the specified encoding.
591+
/// </summary>
592+
public void SetString(string key, string value, PdfStringEncoding encoding)
593+
{
594+
this[key] = new PdfString(value, encoding);
595+
}
596+
589597
/// <summary>
590598
/// Converts the specified value to a name.
591599
/// If the value does not exist, the function returns the empty string.

PdfSharpCore/Pdf/PdfDocumentInformation.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal PdfDocumentInformation(PdfDictionary dict)
5353
public string Title
5454
{
5555
get { return Elements.GetString(Keys.Title); }
56-
set { Elements.SetString(Keys.Title, value); }
56+
set { Elements.SetString(Keys.Title, value, PdfStringEncoding.Unicode); }
5757
}
5858

5959
/// <summary>
@@ -62,7 +62,7 @@ public string Title
6262
public string Author
6363
{
6464
get { return Elements.GetString(Keys.Author); }
65-
set { Elements.SetString(Keys.Author, value); }
65+
set { Elements.SetString(Keys.Author, value, PdfStringEncoding.Unicode); }
6666
}
6767

6868
/// <summary>
@@ -71,7 +71,7 @@ public string Author
7171
public string Subject
7272
{
7373
get { return Elements.GetString(Keys.Subject); }
74-
set { Elements.SetString(Keys.Subject, value); }
74+
set { Elements.SetString(Keys.Subject, value, PdfStringEncoding.Unicode); }
7575
}
7676

7777
/// <summary>
@@ -80,7 +80,7 @@ public string Subject
8080
public string Keywords
8181
{
8282
get { return Elements.GetString(Keys.Keywords); }
83-
set { Elements.SetString(Keys.Keywords, value); }
83+
set { Elements.SetString(Keys.Keywords, value, PdfStringEncoding.Unicode); }
8484
}
8585

8686
/// <summary>
@@ -89,7 +89,7 @@ public string Keywords
8989
public string Creator
9090
{
9191
get { return Elements.GetString(Keys.Creator); }
92-
set { Elements.SetString(Keys.Creator, value); }
92+
set { Elements.SetString(Keys.Creator, value, PdfStringEncoding.Unicode); }
9393
}
9494

9595
/// <summary>

0 commit comments

Comments
 (0)