Customize document output
Preview released
This function and the API used in this function have been released in advance. We do not guarantee the quality at this time, so please use it at your own risk. Also, please note that these specifications are subject to change without notice.
Overview
You can customize the document output function that comes standard with Next Design as follows.
- You can specify the target that does not output the document.
- You can add any heading, caption, etc. in the document.
- You can change the output order of the view as you like.
- You can output in a different order from the order of the models in the model navigator.
How to customize document output
You can customize the behavior by registering the setting object DocumentGenerationConfigs
withIApplication.DocumentGeneratorCustomization.RegisterConfig ()
.
How to customize
There are two ways to customize the document output:
- Options
- You can customize the built-in document generation behavior by changing the option settings in
DocumentGenerationConfigs
.
- You can customize the built-in document generation behavior by changing the option settings in
- Callback processing
- Register the callback process in
DocumentGenerationConfigs
. Since callback processing can be registered at various timings in document generation, the output result can be customized arbitrarily.- You can add, delete, or change the output content at the timing after the content is generated.
- You can control the output contents of Word and Html before and after writing the contents.
- Register the callback process in
Option settings
Change target in option settings
You can customize the behavior of document generation by changing the following option settings of DocumentGenerationConfigs
.
API name | Description | Possible values | Correspondence target |
---|---|---|---|
TemplateFilePath | You can specify the file path of the template to be used for output. | Arbitrary file path | Word only |
OrderByViewDefinition | Gets or sets whether the output order of the view is in the view definition order. | true : Outputs in the order of view definition. false : Outputs the diagram sequence diagram first, not in the order of the view definition. | Word only |
AutoFitTables | Gets or sets how to automatically adjust the column widths of a table. If null, it will not be adjusted automatically. | AutoFitBehavior.AutoFitToContents : Match the column width to the width of the string. AutoFitBehavior.AutoFitToWindow : Fits the column width to the window size. | Word only |
Example of customization by setting options
//Example: Customize the output behavior with option settings
public class DocumentExprotSample: IExtension
{
//This is the process when the extension is activated.
public void Activate (IContext context)
{
//Customize the document output
var app = context.App;
app.DocumentGeneratorCustomization.RegisterConfig(this, context =>
{
//Create a configuration object
var config = new DocumentGenerationConfigs ();
if (context.DocumentFormat == "word")
{
//For Word output, get the options with `GetWordOptions ()`
var option = config.GetWordOptions ();
//Example: Specify the Word template file to output
var templateFilePath = Path.Combine ("Any Path", "CustomizeTemplate.dotx");
option.TemplateFilePath = templateFilePath;
//Example: Change the order of the output views
option.OrderByViewDefinition = false;
//Example: Change the automatic adjustment of table column width
option.AutoFitTables = AutoFitBehavior.AutoFitToWindow;
}
return config;
});
}
}
Callback processing
Callback processing registration target
You can register the callback process to be executed at the timing shown below in DocumentGenerationConfigs
.
API name | Description |
---|---|
RegisterGenerationStart | Called when document generation starts. |
RegisterGenerationFinish | Called after document generation is complete. |
RegisterDocumentWriteStart | Called at the start of writing a document. |
RegisterDocumentWriteFinish | Called at the end of writing a document. |
RegisterBeforeContentWrite | Called at the beginning of writing document content. |
RegisterAfterContentWrite | Called at the end of writing document content. |
RegisterAfterContentsGeneration | Called after generating all the content in the document. |
Example of customization by callback processing
You can customize by receiving IDocumentContent
and IPageContent
as callback arguments.
Specify the target not to output the document
If you have a view, model, or field that you don't want to output, you can remove it from the output.
//Example: Delete the table itself that displays arbitrary data
private void RemoveTableContent (IPageContent content)
{
var targetPageContent = content.Pages.FirstOrDefault (_ => _. Title == "Page with grid");
var targetViewContent = targetPageContent.Views.FirstOrDefault (_ => _.Title == "Detail");
var targetTableContent = targetViewContent.Items.FirstOrDefault (_ => _.Control.Title == "Use Case List");
//Example: Delete the "Use Case List" table
targetViewContent.Items.Remove (targetTableContent);
}
//Example: Delete a table column that displays any field value
private void RemoveTableColumnContent (IPageContent content)
{
var targetPageContent = content.Pages.FirstOrDefault (_ => _. Title == "Page with grid");
var targetViewContent = targetPageContent.Views.FirstOrDefault (_ => _.Title == "Detail");
var targetTableViewContent = targetViewContent.Items.FirstOrDefault (_ => _.Control.Title == "Use Case List");
var targetTableContent = targetTableViewContent? .GetControl <ITableContent> ();
//Delete the column header of any table (in the example, delete the first column)
targetTableContent.Headers.RemoveAt (0);
//Delete the cell of any table (in the example, delete the cell of the table corresponding to the first column)
foreach (var tableRow in targetTableViewContent.GetAllContents (). OfType <ITableRowContent> ())
{
tableRow.Cells.RemoveAt (0);
}
}
//Example: Delete the table row corresponding to any element
private void RemoveTableRowContent (IPageContent content)
{
var targetPageContent = content.Pages.FirstOrDefault (_ => _. Title == "Page with grid");
var targetViewContent = targetPageContent.Views.FirstOrDefault (_ => _.Title == "Detail");
var targetTableContent = targetViewContent.Items.FirstOrDefault (_ => _.Control.Title == "Use Case List");
//Delete the table row (in the example, delete the first row)
targetTableContent.Items.RemoveAt (0);
}
Add arbitrary headings, captions, etc. in the document
You can insert text anywhere on the page, so you can add headings, picture numbers, and captions.
//Example: If the output target is a figure, enter the figure number and caption.
config.RegisterAfterContentWrite (p =>
{
if (p.DocumentContent.ContentType == DocumentContentTypes.Image)
{
var imageContent = (IImageContent) p.DocumentContent;
//Processing for Word output
//Get a Word-only write object
var imageNumber = 1;
var wordWriter = p.GetWriter <IWordWriter> ();
if (wordWriter! = null)
{
//Center the Word text and add a picture caption
wordWriter.SetAlignment (ParagraphAlignment.Center);
wordWriter.WriteLine ($ "Figure {imageNumber}: {imageContent.Title}");
wordWriter.RestoreAlignment ();
}
//Processing for HTML output
//Get the HTML-only write object
var htmlWriter = p.GetWriter <IHtmlWriter> ();
if (htmlWriter! = null)
{
//Add a centered diagram caption in HTML
htmlWriter.WriteLine ($ "<p style =\" text-align: center;\"> Figure {imageNumber}: {imageContent.Title} </p>");
htmlWriter.WriteLine ("</div>");
}
imageNumber ++;
}
});
//Example: Add a string to the header/footer of the page, anywhere
config.RegisterBeforeContentWrite (p =>
{
//Processing for Word output
//Get a Word-only write object
var writer = p.GetWriter <IWordWriter> ();
if (writer! = null && p.DocumentContent.ContentType == DocumentContentTypes.Document)
{
//Add any string
writer.WriteText ("Page description");
//Add a string to the header
writer.MoveToHeader ();
writer.WriteText ("Add to header");
//Add the number of pages to the header
writer.InsertField ("PAGE");
writer.WriteText ("/");
writer.InsertField ("SECTIONPAGES");
//Add a string to the footer
writer.MoveToFooter ();
writer.WriteText ("Add to footer");
writer.MoveToDocumentEnd ();
}
//Processing for HTML output
//Get the HTML-only write object
var htmlWriter = p.GetWriter <IHtmlWriter> ();
if (htmlWriter! = null && p.DocumentContent.ContentType == DocumentContentTypes.Image)
{
htmlWriter.WriteLine ("<div style =\" display: inline-block;\">");
}
});
Arbitrarily change the output order of the view
You can change the view to any position and output it.
//Example: Move the view to any position
private void MoveView (IPageContent content)
{
//Get the view to move in order
var targetContent = content.Pages.FirstOrDefault ();
var moveContent = targetContent.Views.First ();
//Move the view to any position (move to the beginning in the example)
targetContent.Views.Remove (moveContent);
targetContent.Views.Insert (0, moveContent);
}
Output in a different order from the model order in the model navigator
You can change the output order by navigating the page corresponding to the model.
//Example: Move the page to any position
private void MovePage (IPageContent content)
{
//Get the target to move
var targetContent = content.Pages.FirstOrDefault ();
var moveContent = targetContent.Pages.First ();
//Move the page to any position
targetContent.Pages.Remove (moveContent);
targetContent.Pages.Insert (4, moveContent);
}
Usage example
public class DocumentExprotSample: IExtension
{
//This is the process when the extension is activated.
public void Activate (IContext context)
{
var app = context.App;
app.DocumentGeneratorCustomization.RegisterConfig(this, context =>
{
var config = new DocumentGenerationConfigs ();
if (context.DocumentFormat == "word")
{
var option = config.GetWordOptions ();
//Change the order of the output views
option.OrderByViewDefinition = false;
//Change the automatic adjustment of table column width
option.AutoFitTables = AutoFitBehavior.AutoFitToWindow;
}
config.RegisterDocumentWriteFinish (p =>
{
//Get the write object for Word
var writer = p.GetWriter <IWordWriter> ();
if (writer! = null)
{
//Page break
writer.InsertSectionBreakNewPage ();
//Specify the page orientation
writer.SetPageDirection (DocumentPageOrientation.Landscape);
//Add bullets
writer.ListFormat.ApplyBulletDefault ();
writer.WriteLine ("Bullets 1");
writer.WriteLine ("Bullets 2");
writer.ListFormat.RemoveNumbers ();
//Line break paragraph
writer.InsertParagraphBreak ();
//Generate tables and rows and columns
writer.CreateTable ();
writer.WriteCell ("Column 1");
writer.WriteCell ("Column 2", Color.AliceBlue, Color.Black, cellWidth: 50d);
writer.WriteCell ("Column 3", Color.LightGreen, Color.Black, cellWidth: 100d);
writer.EndRow ();
writer.RowFormat.Height = 50;
writer.WriteCell ("<b> Cell merge height 50 </b>", Color.Transparent, Color.Red, 20d, true);
writer.Paragraph.LineSpacing = 20d;
writer.CellFormat.HorizontalMerge = CellMerge.First;
writer.WriteCell (string.Empty);
writer.CellFormat.HorizontalMerge = CellMerge.Previous;
writer.WriteCell (string.Empty);
writer.CellFormat.HorizontalMerge = CellMerge.Previous;
writer.EndRow ();
writer.EndTable ();
//Insert a centered bold string
writer.Paragraph.Alignment = ParagraphAlignment.Center;
writer.Font.Bold = true;
writer.WriteLine ("Table");
writer.Paragraph.RestoreAlignment ();
}
});
config.RegisterAfterContentWrite (p =>
{
//If the content is an image, add a caption
if (p.DocumentContent.ContentType == DocumentContentTypes.Image)
{
var imageContent = (IImageContent) p.DocumentContent;
var imageNo = 1;
var wordWriter = p.GetWriter <IWordWriter> ();
if (wordWriter! = null)
{
//Insert the caption of the figure in black text
wordWriter.SetColor (Color.Black);
wordWriter.SetAlignment (ParagraphAlignment.Center);
wordWriter.WriteLine ($ "Figure {imageNo}: {imageContent.Title}");
wordWriter.RestoreAlignment ();
wordWriter.RestoreColor ();
}
imageNo ++;
}
});
return config;
});
}
//Processing when extension is deactivated
public void Deactivate (IContext context)
{
//Unregister the document output customization settings
var app = context.App;
app.DocumentGeneratorCustomization.UnregisterConfig(this);
}
}