将word文件转换为txt文件时如何忽略分页符? [英] how to ignore the page break when convert the word file to txt file?

查看:125
本文介绍了将word文件转换为txt文件时如何忽略分页符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将word文件转换为txt文件,当转换word page break转换为txt文件中的特殊符号时,任何人都知道如何通过c#删除所有分页符,我的编码在下面





  private   void  button1_Click( object  sender,RibbonControlEventArgs e)
{
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
string filename = Globals.ThisAddIn.Application.ActiveDocument.Name;
ReBitsCls c = new ReBitsCls(doc);
使用(StreamWriter Swriter = new StreamWriter( @ filename + .txt ,true))
{
foreach(doc.StoryRanges中的Microsoft.Office.Interop.Word.Range范围)
{
if(range.StoryType.Equals(Word.WdStoryType) .wdMainTextStory))
{
foreach(Microsoft.Office.Interop.Word.Paragraph Para in range.Paragraphs)
{
Word.Style style = Para.get_Style()as Word.Style;
string stylename = style.NameLocal;
Word.Style PreStyle = Para.get_Style()as Word.Style;
Word.Style NextStyle = Para.get_Style()as Word .Style;
if(stylename!=
05 07 图标 && stylename!= 08 09 FigBegin && stylename!= 08 10 FigEnd && stylename!= 08 14 TableEnd && stylename!= 08 13 TableBegin && stylename!= 14 00 EMSect && stylename!= 08 40 AppxEnd && stylename!= 08 39 AppxBegin
{
if(Para.Previous()!= null)
{
PreStyle = Para.Previous ()。Randge.get_Style()as Word.Style;
string s1 = PreStyle.NameLocal;
}
string s2 = stylename;
if(Para.Next()!= null)
{
NextStyle = Para.Next()。Range.get_Style()as Word.Style;
string s3 = NextStyle.NameLocal;
}

if(stylename ==
11 28 TPTitle
{
Swriter.Write(
< ; tp> );
}
Swriter.Write(Para.Range.Text); //数据内容
Swriter.Write(
\ n ); //数据内容
if(stylename ==
11 28 TPTitle || stylename == 11 29 TPSubtitle || stylename == 11 30 TPText || stylename == 11 31 PubName || stylename == 11 32 PubLocs || stylename == 11 33 PubDate || stylename == 11 18 AuthEd || stylename == 11 20 Affil
{
if((NextStyle.NameLocal!=
11 . 28 TPTitle && NextStyle .NameLocal!= 11 29 TPSubtitle &&& NextStyle.NameLocal!= 11 30 TPText && NextStyle.NameLocal!= 11 31 PubName && NextStyle.NameLocal!= 11 32 PubLocs &安培;&安培; NextStyle.NameLocal!= 11 33 PubDate && NextStyle.NameLocal!= 11 18 AuthEd && NextStyle.NameLocal!= 11 20 Affil ))
{
Swriter.Write(
< / tp > );
}
}

}
}
}
if(range.StoryType.Equals(Word.WdStoryType.wdFootnotesStory)|| range .Equals(Word.WdStoryType.wdEndnotesStory))
{
Swriter.Write(range.Text);
}
}
}
}

解决方案

< blockquote>文本文件中没有称为分页符的功能!



如果要将文本保存为文本文件,只需使用SaveAs [ ^ ]方法,但它导出整个文本。



我建议使用 Range.Information [ ^ ]识别它是什么类型的范围。如果是 wdInHeaderFooter [ ^ ],不要导出它!

请阅读:如何:以编程方式定义和选择文档中的范围 [ ^ ]


I convert the word file to txt file, when the conversion word page break convert as special symbol in txt file, any one know how to delete all page break in word via c#, my coding is below


private void button1_Click(object sender, RibbonControlEventArgs e)
{
    Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
    string filename = Globals.ThisAddIn.Application.ActiveDocument.Name;
    ReBitsCls c = new ReBitsCls(doc);
    using (StreamWriter Swriter = new StreamWriter(@"filename + ".txt", true))
    {
        foreach (Microsoft.Office.Interop.Word.Range range in doc.StoryRanges)
        {
            if (range.StoryType.Equals(Word.WdStoryType.wdMainTextStory))
            {
                foreach (Microsoft.Office.Interop.Word.Paragraph Para in range.Paragraphs)
                {
                    Word.Style style=Para.get_Style() as Word.Style;
                    string stylename = style.NameLocal;
                    Word.Style PreStyle = Para.get_Style() as Word.Style;
                    Word.Style NextStyle = Para.get_Style() as Word.Style;
                    if (stylename != "05.07 Icon" && stylename != "08.09 FigBegin" && stylename != "08.10 FigEnd" && stylename != "08.14 TableEnd" && stylename != "08.13 TableBegin" && stylename != "14.00 EMSect" && stylename != "08.40 AppxEnd" && stylename != "08.39 AppxBegin")
                    {
                        if (Para.Previous() != null)
                        {
                            PreStyle = Para.Previous().Range.get_Style() as Word.Style;
                            string s1 = PreStyle.NameLocal;
                        }
                        string s2 = stylename;
                        if (Para.Next() != null)
                        {
                            NextStyle = Para.Next().Range.get_Style() as Word.Style;
                            string s3 = NextStyle.NameLocal;
                        }

                        if (stylename == "11.28 TPTitle")
                        {
                            Swriter.Write("<tp>");
                        }
                        Swriter.Write(Para.Range.Text);   // Content of Data
                        Swriter.Write("\n");              //  Content of Data
                        if (stylename == "11.28 TPTitle" || stylename == "11.29 TPSubtitle" || stylename == "11.30 TPText" || stylename == "11.31 PubName" || stylename == "11.32 PubLocs" || stylename == "11.33 PubDate" || stylename == "11.18 AuthEd" || stylename == "11.20 Affil")
                        {
                            if ((NextStyle.NameLocal != "11.28 TPTitle" && NextStyle.NameLocal != "11.29 TPSubtitle" && NextStyle.NameLocal != "11.30 TPText" && NextStyle.NameLocal != "11.31 PubName" && NextStyle.NameLocal != "11.32 PubLocs" && NextStyle.NameLocal != "11.33 PubDate" && NextStyle.NameLocal != "11.18 AuthEd" && NextStyle.NameLocal != "11.20 Affil"))
                            {
                                Swriter.Write("</tp>");
                            }
                        }

                    }
                }
            }
            if (range.StoryType.Equals(Word.WdStoryType.wdFootnotesStory) || range.Equals(Word.WdStoryType.wdEndnotesStory))
            {
                Swriter.Write(range.Text);
            }
        }
    }
}

解决方案

There is no functinality called 'page break' in text files!

If you want to save text as text file, simple use SaveAs[^] method, but it exports entire text.

I'd suggest to use Range.Information[^] to recognize what kind of Range it is. If it is a wdInHeaderFooter[^], do not export it!
Please, read it: How to: Programmatically Define and Select Ranges in Documents[^]


这篇关于将word文件转换为txt文件时如何忽略分页符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆