Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>library</OutputType>
Expand Down Expand Up @@ -256,6 +256,8 @@
<Compile Include="..\PdfSharp\Pdf.Annotations\PdfRubberStampAnnotation.cs" Link="Pdf.Annotations\PdfRubberStampAnnotation.cs" />
<Compile Include="..\PdfSharp\Pdf.Annotations\PdfTextAnnotation.cs" Link="Pdf.Annotations\PdfTextAnnotation.cs" />
<Compile Include="..\PdfSharp\Pdf.Annotations\PdfWidgetAnnotation.cs" Link="Pdf.Annotations\PdfWidgetAnnotation.cs" />
<Compile Include="..\PdfSharp\Pdf.Annotations\PdfFileAttachmentAnnotation.cs" Link="Pdf.Annotations\PdfFileAttachmentAnnotation.cs" />
<Compile Include="..\PdfSharp\Pdf.Annotations\enums\PdfFileAttachmentAnnotationIcon.cs" Link="Pdf.Annotations\enums\PdfFileAttachmentAnnotationIcon.cs" />
<Compile Include="..\PdfSharp\Pdf.Content.Objects\CObjects.cs" Link="Pdf.Content.Objects\CObjects.cs" />
<Compile Include="..\PdfSharp\Pdf.Content.Objects\enum\OpCodeFlags.cs" Link="Pdf.Content.Objects\enum\OpCodeFlags.cs" />
<Compile Include="..\PdfSharp\Pdf.Content.Objects\enum\OpCodeName.cs" Link="Pdf.Content.Objects\enum\OpCodeName.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>library</OutputType>
Expand Down Expand Up @@ -253,6 +253,8 @@
<Compile Include="..\PdfSharp\Pdf.Annotations\PdfRubberStampAnnotation.cs" Link="Pdf.Annotations\PdfRubberStampAnnotation.cs" />
<Compile Include="..\PdfSharp\Pdf.Annotations\PdfTextAnnotation.cs" Link="Pdf.Annotations\PdfTextAnnotation.cs" />
<Compile Include="..\PdfSharp\Pdf.Annotations\PdfWidgetAnnotation.cs" Link="Pdf.Annotations\PdfWidgetAnnotation.cs" />
<Compile Include="..\PdfSharp\Pdf.Annotations\PdfFileAttachmentAnnotation.cs" Link="Pdf.Annotations\PdfFileAttachmentAnnotation.cs" />
<Compile Include="..\PdfSharp\Pdf.Annotations\enums\PdfFileAttachmentAnnotationIcon.cs" Link="Pdf.Annotations\enums\PdfFileAttachmentAnnotationIcon.cs" />
<Compile Include="..\PdfSharp\Pdf.Content.Objects\CObjects.cs" Link="Pdf.Content.Objects\CObjects.cs" />
<Compile Include="..\PdfSharp\Pdf.Content.Objects\enum\OpCodeFlags.cs" Link="Pdf.Content.Objects\enum\OpCodeFlags.cs" />
<Compile Include="..\PdfSharp\Pdf.Content.Objects\enum\OpCodeName.cs" Link="Pdf.Content.Objects\enum\OpCodeName.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// PDFsharp - A .NET library for processing PDF
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.

using PdfSharp.Drawing;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.

using PdfSharp.Drawing;
using PdfSharp.Pdf.Advanced;

namespace PdfSharp.Pdf.Annotations
{
/// <summary>
/// Represents a file attachment annotation.
/// </summary>
public sealed class PdfFileAttachmentAnnotation : PdfAnnotation
{
/// <summary>
/// Initializes a new instance of the <see cref="PdfFileAttachmentAnnotation"/> class.
/// </summary>
public PdfFileAttachmentAnnotation()
{
Initialize();
}

/// <summary>
/// Initializes a new instance of the <see cref="PdfFileAttachmentAnnotation"/> class.
/// </summary>
public PdfFileAttachmentAnnotation(PdfDocument document)
: base(document)
{
Initialize();
}

void Initialize()
{
Elements.SetName(Keys.Subtype, "/FileAttachment");
Icon = PdfFileAttachmentAnnotationIcon.PushPin;
Flags = PdfAnnotationFlags.Locked;
}

/// <summary>
/// Creates a file attachment annotation.
/// </summary>
public static PdfFileAttachmentAnnotation CreateFileAttachmentAnnotation(PdfRectangle rectangle)
{
var annot = new PdfFileAttachmentAnnotation
{
Rectangle = rectangle,
};
return annot;
}

/// <summary>
/// Gets or sets an icon to be used in displaying the annotation.
/// </summary>
public PdfFileAttachmentAnnotationIcon Icon
{
get
{
string value = Elements.GetName(Keys.Name);
if (value == "")
return PdfFileAttachmentAnnotationIcon.NoIcon;

value = value.Substring(1);
if (!Enum.IsDefined(typeof(PdfFileAttachmentAnnotationIcon), value))
return PdfFileAttachmentAnnotationIcon.NoIcon;

return (PdfFileAttachmentAnnotationIcon)Enum.Parse(typeof(PdfFileAttachmentAnnotationIcon), value, false);
}
set
{
if (value != PdfFileAttachmentAnnotationIcon.NoIcon &&
Enum.IsDefined(typeof(PdfFileAttachmentAnnotationIcon), value))
{
Elements.SetName(Keys.Name, "/" + value.ToString());
}
else
Elements.Remove(Keys.Name);
}
}

public IAnnotationAppearanceHandler? CustomAppearanceHandler { get; set; }

/// <summary>
/// Creates the custom appearance form X object for this annotation
/// </summary>
public void RenderCustomAppearance()
{
var visible = (Rectangle.X1 + Rectangle.X2 + Rectangle.Y1 + Rectangle.Y2) != 0;

if (!visible)
return;

if (CustomAppearanceHandler == null)
throw new Exception("AppearanceHandler is null");

XForm form = new(_document, Rectangle.Size);
XGraphics gfx = XGraphics.FromForm(form);

CustomAppearanceHandler.DrawAppearance(gfx, Rectangle.ToXRect());

form.DrawingFinished();

// Get existing or create new appearance dictionary
if (Elements[PdfAnnotation.Keys.AP] is not PdfDictionary ap)
{
ap = new PdfDictionary(_document);
Elements[PdfAnnotation.Keys.AP] = ap;
}

// Set XRef to normal state
ap.Elements["/N"] = form.PdfForm.Reference;

form.PdfRenderer.Close();

Icon = PdfFileAttachmentAnnotationIcon.NoIcon;
}

/// <summary>
/// Predefined keys of this dictionary.
/// </summary>
internal new class Keys : PdfAnnotation.Keys
{
/// <summary>
/// (Required) The file associated with this annotation.
/// </summary>
[KeyInfo(KeyType.FileSpecification | KeyType.Required)]
public const string FS = "/FS";

/// <summary>
/// (Optional) The name of an icon that shall be used in displaying the annotation.
/// Conforming readers shall provide predefined icon appearances for at least the following names:
/// Graph
/// PushPin
/// Paperclip
/// Tag
/// </summary>
[KeyInfo(KeyType.Name | KeyType.Optional)]
public const string Name = "/Name";

public static DictionaryMeta Meta => _meta ??= CreateMeta(typeof(Keys));

static DictionaryMeta? _meta;
}

/// <summary>
/// Gets the KeysMeta of this dictionary type.
/// </summary>
internal override DictionaryMeta Meta => Keys.Meta;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.

namespace PdfSharp.Pdf.Annotations
{
/// <summary>
/// Specifies the pre-defined icon names of file attachment annotations.
/// </summary>
public enum PdfFileAttachmentAnnotationIcon
{
/// <summary>
/// A pre-defined annotation icon.
/// </summary>
NoIcon,

/// <summary>
/// A pre-defined annotation icon.
/// </summary>
Graph,

/// <summary>
/// A pre-defined annotation icon.
/// </summary>
PushPin,

/// <summary>
/// A pre-defined annotation icon.
/// </summary>
Paperclip,

/// <summary>
/// A pre-defined annotation icon.
/// </summary>
Tag,
}
}
36 changes: 35 additions & 1 deletion src/foundation/src/PDFsharp/src/PdfSharp/Pdf/PdfPage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// PDFsharp - A .NET library for processing PDF
// PDFsharp - A .NET library for processing PDF
// See the LICENSE file in the solution root for more information.

using System.ComponentModel;
Expand All @@ -9,6 +9,7 @@
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.Annotations;
using InvalidOperationException = System.InvalidOperationException;
using System.IO;

namespace PdfSharp.Pdf
{
Expand Down Expand Up @@ -683,6 +684,39 @@ public PdfLinkAnnotation AddFileLink(PdfRectangle rect, string fileName)
return annotation;
}

/// <summary>
/// Adds a file attachment annotation.
/// </summary>
/// <param name="rect">The rect.</param>
/// <param name="fileSpecification">The file specification</param>
public PdfFileAttachmentAnnotation AddFileAttachmentAnnotation(PdfRectangle rect, PdfFileSpecification fileSpecification)
{
if (fileSpecification.Reference == null)
Owner.Internals.AddObject(fileSpecification);

var annotation = PdfFileAttachmentAnnotation.CreateFileAttachmentAnnotation(rect);
Annotations.Add(annotation);
annotation.Elements.SetReference(PdfFileAttachmentAnnotation.Keys.FS, fileSpecification.ReferenceNotNull);
return annotation;
}

/// <summary>
/// Adds a file attachment annotation.
/// </summary>
/// <param name="rect">The rect.</param>
/// <param name="fileStream">The file stream</param>
/// <param name="fileName">The file name</param>
public PdfFileAttachmentAnnotation AddFileAttachmentAnnotation(PdfRectangle rect, PdfEmbeddedFileStream fileStream, string fileName)
{
var fileSpecification = new PdfFileSpecification(Owner, fileStream, fileName);
Owner.Internals.AddObject(fileSpecification);

var annotation = PdfFileAttachmentAnnotation.CreateFileAttachmentAnnotation(rect);
Annotations.Add(annotation);
annotation.Elements.SetReference(PdfFileAttachmentAnnotation.Keys.FS, fileSpecification.ReferenceNotNull);
return annotation;
}

#endregion

// TODO_OLD: PdfActions
Expand Down