Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments in XPS document

Tags:

wpf

xps

I am trying to add comments in XPS document. Is it possible with .Net API and read them back? I need to add hidden text embedded in XPS file.

like image 392
i486 Avatar asked Jul 03 '18 13:07

i486


Video Answer


1 Answers

You can add metadata to an XPS document file by using the XpsDocument class and its CoreDocumentProperties property.

I'm not sure if this would satisfy your requirements, but here's how to do it.

using System.IO;
using System.IO.Packaging;
using System.Windows.Xps.Packaging;

namespace StackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            const string XpsFilePath = @" ... path to XPS file ... ";

            using (var document = new XpsDocument(XpsFilePath, FileAccess.ReadWrite))
            {
                PackageProperties properties = document.CoreDocumentProperties;
                properties.Title = "Some title";
                properties.Subject = "Some subject line";
                properties.Keywords = "stackoverflow xps";
                properties.Category = "Some category";
                properties.Description = "Some kind of document";
                properties.Creator = "me";
                properties.LastModifiedBy = "me again";
                properties.Revision = "Rev A";
                properties.Version = "v1";
            }

            // XpsDocument is from System.Windows.Xps.Packaging, in ReachFramework assembly
            // PackageProperties is from System.IO.Packaging, in WindowsBase assembly
        }
    }
}

You can access this information via code by creating a new XpsDocument with read access, ie.

using (var document = new XpsDocument(XpsFilePath, FileAccess.Read))
{
    PackageProperties properties = document.CoreDocumentProperties;
    System.Console.WriteLine(properties.Title);
    // etc...
}

You can view the metadata for the XPS document in Windows by right-clicking on the file, choosing Properties, then showing the Details tab:

Properties for XPS document

The metadata isn't strictly hidden, as you can see it in Windows using the above technique or access it via code. However, it isn't displayed on the actual pages of the XPS document, so is not normally visible when viewing or printing.

like image 189
Steven Rands Avatar answered Oct 17 '22 07:10

Steven Rands