Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to provide custom XMP metadata #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 11 additions & 1 deletion src/PdfSharp/Pdf/PdfDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,10 @@ internal override void PrepareForSave()

// @PDF/UA
// Create PdfMetadata now to include the final document information in XMP generation.
Catalog.Elements.SetReference(PdfCatalog.Keys.Metadata, new PdfMetadata(this));
if( MetadataProvider != null )
Catalog.Elements.SetReference(PdfCatalog.Keys.Metadata, MetadataProvider(this));
else
Catalog.Elements.SetReference(PdfCatalog.Keys.Metadata, new PdfMetadata(this));
}

/// <summary>
Expand Down Expand Up @@ -808,6 +811,13 @@ internal PdfCatalog Catalog
}
PdfCatalog _catalog; // never changes if once created

public delegate PdfMetadata PdfMetadataProvider(PdfDocument document);

/// <summary>
/// Gets or sets the delegate that will return the PdfMetadata object to use when saving the document
/// </summary>
public PdfMetadataProvider MetadataProvider { get; set; }

/// <summary>
/// Gets the PdfInternals object of this document, that grants access to some internal structures
/// which are not part of the public interface of PdfDocument.
Expand Down
12 changes: 6 additions & 6 deletions src/PdfSharp/Pdf/PdfMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace PdfSharp.Pdf
/// <summary>
/// Represents an XML Metadata stream.
/// </summary>
public sealed class PdfMetadata : PdfDictionary
public class PdfMetadata : PdfDictionary
{
/// <summary>
/// Initializes a new instance of the <see cref="PdfMetadata"/> class.
Expand All @@ -60,15 +60,15 @@ public PdfMetadata(PdfDocument document)
SetupStream();
}

void SetupStream()
protected void SetupStream()
{
var stream = GenerateXmp();

byte[] bytes = PdfEncoders.RawEncoding.GetBytes(stream);
CreateStream(bytes);
if(stream != null)
CreateStream(stream);
}

string GenerateXmp()
protected virtual byte[] GenerateXmp()
{
var instanceId = Guid.NewGuid().ToString();
var documentId = Guid.NewGuid().ToString();
Expand Down Expand Up @@ -123,7 +123,7 @@ string GenerateXmp()
" </x:xmpmeta>\n" +
"<?xpacket end=\"r\"?>\n";

return str;
return PdfEncoders.RawEncoding.GetBytes(str);
}

/// <summary>
Expand Down