コード例 #1
0
        public static PartialDictionary <string, string> GetOptions(ContentControl cc)
        {
            PartialDictionary <string, string> options = new PartialDictionary <string, string>();

            foreach (ContentControlListEntry option in cc.DropdownListEntries)
            {
                options.Add(option.Text, option.Value);
            }
            return(options);
        }
コード例 #2
0
        public void GetFromDocumentShape(Document doc, ContentControl cc, Microsoft.Office.Interop.Word.Shape shp)
        {
            DateTime timerStart = DateTime.Now;

            FieldType = WordFieldInfo.ConvertFieldType(cc);
            if (FieldType == FieldType.DropDownList)
            {
                Options = GetOptions(cc);
            }
            X      = GetXFromShape(shp);
            Y      = GetYFromShape(shp);
            Height = shp.Height;
            Width  = shp.Width;
            Page   = cc.Range.Information[WdInformation.wdActiveEndPageNumber];
            Status = "Done";
            DateTime timerStop = DateTime.Now;

            TimeToGetLocation = timerStop - timerStart;
        }
コード例 #3
0
        public void GetFromDocument(Document doc, ContentControl cc)
        {
            DateTime timerStart = DateTime.Now;

            Status = "Initializing";

            FieldType = WordFieldInfo.ConvertFieldType(cc);
            if (FieldType == FieldType.DropDownList)
            {
                Options = GetOptions(cc);
            }
            Range fieldRange = cc.Range;

            Status = "Selecting";
            object collapseDirectionStart = WdCollapseDirection.wdCollapseStart;
            object collapseDirectionEnd   = WdCollapseDirection.wdCollapseEnd;
            object moveUnit = WdUnits.wdCharacter;

            fieldRange.Collapse(ref collapseDirectionStart);
            fieldRange.Move(ref moveUnit, -1);
            Status = "Getting Page";

            double pageWidth        = 0.0;
            double pageWidthUsable  = 0.0;
            double pageHeight       = 0.0;
            double pageTopMargin    = 0.0;
            double pageBottomMargin = 0.0;
            double pageHeightUsable = 0.0;

            try
            {
                pageWidth        = fieldRange.PageSetup.PageWidth;
                pageWidthUsable  = pageWidth - fieldRange.PageSetup.LeftMargin - fieldRange.PageSetup.RightMargin - fieldRange.PageSetup.Gutter;
                pageHeight       = fieldRange.PageSetup.PageHeight;
                pageTopMargin    = fieldRange.PageSetup.TopMargin;
                pageBottomMargin = fieldRange.PageSetup.BottomMargin;
                pageHeightUsable = pageHeight - pageTopMargin - pageBottomMargin;
            }
            catch (Exception ex)
            {
                // The most likely scenario is that we're in a textbox so there's no page context, ignore this exception and accept zeros for the page information
                System.Diagnostics.Trace.WriteLine($"{ex}");
            }
            Page = fieldRange.Information[WdInformation.wdActiveEndPageNumber];
            int colNum = fieldRange.Information[WdInformation.wdEndOfRangeColumnNumber];
            int rowNum = fieldRange.Information[WdInformation.wdEndOfRangeRowNumber];

            // Collapsed to START so we get the start values here.
            X = fieldRange.Information[WdInformation.wdHorizontalPositionRelativeToPage];
            Y = fieldRange.Information[WdInformation.wdVerticalPositionRelativeToPage]; Status = "Checking for Table";
            if (colNum > 0 && rowNum > 0)
            {
                // This is inside a table cell, the field bounds are the cell bounds
                Table tbl  = fieldRange.Tables[1];
                Cell  cell = tbl.Cell(rowNum, colNum);
                X     -= cell.LeftPadding;
                Y     -= cell.TopPadding;
                Width  = cell.Width;
                Height = cell.Height;
                if (Height == 9999999 && FieldType == FieldType.MultiLine)
                {
                    // Work around height not being accurate
                    double nextY;
                    if (tbl.Rows.Count > rowNum)
                    {
                        nextY = tbl.Rows[rowNum + 1].Range.Information[WdInformation.wdVerticalPositionRelativeToPage];
                    }
                    else
                    {
                        Range next = tbl.Range; // tbl.Cell(tbl.Rows.Count, tbl.Columns.Count).Range;
                        next.Collapse(ref collapseDirectionEnd);
                        //object moveForward = WdConstants.wdForward;
                        //next.MoveUntil("^p",ref  moveForward);
                        nextY = next.Information[WdInformation.wdVerticalPositionRelativeToPage];
                    }
                    // If we're on the next page, add the usable page height and remove the top margin to get field size
                    if (nextY < Y)
                    {
                        nextY += pageHeightUsable - pageTopMargin;
                    }
                    Height = nextY - Y;
                    if (Height < 0.0)
                    {
                        Height = 0.0;
                    }
                }
            }
            if (Width == 0 || Width > 5000 || Height == 0 || Height > 5000) // Need to fixup
            {
                Status     = "Finding field width/height";
                fieldRange = cc.Range;

                fieldRange.Collapse(ref collapseDirectionEnd);
                double endX = fieldRange.Information[WdInformation.wdHorizontalPositionRelativeToPage];
                if (Width == 0 || Width > 5000)
                {
                    Width = Math.Abs(endX - X);
                }
                if (Width == 0)
                {
                    Width = pageWidthUsable; // Not in table, and no width, so set it to usable page width
                    // Adjust for left indent
                    float leftIndent = fieldRange.ParagraphFormat.LeftIndent + fieldRange.ParagraphFormat.FirstLineIndent;
                    if (leftIndent > 0 && leftIndent < pageWidthUsable)
                    {
                        Width -= leftIndent; X += leftIndent;
                    }
                }
                double endY = fieldRange.Information[WdInformation.wdVerticalPositionRelativeToPage];
                if (Height == 0 || Height > 5000)
                {
                    // Try to get the difference between the start and end
                    Height = endY - Y;
                    if (Height < 0) // Page Transition, add one page height
                    {
                        Height    += pageHeightUsable - pageTopMargin;
                        SpansPages = true;
                    }
                    else if (Height == 0) // Still don't have a height, set to font height
                    {
                        Font rangeFont = fieldRange.Font;
                        Height += rangeFont.Size;
                    }
                }
            }
            Status = "Done";
            DateTime timerStop = DateTime.Now;

            TimeToGetLocation = timerStop - timerStart;
        }