Example #1
0
        public void ProcessAssembly()
        {
            Done = false;
            ProcessFileImages(Assembly.DefaultSettings, null, WordDoc);
            ProcessFileText(Assembly.DefaultSettings, null, WordDoc);
            ProcessFileMargins(Assembly.DefaultSettings, null, WordDoc);
            ProcessFilePage(Assembly.DefaultSettings, null, WordDoc);
            WordDoc.TrackRevisions = false;

            for (int i = 0; i < Assembly.Files.Count; i++)
            {
                if (!Continue)
                {
                    Done = true; return;
                }
                BindingAssembly.File file = Assembly.Files[i];
                bool nextStartOnOdd       = false;
                if (i + 1 < Assembly.Files.Count &&
                    Assembly.Files[i + 1].Settings.Assembly.StartOnOdd)
                {
                    nextStartOnOdd = true;
                }
                ProcessFile(file, nextStartOnOdd);
            }

            WordDoc.Repaginate();
            Done = true;
        }
Example #2
0
        public void ProcessFileMargins(BindingAssembly.Settings settings, BindingAssembly.File file, Document wordDoc)
        {
            if (!settings.Margins.Perform)
            {
                return;
            }

            if (file != null)
            {
                file.SetStatus("Adjusting margins...");
            }
            try
            {
                wordDoc.PageSetup.TopMargin    = (float)InchesToPoints(settings.Margins.Top);
                wordDoc.PageSetup.BottomMargin = (float)InchesToPoints(settings.Margins.Bottom);
                wordDoc.PageSetup.LeftMargin   = (float)InchesToPoints(settings.Margins.Left);
                wordDoc.PageSetup.RightMargin  = (float)InchesToPoints(settings.Margins.Right);
                wordDoc.PageSetup.Gutter       = (float)InchesToPoints(settings.Margins.Gutter);
                wordDoc.PageSetup.GutterPos    = WdGutterStyle.wdGutterPosLeft; // TODO make configurable
                if (file != null)
                {
                    file.SetStatus("Finished adjusting margins...");
                }
            }
            catch (Exception ex)
            {
                if (file != null)
                {
                    file.SetStatus($"Unable to adjust margins.  Exception {ex.ToString()}");
                }
            }
        }
Example #3
0
        private void btnAddFile_Click(object sender, EventArgs e)
        {
            FileSettings fs = new FileSettings();

            BindingAssembly.File file = new BindingAssembly.File();
            file.Settings.CopyFrom(assembly.DefaultSettings);
            fs.FiletoScreen(file);
            fs.ShowDialog();
            if (!string.IsNullOrWhiteSpace(file.FileName))
            {
                assembly.Files.Add(file);
            }
        }
Example #4
0
        private void btnRemove_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = grdFiles.SelectedRows;

            if (rows != null && rows.Count == 1)
            {
                DialogResult dr = MessageBox.Show("Delete Row?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    BindingAssembly.File file = rows[0].DataBoundItem as BindingAssembly.File;
                    assembly.Files.Remove(file);
                }
            }
        }
Example #5
0
        private void grdFiles_DoubleClick(object sender, EventArgs e)
        {
            // Process row
            DataGridViewSelectedRowCollection rows = grdFiles.SelectedRows;

            if ((rows == null) || (rows.Count != 1))
            {
                return;                                      // Not sure what happened here
            }
            BindingAssembly.File file = rows[0].DataBoundItem as BindingAssembly.File;

            if (file != null)
            {
                FileSettings fs = new FileSettings();
                fs.FiletoScreen(file);
                fs.ShowDialog();
            }
            grdFiles.Refresh();
        }
Example #6
0
        private void btnFileDown_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = grdFiles.SelectedRows;

            if ((rows == null) || (rows.Count != 1))
            {
                return;
            }
            int viewIndex = rows[0].Index;

            BindingAssembly.File selectedRow = rows[0].DataBoundItem as BindingAssembly.File;
            int rowIndex = assembly.Files.IndexOf(selectedRow);

            BindingAssembly.File nextRow = assembly.Files[rowIndex + 1];
            assembly.Files.Remove(selectedRow);
            assembly.Files.Insert(rowIndex + 1, selectedRow);

            // Update the selected row
            grdFiles.Rows[viewIndex].Selected     = false;
            grdFiles.Rows[viewIndex + 1].Selected = true;
            grdFiles.Refresh();
        }
Example #7
0
        public void ProcessFilePage(BindingAssembly.Settings settings, BindingAssembly.File file, Document wordDoc)
        {
            if (!settings.Page.Perform)
            {
                return;
            }

            if (file != null)
            {
                file.SetStatus("Adjusting page size...");
            }
            try
            {
                wordDoc.PageSetup.PageHeight = (float)InchesToPoints(settings.Page.Height);
                wordDoc.PageSetup.PageWidth  = (float)InchesToPoints(settings.Page.Width);
                switch (settings.Page.Orientation)
                {
                case BindingAssembly.PageOrientation.Landscape:
                    wordDoc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
                    break;

                case BindingAssembly.PageOrientation.Portrait:
                    wordDoc.PageSetup.Orientation = WdOrientation.wdOrientPortrait;
                    break;
                }
                if (file != null)
                {
                    file.SetStatus("Finshed adjusting page size...");
                }
            }
            catch (Exception ex)
            {
                if (file != null)
                {
                    file.SetStatus($"Unable to set page size. Exception {ex.ToString()}");
                }
            }
        }
Example #8
0
 public void FiletoScreen(BindingAssembly.File file)
 {
     File             = file;
     txtFileName.Text = file.FileName;
     ctlSettings.SettingsToScreen(file.Settings);
 }
Example #9
0
        public void ProcessFile(BindingAssembly.File file, bool nextStartOnOdd)
        {
            object collapseEnd        = WdCollapseDirection.wdCollapseEnd; // used to collapse the selection
            bool   resetPageNumbering = (file.Settings.Assembly.DocBreak == BindingAssembly.DocumentBreak.Section &&
                                         file.Settings.Assembly.ResetPageNumber);

            try
            {
                // Assembly - Copy to Temp
                string fileName = file.FileName;
                if (file.Settings.Assembly.CopyToTempFirst)
                {
                    try {
                        file.SetStatus("Copying File...");
                        string destName = Path.GetTempFileName();
                        System.IO.File.Copy(file.FileName, destName, true);
                        fileName = destName;
                    }
                    catch (Exception ex)
                    {
                        file.SetStatus($"Error copying file. Exception {ex.ToString()}");
                        return;
                    }
                }

                // Open Document
                file.SetStatus("Opening Document...");
                Application app                   = WordDoc.Application;
                object      fileObj               = fileName;
                object      confirmConversions    = false;
                object      readOnly              = true;
                object      addToRecentFiles      = false;
                object      passwordDocument      = Missing.Value;
                object      passwordTemplate      = Missing.Value;
                object      revert                = true;
                object      writePasswordDocument = Missing.Value;
                object      writePasswordTemplate = Missing.Value;
                object      format                = Missing.Value;
                object      encoding              = Missing.Value;
                object      visible               = false;
                object      openAndRepair         = false;
                object      documentDirection     = Missing.Value;
                object      noEncodingDialog      = Missing.Value;
                object      xMLTransform          = Missing.Value;
                Document    newDoc                = app.Documents.Open(ref fileObj, ref confirmConversions, ref readOnly, ref addToRecentFiles,
                                                                       ref passwordDocument, ref passwordTemplate, ref revert, ref writePasswordDocument,
                                                                       ref writePasswordTemplate, ref format, ref encoding, ref visible, ref openAndRepair,
                                                                       ref documentDirection, ref noEncodingDialog, ref xMLTransform);
                // Get Statistics for File
                file.Images  = newDoc.InlineShapes.Count;
                file.Fields  = newDoc.Fields.Count;
                file.Numbers = newDoc.CountNumberedItems(WdNumberType.wdNumberListNum, 1);

                // - Images
                ProcessFileImages(file.Settings, file, newDoc);

                // - Text
                ProcessFileText(file.Settings, file, newDoc);

                // - Margins
                ProcessFileMargins(file.Settings, file, newDoc);

                // - Page Layout
                ProcessFilePage(file.Settings, file, newDoc);

                System.Diagnostics.Trace.WriteLine($"Master: {WordDoc.Characters.Count} New: {file.FileName} / {newDoc.Characters.Count}");
                // Add to master
                file.SetStatus("Copying references...");
                CopyReferences(newDoc, WordDoc);
                file.SetStatus("Copying document...");
                newDoc.Select();
                int newDocSections = newDoc.Sections.Count;
                app.Selection.Copy();

                // For performance, try to not grab the whole document as a selection
                Section lastSectionBeforePaste = WordDoc.Sections.Last;
                int     wordDocSections        = WordDoc.Sections.Count;
                lastSectionBeforePaste.Range.Select();
                app.Selection.Collapse(ref collapseEnd);
                app.Selection.Paste();
                if (resetPageNumbering)
                {
                    // Find the right section
                    Section sec = WordDoc.Sections[wordDocSections];
                    ResetNumberingForSection(sec);
                    WordDoc.Sections.Last.Range.Select();
                    app.Selection.Collapse(ref collapseEnd);
                }

                // Assembly - Break
                object breakType = null;
                switch (file.Settings.Assembly.DocBreak)
                {
                case BindingAssembly.DocumentBreak.Page:
                    breakType = WdBreakType.wdPageBreak;
                    break;

                case BindingAssembly.DocumentBreak.Section:
                    if (nextStartOnOdd)
                    {
                        breakType = WdBreakType.wdSectionBreakOddPage;
                    }
                    else
                    {
                        breakType = WdBreakType.wdSectionBreakNextPage;
                    }
                    break;
                }
                if (breakType != null)
                {
                    //Trace.WriteLine($"Adding break {file.Settings.Assembly.DocBreak} to {app.Selection.Document.Name}");
                    app.Selection.InsertBreak(ref breakType);
                }

                // Close Document
                object saveChanges    = WdSaveOptions.wdDoNotSaveChanges;
                object originalFormat = WdOpenFormat.wdOpenFormatAuto;
                object routeDocument  = Missing.Value;
                // Syntax to suppress compiler warning CS0467
                ((Microsoft.Office.Interop.Word._Document)(newDoc)).Close(ref saveChanges, ref originalFormat, ref routeDocument);

                // if copied, delete temp file.
                if (file.Settings.Assembly.CopyToTempFirst)
                {
                    file.SetStatus("Deleting temp file...");
                    File.Delete(fileName);
                }
                file.SetStatus("Processing completed...");
            }
            catch (Exception ex)
            {
                file.SetStatus($"Error processing file. Exception:{ex.ToString()}");
            }
        }
Example #10
0
        public void ProcessFileImages(BindingAssembly.Settings settings, BindingAssembly.File file, Document wordDoc)
        {
            if (!settings.Images.Perform)
            {
                return;
            }

            if (file != null)
            {
                file.SetStatus("Adjusting Images...");
            }

            // Get Maximum Height / Width
            double maxHeight = 0.0, maxWidth = 0.0;

            if (settings.Images.MaximumHeight != 0.0)
            {
                maxHeight = InchesToPoints(settings.Images.MaximumHeight);
            }
            else // No taller than page
            {
                maxHeight = wordDoc.PageSetup.PageHeight - wordDoc.PageSetup.TopMargin - wordDoc.PageSetup.BottomMargin;
                if (wordDoc.PageSetup.GutterPos == WdGutterStyle.wdGutterPosTop)
                {
                    maxHeight -= wordDoc.PageSetup.Gutter;
                }
            }

            if (settings.Images.MaximumWidth != 0.0)
            {
                maxWidth = InchesToPoints(settings.Images.MaximumWidth);
            }
            else // No wider than page
            {
                maxWidth = wordDoc.PageSetup.PageWidth - wordDoc.PageSetup.LeftMargin - wordDoc.PageSetup.RightMargin;
                if (wordDoc.PageSetup.GutterPos != WdGutterStyle.wdGutterPosTop)
                {
                    maxWidth -= wordDoc.PageSetup.Gutter;
                }
            }

            int currentImage = 0;

            while (currentImage < file.Images)
            {
                ++currentImage;
                if (file != null)
                {
                    file.SetStatus($"Processing image {currentImage}...");
                }
                InlineShape curShape = wordDoc.InlineShapes[currentImage];

                if (curShape.Type == WdInlineShapeType.wdInlineShapePicture)
                {                                  // If it's a picture we can do fixup with it
                    if (curShape.Width > maxWidth) // Too Big, must horizontally
                    {
                        float scaling = (float)maxWidth / curShape.Width;
                        curShape.Width  = (float)maxWidth;
                        curShape.Height = curShape.Height * scaling;
                    }

                    if (curShape.Height > maxHeight)
                    {
                        float scaling = (float)maxHeight / curShape.Height;
                        curShape.Height = (float)maxHeight;
                        curShape.Width  = curShape.Width * scaling;
                    }

                    if (curShape.Height > settings.Images.MinimumAdjustmentHeight)
                    {
                        // Adjust/Set Brightness/Contrast
                        curShape.PictureFormat.Brightness = .5F + ((float)settings.Images.Brightness / 200F);
                        curShape.PictureFormat.Contrast   = .5F + ((float)settings.Images.Contrast / 200F);

                        // Clear Indent/Set Centered
                        // TODO: Evaluate for later
                        //curShape.Range.ParagraphFormat.LeftIndent = 0;
                        //curShape.Range.ParagraphFormat.RightIndent = 0;
                        //curShape.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
                    }
                }
            }
            if (file != null)
            {
                file.SetStatus("Finished adjusting images...");
            }
        }
Example #11
0
        public void ProcessFileText(BindingAssembly.Settings settings, BindingAssembly.File file, Document wordDoc)
        {
            if (file != null)
            {
                file.SetStatus("Adjusting text...");
            }

            // -- Fields to Text
            if (settings.Text.FieldsToText)
            {
                if (file != null)
                {
                    file.SetStatus("Converting fields to text");
                }
                try
                {
                    int fieldIndex = wordDoc.Fields.Count;
                    while (fieldIndex > 0)
                    {
                        Field thisField = wordDoc.Fields[fieldIndex];
                        if (thisField.Kind != WdFieldKind.wdFieldKindCold)
                        {
                            try
                            {
                                thisField.Update();
                                thisField.Unlink();
                            }
                            catch (COMException cEx)
                            {
                                if (string.Compare(cEx.Message, "Object has been deleted.") != 0)
                                {
                                    throw cEx;
                                }
                            }
                            fieldIndex--;
                        }
                        else
                        {
                            fieldIndex--;
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (file != null)
                    {
                        file.SetStatus($"Failed to convert fields to text.  Exception {ex.ToString()}");
                    }
                }
            }

            // -- Numbers to Text
            if (settings.Text.NumbersToText)
            {
                if (file != null)
                {
                    file.SetStatus("Converting numbers to text...");
                }
                try
                {
                    wordDoc.ConvertNumbersToText(Missing.Value);
                }
                catch (Exception ex)
                {
                    file.SetStatus($"Filed to convert numbers to text.  Exception {ex.ToString()}");
                }
            }

            // -- Clear Comments
            if (settings.Text.ClearComments)
            {
                if (file != null)
                {
                    file.SetStatus($"Clearing comments...");
                }
                try
                {
                    while (wordDoc.Comments.Count > 0)
                    {
                        wordDoc.Comments[1].DeleteRecursively();
                    }
                }
                catch (Exception ex)
                {
                    if (file != null)
                    {
                        file.SetStatus($"Failed to clear comments. Exception {ex.ToString()}");
                    }
                }
            }
            // -- Accept Revisions
            if (settings.Text.AcceptRevisions)
            {
                if (file != null)
                {
                    file.SetStatus("Accepting revisions...");
                }
                try
                {
                    wordDoc.Revisions.AcceptAll();
                    wordDoc.TrackRevisions = false;
                }
                catch (Exception ex)
                {
                    if (file != null)
                    {
                        file.SetStatus($"Failed to accept revisions.  Exception {ex.ToString()}");
                    }
                }
            }
            if (file != null)
            {
                file.SetStatus("Finished adjusting text...");
            }
        }