Skip to content

Commit 978cd37

Browse files
authored
Merge pull request #38 from synercoder/develop
Added shape support
2 parents a6bef8d + c2a2c06 commit 978cd37

File tree

95 files changed

+4970
-1338
lines changed

Some content is hidden

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

95 files changed

+4970
-1338
lines changed

Directory.Build.props

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@
4848
</PropertyGroup>
4949

5050
<Choose>
51-
<When Condition="'$(TargetFramework)' == 'net472'">
52-
<PropertyGroup>
53-
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
54-
</PropertyGroup>
55-
</When>
56-
<When Condition="'$(TargetFramework)' == 'netstandard2.0'">
57-
<PropertyGroup>
58-
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
59-
</PropertyGroup>
60-
</When>
6151
<When Condition="'$(TargetFramework)' == 'netstandard2.1'">
6252
<PropertyGroup>
6353
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
@@ -66,19 +56,6 @@
6656
<DefineConstants>$(DefineConstants);SUPPORTS_NULLABLEREFATTRIBUTES</DefineConstants>
6757
</PropertyGroup>
6858
</When>
69-
<When Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
70-
<PropertyGroup>
71-
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
72-
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
73-
</PropertyGroup>
74-
</When>
75-
<When Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
76-
<PropertyGroup>
77-
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
78-
<DefineConstants>$(DefineConstants);SUPPORTS_HASHCODE</DefineConstants>
79-
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
80-
</PropertyGroup>
81-
</When>
8259
<When Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
8360
<PropertyGroup>
8461
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Gerard Gunnewijk
3+
Copyright (c) 2021 Gerard Gunnewijk
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This project is licensed under MIT license.
1515
## Specifications used
1616
This library was created using the specifications lay out in ["PDF 32000-1:2008, Document management – Portable document format – Part 1: PDF 1.7"](https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf).
1717

18-
The full specifications are not implemented. This library currently only supports placements of images and setting the different boxes.
18+
The full specifications are not implemented. This library currently only supports placements of images, drawing of vector shapes (CMYK, RGB & gray scale), and setting the different boxes.
1919

2020
## Remarks
2121
Unlike most PDF libraries this library does not create the entire PDF model in memory before writing the PDF to a (file)stream. Most libaries support editing capabilities, because this libary only supports creating files, it was not necessary to keep the PDF model in memory. This results in less memory usage.

samples/Synercoding.FileFormats.Pdf.ConsoleTester/Program.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using Synercoding.FileFormats.Pdf.Primitives;
1+
using Synercoding.FileFormats.Pdf.Extensions;
2+
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
3+
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
24
using Synercoding.Primitives;
35
using Synercoding.Primitives.Extensions;
6+
using System;
47
using System.IO;
58

69
namespace Synercoding.FileFormats.Pdf.ConsoleTester
@@ -53,6 +56,45 @@ public static void Main(string[] args)
5356
page.AddImage(eyeStream, new Rectangle(offSet, offSet, width + offSet, height + offSet, Unit.Millimeters));
5457
}
5558
})
59+
// Test shape graphics
60+
.AddPage(page =>
61+
{
62+
page.AddShapes(ctx =>
63+
{
64+
ctx.DefaultState(g =>
65+
{
66+
g.LineWidth = 1;
67+
g.Fill = null;
68+
g.Stroke = null;
69+
g.Dash = new Dash()
70+
{
71+
Array = Array.Empty<double>(),
72+
Phase = 0
73+
};
74+
g.MiterLimit = 10;
75+
g.LineCap = LineCapStyle.ButtCap;
76+
g.LineJoin = LineJoinStyle.MiterJoin;
77+
});
78+
79+
ctx.NewPath(g => { g.Fill = PredefinedColors.Red; g.Stroke = PredefinedColors.Black; g.LineWidth = 5; })
80+
.Move(100, 100)
81+
.LineTo(200, 100)
82+
.LineTo(200, 200)
83+
.LineTo(100, 200);
84+
ctx.NewPath(g => { g.Fill = PredefinedColors.Blue; g.Stroke = null; })
85+
.Move(50, 50)
86+
.LineTo(150, 50)
87+
.LineTo(150, 150)
88+
.LineTo(50, 150)
89+
.Close();
90+
ctx.NewPath(g => { g.Fill = null; g.Stroke = PredefinedColors.Yellow; g.LineWidth = 3; g.Dash = new Dash() { Array = new[] { 5d } }; })
91+
.Move(150, 150)
92+
.LineTo(250, 150)
93+
.LineTo(250, 250)
94+
.LineTo(150, 250)
95+
.Close();
96+
});
97+
})
5698
// Test placement using matrix
5799
.AddPage(page =>
58100
{

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33

44
<PropertyGroup>
5-
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp2.1;netstandard2.1;netstandard2.0;netstandard1.6</TargetFrameworks>
5+
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.1</TargetFrameworks>
66
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.props</MSBuildAllProjects>
77
<SynercodingProjectCategory>src</SynercodingProjectCategory>
88
</PropertyGroup>

src/Synercoding.FileFormats.Pdf/DocumentInformation.cs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
using Synercoding.FileFormats.Pdf.LowLevel;
2+
using Synercoding.FileFormats.Pdf.LowLevel.Extensions;
13
using System;
24

35
namespace Synercoding.FileFormats.Pdf
46
{
57
/// <summary>
68
/// This class contains information about the document
79
/// </summary>
8-
public class DocumentInformation
10+
public class DocumentInformation : IPdfObject
911
{
12+
private bool _isWritten;
13+
14+
internal DocumentInformation(PdfReference id)
15+
{
16+
Reference = id;
17+
}
18+
1019
/// <summary>
1120
/// The document's title
1221
/// </summary>
@@ -46,5 +55,87 @@ public class DocumentInformation
4655
/// The date and time the document was most recently modified, in human-readable form.
4756
/// </summary>
4857
public DateTime? ModDate { get; set; }
58+
59+
/// <inheritdoc />
60+
public PdfReference Reference { get; }
61+
62+
internal uint WriteToStream(PdfStream stream)
63+
{
64+
if (_isWritten)
65+
throw new InvalidOperationException("Object is already written to stream.");
66+
67+
var position = (uint)stream.Position;
68+
69+
stream.IndirectDictionary(this, static (did, dictionary) =>
70+
{
71+
if (!string.IsNullOrWhiteSpace(did.Title))
72+
dictionary.Write(PdfName.Get("Title"), _toPdfHexadecimalString(did.Title!));
73+
if (!string.IsNullOrWhiteSpace(did.Author))
74+
dictionary.Write(PdfName.Get("Author"), _toPdfHexadecimalString(did.Author!));
75+
if (!string.IsNullOrWhiteSpace(did.Subject))
76+
dictionary.Write(PdfName.Get("Subject"), _toPdfHexadecimalString(did.Subject!));
77+
if (!string.IsNullOrWhiteSpace(did.Keywords))
78+
dictionary.Write(PdfName.Get("Keywords"), _toPdfHexadecimalString(did.Keywords!));
79+
if (!string.IsNullOrWhiteSpace(did.Creator))
80+
dictionary.Write(PdfName.Get("Creator"), _toPdfHexadecimalString(did.Creator!));
81+
if (!string.IsNullOrWhiteSpace(did.Producer))
82+
dictionary.Write(PdfName.Get("Producer"), _toPdfHexadecimalString(did.Producer!));
83+
if (did.CreationDate != null)
84+
dictionary.Write(PdfName.Get("CreationDate"), _toPdfDate(did.CreationDate.Value));
85+
if (did.ModDate != null)
86+
dictionary.Write(PdfName.Get("ModDate"), _toPdfDate(did.ModDate.Value));
87+
});
88+
89+
_isWritten = true;
90+
91+
return position;
92+
}
93+
94+
private static string _toPdfHexadecimalString(string input)
95+
{
96+
var bytes = System.Text.Encoding.ASCII.GetBytes(input);
97+
var builder = new System.Text.StringBuilder((bytes.Length * 2) + 2);
98+
builder.Append('<');
99+
foreach (var b in bytes)
100+
{
101+
builder.Append(b.ToString("X2"));
102+
}
103+
builder.Append('>');
104+
return builder.ToString();
105+
}
106+
107+
private static string _toPdfDate(DateTimeOffset input)
108+
{
109+
var datePart = input.ToString("yyyyMMddHHmmss");
110+
111+
var builder = new System.Text.StringBuilder(22);
112+
builder.Append("(D:");
113+
builder.Append(datePart);
114+
115+
var hours = input.Offset.Hours;
116+
var minutes = input.Offset.Minutes;
117+
118+
if (hours == 0 && minutes == 0)
119+
{
120+
builder.Append("Z00'00");
121+
}
122+
else
123+
{
124+
if (hours > 0 || (hours == 0 && minutes > 0))
125+
{
126+
builder.Append('+');
127+
}
128+
else
129+
{
130+
builder.Append('-');
131+
}
132+
builder.Append(Math.Abs(hours).ToString().PadLeft(2, '0'));
133+
builder.Append('\'');
134+
builder.Append(minutes.ToString().PadLeft(2, '0'));
135+
}
136+
builder.Append(')');
137+
138+
return builder.ToString();
139+
}
49140
}
50141
}

src/Synercoding.FileFormats.Pdf/Extensions/MatrixExtensions.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)