using it.stefanochizzolini.clown.documents;
using it.stefanochizzolini.clown.documents.contents;
using it.stefanochizzolini.clown.documents.contents.composition;
using it.stefanochizzolini.clown.documents.contents.objects;
using it.stefanochizzolini.clown.files;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace it.stefanochizzolini.clown.samples{
/**
<summary>This sample demonstrates how to alter the graphic contents (in this case:
the line width) of pages within a PDF document.</summary>
<remarks>This implementation is just a humble exercise: see the API documentation
to perform all the possible access functionalities.</remarks>
*/
public class ContentTweakingSample
: ISample
{
#region dynamic
#region interface
#region public
#region ISample
public void Run(
SampleLoader loader
)
{
// (boilerplate user choice -- ignore it)
string filePath = loader.GetPdfFileChoice("Please select a PDF file");
// 1. Open the PDF file!
File file = new File(filePath);
// 2. Parsing the document...
// Get the PDF document!
Document document = file.Document;
// Interating through the pages...
foreach(Page page in document.Pages)
{
// Get the page contents!
Contents contents = page.Contents;
contents.Insert(0,new SetLineWidth(10)); // Forces the override of line width's initial value (1.0) [PDF:1.6:4.3] setting it at 10 user-space units.
foreach(ContentObject obj in contents)
{NormalizeLineWidth(obj);}
// Update the page contents!
contents.Flush();
}
// (boilerplate metadata insertion -- ignore it)
loader.BuildAccessories(document,this.GetType(),"Content tweaking","content tweaking inside existing pages");
// 3. Serialize the PDF file (again, boilerplate code -- see the SampleLoader class source code)!
loader.Serialize(file,this.GetType().Name);
}
#endregion
#endregion
#region private
private void NormalizeLineWidth(
ContentObject content
)
{
if(content is SetLineWidth)
{
SetLineWidth setLineWidth = (SetLineWidth)content;
// Force lines under 10 user-space units to be set to 10!
if(setLineWidth.Value < 10)
{setLineWidth.Value = 10;}
}
else if(content is CompositeObject<ContentObject>)
{
List<ContentObject> objects = ((CompositeObject<ContentObject>)content).Objects;
foreach(ContentObject obj in objects)
{NormalizeLineWidth(obj);}
}
}
#endregion
#endregion
#endregion
}
}
|