Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@
</PropertyGroup>

<Choose>
<When Condition="'$(TargetFramework)' == 'net472'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
</PropertyGroup>
</When>
<When Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
</PropertyGroup>
</When>
<When Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
Expand All @@ -66,19 +56,6 @@
<DefineConstants>$(DefineConstants);SUPPORTS_NULLABLEREFATTRIBUTES</DefineConstants>
</PropertyGroup>
</When>
<When Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
</PropertyGroup>
</When>
<When Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
<DefineConstants>$(DefineConstants);SUPPORTS_HASHCODE</DefineConstants>
<DefineConstants>$(DefineConstants);SUPPORTS_CODECOVERAGE</DefineConstants>
</PropertyGroup>
</When>
<When Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PropertyGroup>
<DefineConstants>$(DefineConstants);SUPPORTS_MATHF</DefineConstants>
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Gerard Gunnewijk
Copyright (c) 2021 Gerard Gunnewijk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This project is licensed under MIT license.
## Specifications used
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).

The full specifications are not implemented. This library currently only supports placements of images and setting the different boxes.
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.

## Remarks
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.
Expand Down
44 changes: 43 additions & 1 deletion samples/Synercoding.FileFormats.Pdf.ConsoleTester/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Synercoding.FileFormats.Pdf.Primitives;
using Synercoding.FileFormats.Pdf.Extensions;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics;
using Synercoding.FileFormats.Pdf.LowLevel.Graphics.Colors;
using Synercoding.Primitives;
using Synercoding.Primitives.Extensions;
using System;
using System.IO;

namespace Synercoding.FileFormats.Pdf.ConsoleTester
Expand Down Expand Up @@ -53,6 +56,45 @@ public static void Main(string[] args)
page.AddImage(eyeStream, new Rectangle(offSet, offSet, width + offSet, height + offSet, Unit.Millimeters));
}
})
// Test shape graphics
.AddPage(page =>
{
page.AddShapes(ctx =>
{
ctx.DefaultState(g =>
{
g.LineWidth = 1;
g.Fill = null;
g.Stroke = null;
g.Dash = new Dash()
{
Array = Array.Empty<double>(),
Phase = 0
};
g.MiterLimit = 10;
g.LineCap = LineCapStyle.ButtCap;
g.LineJoin = LineJoinStyle.MiterJoin;
});

ctx.NewPath(g => { g.Fill = PredefinedColors.Red; g.Stroke = PredefinedColors.Black; g.LineWidth = 5; })
.Move(100, 100)
.LineTo(200, 100)
.LineTo(200, 200)
.LineTo(100, 200);
ctx.NewPath(g => { g.Fill = PredefinedColors.Blue; g.Stroke = null; })
.Move(50, 50)
.LineTo(150, 50)
.LineTo(150, 150)
.LineTo(50, 150)
.Close();
ctx.NewPath(g => { g.Fill = null; g.Stroke = PredefinedColors.Yellow; g.LineWidth = 3; g.Dash = new Dash() { Array = new[] { 5d } }; })
.Move(150, 150)
.LineTo(250, 150)
.LineTo(250, 250)
.LineTo(150, 250)
.Close();
});
})
// Test placement using matrix
.AddPage(page =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp2.1;netstandard2.1;netstandard2.0;netstandard1.6</TargetFrameworks>
<TargetFrameworks>net5.0;netcoreapp3.1;netstandard2.1</TargetFrameworks>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\Directory.Build.props</MSBuildAllProjects>
<SynercodingProjectCategory>src</SynercodingProjectCategory>
</PropertyGroup>
Expand Down
93 changes: 92 additions & 1 deletion src/Synercoding.FileFormats.Pdf/DocumentInformation.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using Synercoding.FileFormats.Pdf.LowLevel;
using Synercoding.FileFormats.Pdf.LowLevel.Extensions;
using System;

namespace Synercoding.FileFormats.Pdf
{
/// <summary>
/// This class contains information about the document
/// </summary>
public class DocumentInformation
public class DocumentInformation : IPdfObject
{
private bool _isWritten;

internal DocumentInformation(PdfReference id)
{
Reference = id;
}

/// <summary>
/// The document's title
/// </summary>
Expand Down Expand Up @@ -46,5 +55,87 @@ public class DocumentInformation
/// The date and time the document was most recently modified, in human-readable form.
/// </summary>
public DateTime? ModDate { get; set; }

/// <inheritdoc />
public PdfReference Reference { get; }

internal uint WriteToStream(PdfStream stream)
{
if (_isWritten)
throw new InvalidOperationException("Object is already written to stream.");

var position = (uint)stream.Position;

stream.IndirectDictionary(this, static (did, dictionary) =>
{
if (!string.IsNullOrWhiteSpace(did.Title))
dictionary.Write(PdfName.Get("Title"), _toPdfHexadecimalString(did.Title!));
if (!string.IsNullOrWhiteSpace(did.Author))
dictionary.Write(PdfName.Get("Author"), _toPdfHexadecimalString(did.Author!));
if (!string.IsNullOrWhiteSpace(did.Subject))
dictionary.Write(PdfName.Get("Subject"), _toPdfHexadecimalString(did.Subject!));
if (!string.IsNullOrWhiteSpace(did.Keywords))
dictionary.Write(PdfName.Get("Keywords"), _toPdfHexadecimalString(did.Keywords!));
if (!string.IsNullOrWhiteSpace(did.Creator))
dictionary.Write(PdfName.Get("Creator"), _toPdfHexadecimalString(did.Creator!));
if (!string.IsNullOrWhiteSpace(did.Producer))
dictionary.Write(PdfName.Get("Producer"), _toPdfHexadecimalString(did.Producer!));
if (did.CreationDate != null)
dictionary.Write(PdfName.Get("CreationDate"), _toPdfDate(did.CreationDate.Value));
if (did.ModDate != null)
dictionary.Write(PdfName.Get("ModDate"), _toPdfDate(did.ModDate.Value));
});

_isWritten = true;

return position;
}

private static string _toPdfHexadecimalString(string input)
{
var bytes = System.Text.Encoding.ASCII.GetBytes(input);
var builder = new System.Text.StringBuilder((bytes.Length * 2) + 2);
builder.Append('<');
foreach (var b in bytes)
{
builder.Append(b.ToString("X2"));
}
builder.Append('>');
return builder.ToString();
}

private static string _toPdfDate(DateTimeOffset input)
{
var datePart = input.ToString("yyyyMMddHHmmss");

var builder = new System.Text.StringBuilder(22);
builder.Append("(D:");
builder.Append(datePart);

var hours = input.Offset.Hours;
var minutes = input.Offset.Minutes;

if (hours == 0 && minutes == 0)
{
builder.Append("Z00'00");
}
else
{
if (hours > 0 || (hours == 0 && minutes > 0))
{
builder.Append('+');
}
else
{
builder.Append('-');
}
builder.Append(Math.Abs(hours).ToString().PadLeft(2, '0'));
builder.Append('\'');
builder.Append(minutes.ToString().PadLeft(2, '0'));
}
builder.Append(')');

return builder.ToString();
}
}
}
35 changes: 0 additions & 35 deletions src/Synercoding.FileFormats.Pdf/Extensions/MatrixExtensions.cs

This file was deleted.

Loading