private void WriteRun(WriterContext context)
        {
            Run run = new Run();

            // Format run
            RunProperties runProperties = run.AppendChild(new RunProperties());
            Color         color         = new Color {
                Val = "CC0000"
            };

            runProperties.AppendChild(color);

            // Add text
            Text text = new Text(Message + " ");

            run.AppendChild(text);

            // Output run
            context.Writer.Current.AppendChild(run);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Generates the content represented by this write instruction.
        /// </summary>
        /// <param name="context">The context.</param>
        protected override void OnGenerate(WriterContext context)
        {
            string value = GetText(context);

            // Find a run to clone
            Run sourceRun;

            if (FieldData.FieldType == FieldType.Simple)
            {
                // Get from fldSimple to rPr
                // i.e. <fldSimple> <r> <rPr>
                sourceRun = FieldData.SourceStart.SourceNode.FirstChild as Run;
            }
            else
            {
                // Get from the first run in the complex field to its properties
                // i.e. <r><rPr>..</rPr><w:fldChar w:fldCharType="begin" /></r>
                sourceRun = FieldData.SourceStart.SourceNode as Run;
            }
            if (sourceRun == null)
            {
                return; // assert false
            }

            // Clone the run
            Run run = (Run)sourceRun.CloneNode(false);

            if (sourceRun.RunProperties != null)
            {
                run.RunProperties = sourceRun.RunProperties.CloneNode(true) as RunProperties;
            }

            // Add text
            Text text = new Text(value);

            run.AppendChild(text);

            // Output the run
            context.Writer.Current.AppendChild(run);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Generates the content for this instruction.
        /// </summary>
        /// <param name="context">The context.</param>
        protected override void OnGenerate(WriterContext context)
        {
            // Get entities
            IEnumerable <DataElement> data = DataSource.GetData(context);

            // Render for each entity
            foreach (var dataElement in data)
            {
                context.DataElementStack.Push(dataElement);
                try
                {
                    WriteChildren(context);
                }
                finally
                {
                    context.DataElementStack.Pop();
                }

                // Fix any inconsistencies in XML stack level resulting from mismatched children
                context.Writer.RealignOnNextWrite();
            }
        }
 /// <summary>
 /// Render content for this iteration of this instruction.
 /// </summary>
 /// <param name="context">The writing context.</param>
 protected override void OnGenerate(WriterContext context)
 {
     WriteRun(context);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Render content for this iteration of this instruction.
 /// </summary>
 /// <param name="context">The writing context.</param>
 protected virtual void OnGenerate(WriterContext context)
 {
     WriteChildren(context);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Return the entities for this data source.
 /// </summary>
 /// <param name="context">The context, such as the parent entity, from which these entities are being loaded.</param>
 /// <returns>List of entities.</returns>
 public abstract IEnumerable <DataElement> GetData(WriterContext context);
Ejemplo n.º 7
0
 /// <summary>
 /// Determine the text to be shown.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <returns>The text</returns>
 protected abstract string GetText(WriterContext context);
Ejemplo n.º 8
0
        /// <summary>
        /// Generate a document from a template.
        /// </summary>
        /// <param name="templateStream">File stream of the template Word document.</param>
        /// <param name="outputStream">Output stream to receive the generated document.</param>
        /// <param name="settings">Various settings to pass into generation.</param>
        public void CreateDocument(Stream templateStream, Stream outputStream, GeneratorSettings settings = null)
        {
            if (templateStream == null)
            {
                throw new ArgumentNullException(nameof(templateStream));
            }
            if (outputStream == null)
            {
                throw new ArgumentNullException(nameof(outputStream));
            }

            using (Profiler.Measure("Generator.CreateDocument"))
            {
                if (settings == null)
                {
                    settings = new GeneratorSettings();
                }

                // First copy the template to the output, and then modify the output
                settings.UpdateProgress("Copying template");
                templateStream.CopyTo(outputStream);

                // Open both streams for Open XML processing
                settings.UpdateProgress("Reading template");
                using (WordprocessingDocument sourceDoc = WordprocessingDocument.Open(templateStream, false))
                    using (WordprocessingDocument targetDoc = WordprocessingDocument.Open(outputStream, true))
                    {
                        var sourceBody     = GetBody(sourceDoc);
                        var sourceBodyHost = sourceBody?.Parent;
                        if (sourceBodyHost == null)
                        {
                            throw new Exception("Source document body host not found.");
                        }

                        var targetBodyHost = GetBody(targetDoc)?.Parent;
                        if (targetBodyHost == null)
                        {
                            throw new Exception("Source document body not found.");
                        }

                        if (settings.WriteDebugFiles)
                        {
                            DebugXml(sourceBody.OuterXml, @"template.xml");
                        }

                        // Purge the document body of the target document
                        targetBodyHost.RemoveAllChildren();

                        // Build instruction tree from the source stream
                        settings.UpdateProgress("Parsing template");
                        OpenXmlReader reader = new OpenXmlReader(ExternalServices)
                        {
                            ReaderContext     = new ReaderContext( ),
                            GeneratorSettings = settings
                        };

                        TemplateData template;

                        using (new SecurityBypassContext())
                        {
                            template = reader.BuildInstructionTree(sourceBody);
                        }

                        Instruction rootInstruction = template.RootInstruction;
                        if (rootInstruction == null)
                        {
                            throw new Exception("Root instruction not found.");
                        }

                        settings.UpdateProgress("Writing instructions debug");
                        if (settings.WriteDebugFiles)
                        {
                            DebugInstruction(rootInstruction);
                        }

                        // Run instructions and write to the result stream
                        settings.UpdateProgress("Generating result");
                        OpenXmlWriter writer  = new OpenXmlWriter(targetBodyHost, sourceBodyHost);
                        WriterContext context = new WriterContext {
                            ExternalServices = ExternalServices,
                            Writer           = writer,
                            Template         = template,
                            Settings         = settings
                        };
                        rootInstruction.Generate(context);
                    }
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Return the entities for this data source.
 /// </summary>
 /// <param name="context">Unused.</param>
 /// <returns>
 /// Some dummy 'Resource' entities, with position indexes.
 /// </returns>
 public override IEnumerable <DataElement> GetData(WriterContext context)
 {
     return(Enumerable.Range(0, 3)
            .Select(pos => new DataElement(TestEntity, pos)));
 }