Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Wed Jan 06, 2021 9:53 pm

Hello, I have an issue that I need to raise urgently, thank you very much for your help. I am new to the subject and I have limited time to research and develop, so I would be very happy if I could get answers to my questions. Thank you from now.

What I want to do is check the word file.
Title font, size, color of each page of Word file ------
font, size, color of each page's paragraph ---------
page width (left and right margin but in cm)

the accuracy of the bibliography (if the bibliography is added, go to the source and check whether the url is valid)

If these can be done can you help? Thank you

galabuska.07
 
Posts: 3
Joined: Mon Jan 04, 2021 11:57 pm

Thu Jan 07, 2021 8:21 am

Hello,

Thanks for your inquiry.

Please note that MS Word document is flow document and does not contain any information about its layout into lines and pages. Therefore, technically there is no “Page” and “Line” concept in Word document, our Spire.Doc product is based on MS Word specification, so it is impossible to get the paragraph on which page.

Please refer to the following code to check font, size, color of all content of the entire document, get the page width in cm and check whether the URL is valid, I have attached my test file too.
Code: Select all
class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile(@"E:\testdoc\CheckWord.docx");

            List<FontInfo> HeadInfos = new List<FontInfo>();
            List<FontInfo> ParaInfos = new List<FontInfo>();
            List<PageInfo> PageInfos = new List<PageInfo>();
            List<UrlInfo> UrlInfos = new List<UrlInfo>();


            foreach (Section sec in doc.Sections)
            {
                //page width (left and right margin but in cm)
                //2.54cm = 72pt
                PageInfo pageInfo = new PageInfo();
                pageInfo.Left = sec.PageSetup.Margins.Left * 2.54f / 72f;
                pageInfo.Right = sec.PageSetup.Margins.Right * 2.54f / 72f;
                PageInfos.Add(pageInfo);

                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if(obj.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        Paragraph para = obj as Paragraph;

                        if (para.StyleName.Contains("Heading"))     //Title font, size, color
                        {
                            foreach(DocumentObject paraObj in para.ChildObjects)
                            {
                                if (paraObj.DocumentObjectType == DocumentObjectType.TextRange)
                                {
                                    TextRange textRange = paraObj as TextRange;
                                    FontInfo head = new FontInfo();
                                    head.FontName = textRange.CharacterFormat.FontName;
                                    head.FontSize = textRange.CharacterFormat.FontSize;
                                    head.FontColor = textRange.CharacterFormat.TextColor;
                                    HeadInfos.Add(head);
                                }
                            }
                        }else if (!para.Text.Equals(""))            //font, size, color of each paragraph
                        {
                            foreach (DocumentObject paraObj in para.ChildObjects)
                            {
                                if (paraObj.DocumentObjectType == DocumentObjectType.TextRange)
                                {
                                    TextRange textRange = paraObj as TextRange;
                                    FontInfo parainfo = new FontInfo();
                                    parainfo.FontName = textRange.CharacterFormat.FontName;
                                    parainfo.FontSize = textRange.CharacterFormat.FontSize;
                                    parainfo.FontColor = textRange.CharacterFormat.TextColor;
                                    ParaInfos.Add(parainfo);

                                }

                                if(paraObj.DocumentObjectType == DocumentObjectType.Field)
                                {
                                    Field field = paraObj as Field;
                                    if (field.Type.Equals(FieldType.FieldHyperlink))
                                    {
                                        UrlInfo urlInfo = new UrlInfo();
                                        urlInfo.Url = field.FieldText;
                                        UrlInfos.Add(urlInfo);
                                    }
                                }
                            }
                        }

                    }
                }
            }


            string pattern = "[a-zA-z]+://[^\\s]*";
            Regex rgx = new Regex(pattern);
            TextSelection[] textSelections = doc.FindAllPattern(rgx);
            foreach(TextSelection textSelection in textSelections)
            {
                TextRange textRange = textSelection.GetAsOneRange();
                UrlInfo urlInfo = new UrlInfo();
                urlInfo.Url = textRange.Text;
                UrlInfos.Add(urlInfo);
            }

            //check whether the url is valid
            foreach (UrlInfo urlInfo in UrlInfos)
            {
                string url = urlInfo.Url;
                HttpWebRequest req = null;
                try
                {
                    req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
                    req.Method = "HEAD";
                    req.Timeout = 10000;
                    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                    if (Convert.ToInt32(res.StatusCode).ToString().Equals("200"))
                    {
                        urlInfo.Message = "the url is valid";
                    }
                }
                catch (Exception ex)
                {
                    urlInfo.Message = ex.Message;
                }
                finally
                {
                    if (req != null)
                    {
                        req.Abort();
                        req = null;
                    }
                }
            }
        }
    }

    public class FontInfo
    {
        public string FontName;
        public float FontSize;
        public Color FontColor;
    }

    public class PageInfo
    {
        public float Left;
        public float Right;
    }

    public class UrlInfo
    {
        public string Url;
        public string Message;
    }


If the code does not match your needs, please provide us with your input file and result sample. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
You do not have the required permissions to view the files attached to this post.
User avatar

Marcia.Zhou
 
Posts: 225
Joined: Wed Nov 04, 2020 2:29 am

Mon Jan 11, 2021 9:52 am

Hello,

Hope you are doing well.

How is the issue now? If it still exists, could you please provide us with your input file and result sample for further investigation?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 225
Joined: Wed Nov 04, 2020 2:29 am

Sat Jan 23, 2021 4:16 pm

hello i haven't achieved anything and 4 hours left

galabuska.07
 
Posts: 3
Joined: Mon Jan 04, 2021 11:57 pm

Mon Jan 25, 2021 1:53 am

Hello,

Thanks for your feedback and sorry for the late reply as weekend.

Does the code I provided on Jan 07 achieve your needs? If not, to help us solve your issue effectively and quickly, please provide us with your input file and result sample.

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 225
Joined: Wed Nov 04, 2020 2:29 am

Fri Jan 29, 2021 7:23 am

Hello,

Hope you are doing well.

How is the issue now? If it still exists, could you please provide your input file and result sample to us for further investigation?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 225
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc