本文整理汇总了C#中Document.GetText方法的典型用法代码示例。如果您正苦于以下问题:C# Document.GetText方法的具体用法?C# Document.GetText怎么用?C# Document.GetText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Document
的用法示例。
在下文中一共展示了Document.GetText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
// Check for license and apply if exists
string licenseFile = AppDomain.CurrentDomain.BaseDirectory + "Aspose.Words.lic";
if (File.Exists(licenseFile))
{
// Apply Aspose.Words API License
Aspose.Words.License license = new Aspose.Words.License();
// Place license file in Bin/Debug/ Folder
license.SetLicense("Aspose.Words.lic");
}
Document doc = new Document("../../data/document.doc");
// Enter a dummy field into the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField("MERGEFIELD Field");
// GetText will retrieve all field codes and special characters
Console.WriteLine("GetText() Result: " + doc.GetText());
string text = doc.GetText();
text = text.Replace(ControlChar.Cr, ControlChar.CrLf);
Console.WriteLine("Replaced text Result: " + text);
}
示例2: AddRemove
public void AddRemove()
{
//ExStart
//ExFor:Document.Sections
//ExFor:Section.Clone
//ExFor:SectionCollection
//ExFor:NodeCollection.RemoveAt(Int32)
//ExSummary:Shows how to add/remove sections in a document.
// Open the document.
Document doc = new Document(MyDir + "Section.AddRemove.doc");
// This shows what is in the document originally. The document has two sections.
Console.WriteLine(doc.GetText());
// Delete the first section from the document
doc.Sections.RemoveAt(0);
// Duplicate the last section and append the copy to the end of the document.
int lastSectionIdx = doc.Sections.Count - 1;
Section newSection = doc.Sections[lastSectionIdx].Clone();
doc.Sections.Add(newSection);
// Check what the document contains after we changed it.
Console.WriteLine(doc.GetText());
//ExEnd
Assert.AreEqual("Hello2\x000cHello2\x000c", doc.GetText());
}
示例3: BodyEnsureMinimum
public void BodyEnsureMinimum()
{
//ExStart
//ExFor:Section.Body
//ExFor:Body.EnsureMinimum
//ExSummary:Clears main text from all sections from the document leaving the sections themselves.
// Open a document.
Document doc = new Document(MyDir + "Section.BodyEnsureMinimum.doc");
// This shows what is in the document originally. The document has two sections.
Console.WriteLine(doc.GetText());
// Loop through all sections in the document.
foreach (Section section in doc.Sections)
{
// Each section has a Body node that contains main story (main text) of the section.
Body body = section.Body;
// This clears all nodes from the body.
body.RemoveAllChildren();
// Technically speaking, for the main story of a section to be valid, it needs to have
// at least one empty paragraph. That's what the EnsureMinimum method does.
body.EnsureMinimum();
}
// Check how the content of the document looks now.
Console.WriteLine(doc.GetText());
//ExEnd
Assert.AreEqual("\x000c\x000c", doc.GetText());
}
示例4: Main
static void Main(string[] args)
{
Document doc = new Document("../../data/document.doc");
// Enter a dummy field into the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField("MERGEFIELD Field");
// GetText will retrieve all field codes and special characters
Console.WriteLine("GetText() Result: " + doc.GetText());
string text = doc.GetText();
text = text.Replace(ControlChar.Cr, ControlChar.CrLf);
Console.WriteLine("Replaced text Result: " + text);
}
示例5: Read
public string Read(Stream inputStream)
{
try
{
var document = new Document(inputStream);
return document.GetText();
}
catch (Exception ex)
{
throw new ReadException(ErrorMessages.ReadTextFailed, ex);
}
}
示例6: DeleteSelection
public void DeleteSelection()
{
//ExStart
//ExFor:Node.Range
//ExFor:Range.Delete
//ExSummary:Shows how to delete a section from a Word document.
// Open Word document.
Document doc = new Document(MyDir + "Range.DeleteSection.doc");
// The document contains two sections. Each section has a paragraph of text.
Console.WriteLine(doc.GetText());
// Delete the first section from the document.
doc.Sections[0].Range.Delete();
// Check the first section was deleted by looking at the text of the whole document again.
Console.WriteLine(doc.GetText());
//ExEnd
Assert.AreEqual("Hello2\x000c", doc.GetText());
}
示例7: Run
public static void Run()
{
// ExStart:ExtractTextOnly
Document doc = new Document();
// Enter a dummy field into the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField("MERGEFIELD Field");
// GetText will retrieve all field codes and special characters
Console.WriteLine("GetText() Result: " + doc.GetText());
// ToString will export the node to the specified format. When converted to text it will not retrieve fields code
// Or special characters, but will still contain some natural formatting characters such as paragraph markers etc.
// This is the same as "viewing" the document as if it was opened in a text editor.
Console.WriteLine("ToString() Result: " + doc.ToString(SaveFormat.Text));
// ExEnd:ExtractTextOnly
}
示例8: ReplaceSimple
public void ReplaceSimple()
{
//ExStart
//ExFor:Range.Replace(String,String,Boolean,Boolean)
//ExSummary:Simple find and replace operation.
// Open the document.
Document doc = new Document(MyDir + "Range.ReplaceSimple.doc");
// Check the document contains what we are about to test.
Console.WriteLine(doc.FirstSection.Body.Paragraphs[0].GetText());
// Replace the text in the document.
doc.Range.Replace("_CustomerName_", "James Bond", false, false);
// Save the modified document.
doc.Save(MyDir + @"\Artifacts\Range.ReplaceSimple.doc");
//ExEnd
Assert.AreEqual("Hello James Bond,\r\x000c", doc.GetText());
}
示例9: DocumentByteArray
public void DocumentByteArray()
{
//ExStart
//ExId:DocumentToFromByteArray
//ExSummary:Shows how to convert a document object to an array of bytes and back into a document object again.
// Load the document.
Document doc = new Document(MyDir + "Document.doc");
// Create a new memory stream.
MemoryStream outStream = new MemoryStream();
// Save the document to stream.
doc.Save(outStream, SaveFormat.Docx);
// Convert the document to byte form.
byte[] docBytes = outStream.ToArray();
// The bytes are now ready to be stored/transmitted.
// Now reverse the steps to load the bytes back into a document object.
MemoryStream inStream = new MemoryStream(docBytes);
// Load the stream into a new document object.
Document loadDoc = new Document(inStream);
//ExEnd
Assert.AreEqual(doc.GetText(), loadDoc.GetText());
}
示例10: TrimWhiteSpaces
public void TrimWhiteSpaces(bool option, string expectedText)
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField("MERGEFIELD field", null);
doc.MailMerge.TrimWhitespaces = option;
doc.MailMerge.Execute(new[] { "field" }, new object[] { " first line\rsecond line\rthird line " });
Assert.AreEqual(expectedText, doc.GetText());
}
示例11: ExtractNestedTypes
private string ExtractNestedTypes(string source, bool staticOnly)
{
Func<TypeDeclaration, bool> predicate = null;
Func<TypeDeclaration, TextRange> selector = null;
Document document = new Document();
document.set_Language(new CSharpSyntaxLanguage());
document.set_Text(source);
IAstNode root = null;
TextRange[] rangeArray = null;
ClassDeclaration declaration = null;
for (int i = 0; i < 50; i++)
{
if (i > 0)
{
Thread.Sleep(50);
}
root = document.get_SemanticParseData() as IAstNode;
if (root != null)
{
declaration = root.get_ChildNodes().OfType<ClassDeclaration>().FirstOrDefault<ClassDeclaration>(c => c.get_FullName() == "UserQuery");
if (declaration != null)
{
if (i < 3)
{
i = 0x2f;
}
else
{
i = 0x2d;
}
if (predicate == null)
{
predicate = m => !staticOnly || ((m is ClassDeclaration) && ((m.get_Modifiers() & 0x100000) == 0x100000));
}
if (selector == null)
{
selector = m => (((m.get_AttributeSections() == null) || (m.get_AttributeSections().Count == 0)) || ((m.get_AttributeSections().get_Item(0).get_StartOffset() < 0) || (m.get_AttributeSections().get_Item(0).get_StartOffset() <= root.get_StartOffset()))) ? m.get_TextRange() : new TextRange(m.get_AttributeSections().get_Item(0).get_StartOffset(), m.get_TextRange().get_EndOffset());
}
rangeArray = declaration.get_Members().OfType<TypeDeclaration>().Where<TypeDeclaration>(predicate).Select<TypeDeclaration, TextRange>(selector).ToArray<TextRange>();
if (rangeArray.Length > 0)
{
break;
}
}
}
}
string text = document.GetText(0);
StringBuilder builder = new StringBuilder();
StringBuilder builder2 = new StringBuilder();
int startIndex = 0;
foreach (TextRange range in rangeArray)
{
builder.Append(text.Substring(startIndex, range.get_StartOffset() - startIndex));
int introduced18 = range.get_StartOffset();
int num4 = text.Take<char>((introduced18 + range.get_Length())).Count<char>(c => (c == '\n')) + 1;
builder.Append("\n#line " + num4 + "\n");
num4 = text.Take<char>(range.get_StartOffset()).Count<char>(c => (c == '\n')) + 1;
object[] objArray = new object[5];
objArray[0] = "#line ";
objArray[1] = num4;
objArray[2] = "\n";
int introduced19 = range.get_StartOffset();
objArray[3] = text.Substring(introduced19, range.get_Length());
objArray[4] = "\n";
builder2.Append(string.Concat(objArray));
startIndex = range.get_EndOffset();
}
builder.Append(text.Substring(startIndex));
return (builder.ToString() + "\n" + builder2.ToString());
}
示例12: OpenFromStream
public void OpenFromStream()
{
//ExStart
//ExFor:Document.#ctor(Stream)
//ExId:OpenFromStream
//ExSummary:Opens a document from a stream.
// Open the stream. Read only access is enough for Aspose.Words to load a document.
Stream stream = File.OpenRead(MyDir + "Document.doc");
// Load the entire document into memory.
Document doc = new Document(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
stream.Close();
// ... do something with the document
//ExEnd
Assert.AreEqual("Hello World!\x000c", doc.GetText());
}
示例13: DocumentGetText_ToTxt
public void DocumentGetText_ToTxt()
{
//ExStart
//ExFor:CompositeNode.GetText
//ExFor:Node.ToTxt
//ExId:NodeTxtExportDifferences
//ExSummary:Shows the difference between calling the GetText and ToTxt methods on a node.
Document doc = new Document();
// Enter a dummy field into the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField("MERGEFIELD Field");
// GetText will retrieve all field codes and special characters
Console.WriteLine("GetText() Result: " + doc.GetText());
// ToTxt will not retrieve fields code or special characters, but will still contain some natural formatting characters
// such as paragraph markers etc. This is the same as "viewing" the document as if it was opened in a text editor
// Only the results of fields are shown without any internal codes or characters
Console.WriteLine("ToTxt() Result: " + doc.ToTxt());
//ExEnd
}
示例14: DocumentGetText_ToString
public void DocumentGetText_ToString()
{
//ExStart
//ExFor:CompositeNode.GetText
//ExFor:Node.ToString(SaveFormat)
//ExId:NodeTxtExportDifferences
//ExSummary:Shows the difference between calling the GetText and ToString methods on a node.
Document doc = new Document();
// Enter a dummy field into the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField("MERGEFIELD Field");
// GetText will retrieve all field codes and special characters
Console.WriteLine("GetText() Result: " + doc.GetText());
// ToString will export the node to the specified format. When converted to text it will not retrieve fields code
// or special characters, but will still contain some natural formatting characters such as paragraph markers etc.
// This is the same as "viewing" the document as if it was opened in a text editor.
Console.WriteLine("ToString() Result: " + doc.ToString(SaveFormat.Text));
//ExEnd
}
示例15: CreateFromScratch
public void CreateFromScratch()
{
//ExStart
//ExFor:Node.GetText
//ExFor:CompositeNode.RemoveAllChildren
//ExFor:CompositeNode.AppendChild
//ExFor:Section
//ExFor:Section.#ctor
//ExFor:Section.PageSetup
//ExFor:PageSetup.SectionStart
//ExFor:PageSetup.PaperSize
//ExFor:SectionStart
//ExFor:PaperSize
//ExFor:Body
//ExFor:Body.#ctor
//ExFor:Paragraph
//ExFor:Paragraph.#ctor
//ExFor:Paragraph.ParagraphFormat
//ExFor:ParagraphFormat
//ExFor:ParagraphFormat.StyleName
//ExFor:ParagraphFormat.Alignment
//ExFor:ParagraphAlignment
//ExFor:Run
//ExFor:Run.#ctor(DocumentBase)
//ExFor:Run.Text
//ExFor:Inline.Font
//ExSummary:Creates a simple document from scratch using the Aspose.Words object model.
// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document doc = new Document();
// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.RemoveAllChildren();
// Create a new section node.
// Note that the section has not yet been added to the document,
// but we have to specify the parent document.
Section section = new Section(doc);
// Append the section to the document.
doc.AppendChild(section);
// Lets set some properties for the section.
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;
// The section that we created is empty, lets populate it. The section needs at least the Body node.
Body body = new Body(doc);
section.AppendChild(body);
// The body needs to have at least one paragraph.
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document.
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information.
Paragraph para = new Paragraph(doc);
body.AppendChild(para);
// We can set some formatting for the paragraph
para.ParagraphFormat.StyleName = "Heading 1";
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
// So far we have one empty paragraph in the document.
// The document is valid and can be saved, but lets add some text before saving.
// Create a new run of text and add it to our paragraph.
Run run = new Run(doc);
run.Text = "Hello World!";
run.Font.Color = System.Drawing.Color.Red;
para.AppendChild(run);
// As a matter of interest, you can retrieve text of the whole document and
// see that \x000c is automatically appended. \x000c is the end of section character.
Console.WriteLine("Hello World!\x000c", doc.GetText());
// Save the document.
doc.Save(MyDir + "Section.CreateFromScratch Out.doc");
//ExEnd
Assert.AreEqual("Hello World!\x000c", doc.GetText());
}