This example demonstrates how to set document infomation.
You can get information about pdf document, such as the title, the fonts used, and security settings. Some of this information is set by the person who created the document, and some is generated automatically
advisory
identifier
Title
Author
Keywords
Subject
Producer
CreationDate
Custom metadata
Clicking on the generate button will launch a pdf with preset document properties
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using OfficeComponent.Pdf;
using OfficeComponent.Pdf.Graphics;
using OfficeComponent.Pdf.Xmp;
namespace OfficeComponent.Samples
{
class DocumentSettingsExample : ExampleBase
{
public DocumentSettingsExample(string commonDataPath, string outputDir)
: base(commonDataPath, outputDir)
{
}
public DocumentSettingsExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
{
}
public override string Execute()
{
// Create a new instance of PdfDocument class.
PdfDocument doc = new PdfDocument();
PdfPage page = doc.Pages.Add();
// Get xmp object.
XmpMetadata xmp = doc.DocumentInformation.XmpMetadata;
// XMP Basic Schema.
XmpBasicSchema basic = xmp.BasicSchema;
basic.Advisory.Add("advisory");
basic.BaseURL = new Uri("http://google.com");
basic.CreateDate = DateTime.Now;
basic.CreatorTool = "creator tool";
basic.Identifier.Add("identifier");
basic.Label = "label";
basic.MetadataDate = DateTime.Now;
basic.ModifyDate = DateTime.Now;
basic.Nickname = "nickname";
basic.Rating.Add(-25);
//Setting various Document properties.
doc.DocumentInformation.Title = "Document Properties Information";
doc.DocumentInformation.Author = "OfficeComponent";
doc.DocumentInformation.Keywords = "PDF";
doc.DocumentInformation.Subject = "PDF demo";
doc.DocumentInformation.Producer = "OfficeComponent Software";
doc.DocumentInformation.CreationDate = DateTime.Now;
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f);
PdfFont boldFont = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
PdfBrush brush = PdfBrushes.Black;
PdfGraphics g = page.Graphics;
PdfStringFormat format = new PdfStringFormat();
format.LineSpacing = 20f;
g.DrawString("Press Ctrl+D to see Document Properties", boldFont, brush, 10, 10);
g.DrawString("Basic Schema Xml:", boldFont, brush, 10, 50);
g.DrawString(basic.XmlData.OuterXml, font, brush, new RectangleF(10, 70, 500, 500), format);
//Defines and set values for Custom metadata and add them to the Pdf document
OfficeComponent.Pdf.Xmp.XmpCustomSchema custom = new XmpCustomSchema(xmp, "custom", "http://www.officecomponent.com/");
custom["Company"] = "OfficeComponent";
custom["Website"] = "http://www.officecomponent.com/";
custom["Product"] = "OfficeComponent PDF";
// Save and close the document.
var outputPath = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + ".pdf");
doc.Save(outputPath);
doc.Close(true);
return outputPath;
}
}
}
Imports System.IO
Imports System.Text
Imports OfficeComponent.Pdf
Imports OfficeComponent.Pdf.Graphics
Imports OfficeComponent.Pdf.Xmp
Namespace OfficeComponent.Samples
Friend Class DocumentSettingsExample
Inherits ExampleBase
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
MyBase.New(commonDataPath, outputDir)
End Sub
Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
MyBase.New(commonDataPath, outputDir, xmlFile)
End Sub
Public Overrides Function Execute() As String
' Create a new instance of PdfDocument class.
Dim doc As New PdfDocument()
Dim page As PdfPage = doc.Pages.Add()
' Get xmp object.
Dim xmp As XmpMetadata = doc.DocumentInformation.XmpMetadata
' XMP Basic Schema.
Dim basic As XmpBasicSchema = xmp.BasicSchema
basic.Advisory.Add("advisory")
basic.BaseURL = New Uri("http://google.com")
basic.CreateDate = Date.Now
basic.CreatorTool = "creator tool"
basic.Identifier.Add("identifier")
basic.Label = "label"
basic.MetadataDate = Date.Now
basic.ModifyDate = Date.Now
basic.Nickname = "nickname"
basic.Rating.Add(-25)
'Setting various Document properties.
doc.DocumentInformation.Title = "Document Properties Information"
doc.DocumentInformation.Author = "OfficeComponent"
doc.DocumentInformation.Keywords = "PDF"
doc.DocumentInformation.Subject = "PDF demo"
doc.DocumentInformation.Producer = "OfficeComponent Software"
doc.DocumentInformation.CreationDate = Date.Now
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 10F)
Dim boldFont As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12F, PdfFontStyle.Bold)
Dim brush As PdfBrush = PdfBrushes.Black
Dim g As PdfGraphics = page.Graphics
Dim format As New PdfStringFormat()
format.LineSpacing = 20F
g.DrawString("Press Ctrl+D to see Document Properties", boldFont, brush, 10, 10)
g.DrawString("Basic Schema Xml:", boldFont, brush, 10, 50)
g.DrawString(basic.XmlData.OuterXml, font, brush, New RectangleF(10, 70, 500, 500), format)
'Defines and set values for Custom metadata and add them to the Pdf document
Dim custom As OfficeComponent.Pdf.Xmp.XmpCustomSchema = New XmpCustomSchema(xmp, "custom", "http://www.officecomponent.com/")
custom("Company") = "OfficeComponent"
custom("Website") = "http://www.officecomponent.com/"
custom("Product") = "OfficeComponent PDF"
' Save and close the document.
Dim outputPath = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & ".pdf")
doc.Save(outputPath)
doc.Close(True)
Return outputPath
End Function
End Class
End Namespace