本文整理汇总了C#中Aspose.Words.Document类的典型用法代码示例。如果您正苦于以下问题:C# Document类的具体用法?C# Document怎么用?C# Document使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Document类属于Aspose.Words命名空间,在下文中一共展示了Document类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VerticalMerge
public void VerticalMerge()
{
//ExStart
//ExFor:DocumentBuilder.InsertCell
//ExFor:DocumentBuilder.EndRow
//ExFor:CellMerge
//ExFor:CellFormat.VerticalMerge
//ExId:VerticalMerge
//ExSummary:Creates a table with two columns with cells merged vertically in the first column.
Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertCell();
builder.CellFormat.VerticalMerge = CellMerge.First;
builder.Write("Text in merged cells.");
builder.InsertCell();
builder.CellFormat.VerticalMerge = CellMerge.None;
builder.Write("Text in one cell");
builder.EndRow();
builder.InsertCell();
// This cell is vertically merged to the cell above and should be empty.
builder.CellFormat.VerticalMerge = CellMerge.Previous;
builder.InsertCell();
builder.CellFormat.VerticalMerge = CellMerge.None;
builder.Write("Text in another cell");
builder.EndRow();
builder.EndTable();
//ExEnd
}
示例2: PixelToPointEx
public void PixelToPointEx()
{
//ExStart
//ExFor:ConvertUtil.PixelToPoint(double)
//ExFor:ConvertUtil.PixelToPoint(double, double)
//ExSummary:Shows how to specify page properties in pixels with default and custom resolution.
Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Aspose.Words.PageSetup pageSetupNoDpi = builder.PageSetup;
pageSetupNoDpi.TopMargin = Aspose.Words.ConvertUtil.PixelToPoint(100.0);
pageSetupNoDpi.BottomMargin = Aspose.Words.ConvertUtil.PixelToPoint(100.0);
pageSetupNoDpi.LeftMargin = Aspose.Words.ConvertUtil.PixelToPoint(150.0);
pageSetupNoDpi.RightMargin = Aspose.Words.ConvertUtil.PixelToPoint(150.0);
pageSetupNoDpi.HeaderDistance = Aspose.Words.ConvertUtil.PixelToPoint(20.0);
pageSetupNoDpi.FooterDistance = Aspose.Words.ConvertUtil.PixelToPoint(20.0);
builder.Writeln("Hello world.");
builder.Document.Save(MyDir + "PageSetup.PageMargins.DefaultResolution Out.doc");
double myDpi = 150.0;
Aspose.Words.PageSetup pageSetupWithDpi = builder.PageSetup;
pageSetupWithDpi.TopMargin = Aspose.Words.ConvertUtil.PixelToPoint(100.0, myDpi);
pageSetupWithDpi.BottomMargin = Aspose.Words.ConvertUtil.PixelToPoint(100.0, myDpi);
pageSetupWithDpi.LeftMargin = Aspose.Words.ConvertUtil.PixelToPoint(150.0, myDpi);
pageSetupWithDpi.RightMargin = Aspose.Words.ConvertUtil.PixelToPoint(150.0, myDpi);
pageSetupWithDpi.HeaderDistance = Aspose.Words.ConvertUtil.PixelToPoint(20.0, myDpi);
pageSetupWithDpi.FooterDistance = Aspose.Words.ConvertUtil.PixelToPoint(20.0, myDpi);
builder.Document.Save(MyDir + "PageSetup.PageMargins.CustomResolution Out.doc");
//ExEnd
}
示例3: 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.
Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "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;
Aspose.Words.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());
}
示例4: BookmarkNameAndText
public void BookmarkNameAndText()
{
//ExStart
//ExFor:Bookmark
//ExFor:Bookmark.Name
//ExFor:Bookmark.Text
//ExFor:Range.Bookmarks
//ExId:BookmarksGetNameSetText
//ExSummary:Shows how to get or set bookmark name and text.
Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "Bookmark.doc");
// Use the indexer of the Bookmarks collection to obtain the desired bookmark.
Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["MyBookmark"];
// Get the name and text of the bookmark.
string name = bookmark.Name;
string text = bookmark.Text;
// Set the name and text of the bookmark.
bookmark.Name = "RenamedBookmark";
bookmark.Text = "This is a new bookmarked text.";
//ExEnd
Assert.AreEqual("MyBookmark", name);
Assert.AreEqual("This is a bookmarked text.", text);
}
示例5: ConvertWordToImage
/// <summary>
/// 将Word文档转换为图片的方法(该方法基于第三方DLL),你可以像这样调用该方法:
/// ConvertPDF2Image("F:\\PdfFile.doc", "F:\\", "ImageFile", 1, 20, ImageFormat.Png, 256);
/// </summary>
/// <param name="pdfInputPath">Word文件路径</param>
/// <param name="imageOutputPath">图片输出路径,如果为空,默认值为Word所在路径</param>
/// <param name="imageName">图片的名字,不需要带扩展名,如果为空,默认值为Word的名称</param>
/// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
/// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数</param>
/// <param name="imageFormat">设置所需图片格式,如果为null,默认格式为PNG</param>
/// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
public static void ConvertWordToImage(string wordInputPath, string imageOutputPath,
string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, float resolution)
{
try
{
// open word file
Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);
// validate parameter
if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); }
if (imageOutputPath.Trim().Length == 0) { imageOutputPath = Path.GetDirectoryName(wordInputPath); }
if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
if (imageName.Trim().Length == 0) { imageName = Path.GetFileNameWithoutExtension(wordInputPath); }
if (startPageNum <= 0) { startPageNum = 1; }
if (endPageNum > doc.PageCount || endPageNum <= 0) { endPageNum = doc.PageCount; }
if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
if (imageFormat == null) { imageFormat = ImageFormat.Png; }
if (resolution <= 0) { resolution = 128; }
ImageSaveOptions imageSaveOptions = new ImageSaveOptions(GetSaveFormat(imageFormat));
imageSaveOptions.Resolution = resolution;
// start to convert each page
for (int i = startPageNum; i <= endPageNum; i++)
{
imageSaveOptions.PageIndex = i - 1;
doc.Save(Path.Combine(imageOutputPath, imageName) + "_" + i.ToString() + "." + imageFormat.ToString(), imageSaveOptions);
}
}
catch (Exception ex)
{
throw ex;
}
}
示例6: InsertField
public void InsertField()
{
//ExStart
//ExFor:Paragraph.InsertField
//ExSummary:Shows how to insert field using several methods: "field code", "field code and field value", "field code and field value after a run of text"
Aspose.Words.Document doc = new Aspose.Words.Document();
//Get the first paragraph of the document
Paragraph para = doc.FirstSection.Body.FirstParagraph;
//Inseting field using field code
//Note: All methods support inserting field after some node. Just set "true" in the "isAfter" parameter
para.InsertField(" AUTHOR ", null, false);
//Using field type
//Note:
//1. For inserting field using field type, you can choose, update field before or after you open the document ("updateField" parameter)
//2. For other methods it's works automatically
para.InsertField(FieldType.FieldAuthor, false, null, true);
//Using field code and field value
para.InsertField(" AUTHOR ", "Test Field Value", null, false);
//Add a run of text
Run run = new Run(doc) { Text = " Hello World!" };
para.AppendChild(run);
//Using field code and field value before a run of text
//Note: For inserting field before/after a run of text you can use all methods above, just add ref on your text ("refNode" parameter)
para.InsertField(" AUTHOR ", "Test Field Value", run, false);
//ExEnd
}
示例7: ExecuteDataReader
public void ExecuteDataReader()
{
//ExStart
//ExFor:MailMerge.Execute(IDataReader)
//ExSummary:Executes mail merge from an ADO.NET DataReader.
// Open the template document
Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "MailingLabelsDemo.doc");
// Open the database connection.
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
DatabaseDir + "Northwind.mdb";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
// Open the data reader.
OleDbCommand cmd = new OleDbCommand(
"SELECT TOP 50 * FROM Customers ORDER BY Country, CompanyName", conn);
OleDbDataReader dataReader = cmd.ExecuteReader();
// Perform the mail merge
doc.MailMerge.Execute(dataReader);
// Close database.
dataReader.Close();
conn.Close();
doc.Save(MyDir + "MailMerge.ExecuteDataReader Out.doc");
//ExEnd
}
示例8: InsertNewColumnIntoTable
public void InsertNewColumnIntoTable()
{
Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Table.Document.doc");
Table table = (Table)doc.GetChild(NodeType.Table, 1, true);
//ExStart
//ExId:InsertNewColumn
//ExSummary:Shows how to insert a blank column into a table.
// Get the second column in the table.
Column column = Column.FromIndex(table, 1);
// Create a new column to the left of this column.
// This is the same as using the "Insert Column Before" command in Microsoft Word.
Column newColumn = column.InsertColumnBefore();
// Add some text to each of the column cells.
foreach (Cell cell in newColumn.Cells)
cell.FirstParagraph.AppendChild(new Run(doc, "Column Text " + newColumn.IndexOf(cell)));
//ExEnd
doc.Save(MyDir + "Table.InsertColumn Out.doc");
Assert.AreEqual(24, table.GetChildNodes(NodeType.Cell, true).Count);
Assert.AreEqual("Column Text 0", table.FirstRow.Cells[1].ToString(SaveFormat.Text).Trim());
Assert.AreEqual("Column Text 3", table.LastRow.Cells[1].ToString(SaveFormat.Text).Trim());
}
示例9: HorizontalMerge
public void HorizontalMerge()
{
//ExStart
//ExFor:CellMerge
//ExFor:CellFormat.HorizontalMerge
//ExId:HorizontalMerge
//ExSummary:Creates a table with two rows with cells in the first row horizontally merged.
Aspose.Words.Document doc = new Aspose.Words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.Write("Text in merged cells.");
builder.InsertCell();
// This cell is merged to the previous and should be empty.
builder.CellFormat.HorizontalMerge = CellMerge.Previous;
builder.EndRow();
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Write("Text in one cell.");
builder.InsertCell();
builder.Write("Text in another cell.");
builder.EndRow();
builder.EndTable();
//ExEnd
}
示例10: AddClonedRowToTable
public void AddClonedRowToTable()
{
//ExStart
//ExFor:Row
//ExId:AddClonedRowToTable
//ExSummary:Shows how to make a clone of the last row of a table and append it to the table.
Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "Table.SimpleTable.doc");
// Retrieve the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
// Clone the last row in the table.
Row clonedRow = (Row)table.LastRow.Clone(true);
// Remove all content from the cloned row's cells. This makes the row ready for
// new content to be inserted into.
foreach (Cell cell in clonedRow.Cells)
cell.RemoveAllChildren();
// Add the row to the end of the table.
table.AppendChild(clonedRow);
doc.Save(ExDir + "Table.AddCloneRowToTable Out.doc");
//ExEnd
// Verify that the row was cloned and appended properly.
Assert.AreEqual(5, table.Rows.Count);
Assert.AreEqual(string.Empty, table.LastRow.ToString(SaveFormat.Text).Trim());
Assert.AreEqual(2, table.LastRow.Cells.Count);
}
示例11: ChangeTOCTabStops
public void ChangeTOCTabStops()
{
//ExStart
//ExFor:TabStop
//ExFor:ParagraphFormat.TabStops
//ExFor:Style.StyleIdentifier
//ExFor:TabStopCollection.RemoveByPosition
//ExFor:TabStop.Alignment
//ExFor:TabStop.Position
//ExFor:TabStop.Leader
//ExId:ChangeTOCTabStops
//ExSummary:Shows how to modify the position of the right tab stop in TOC related paragraphs.
Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "Document.TableOfContents.doc");
// Iterate through all paragraphs in the document
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
// Check if this paragraph is formatted using the TOC result based styles. This is any style between TOC and TOC9.
if (para.ParagraphFormat.Style.StyleIdentifier >= StyleIdentifier.Toc1 && para.ParagraphFormat.Style.StyleIdentifier <= StyleIdentifier.Toc9)
{
// Get the first tab used in this paragraph, this should be the tab used to align the page numbers.
TabStop tab = para.ParagraphFormat.TabStops[0];
// Remove the old tab from the collection.
para.ParagraphFormat.TabStops.RemoveByPosition(tab.Position);
// Insert a new tab using the same properties but at a modified position.
// We could also change the separators used (dots) by passing a different Leader type
para.ParagraphFormat.TabStops.Add(tab.Position - 50, tab.Alignment, tab.Leader);
}
}
doc.Save(ExDir + "Document.TableOfContentsTabStops Out.doc");
//ExEnd
}
示例12: 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.
Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "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 (Aspose.Words.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());
}
示例13: ReplaceHyperlinks
public void ReplaceHyperlinks()
{
// Specify your document name here.
Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "ReplaceHyperlinks.doc");
// Hyperlinks in a Word documents are fields, select all field start nodes so we can find the hyperlinks.
NodeList fieldStarts = doc.SelectNodes("//FieldStart");
foreach (FieldStart fieldStart in fieldStarts)
{
if (fieldStart.FieldType.Equals(FieldType.FieldHyperlink))
{
// The field is a hyperlink field, use the "facade" class to help to deal with the field.
Hyperlink hyperlink = new Hyperlink(fieldStart);
// Some hyperlinks can be local (links to bookmarks inside the document), ignore these.
if (hyperlink.IsLocal)
continue;
// The Hyperlink class allows to set the target URL and the display name
// of the link easily by setting the properties.
hyperlink.Target = NewUrl;
hyperlink.Name = NewName;
}
}
doc.Save(ExDir + "ReplaceHyperlinks Out.doc");
}
示例14: ConvertRtfToDocx
//ExStart
//ExId:MossRtf2Docx
//ExSummary:Converts an RTF document to OOXML.
public static void ConvertRtfToDocx(string inFileName, string outFileName)
{
// Load an RTF file into Aspose.Words.
Aspose.Words.Document doc = new Aspose.Words.Document(inFileName);
// Save the document in the OOXML format.
doc.Save(outFileName, SaveFormat.Docx);
}
示例15: RangesDeleteText
public void RangesDeleteText()
{
//ExStart
//ExId:RangesDeleteText
//ExSummary:Shows how to delete all characters of a range.
Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Document.doc");
doc.Sections[0].Range.Delete();
//ExEnd
}