插入表格
static void LargeTable() { Console.WriteLine("\tLargeTable()"); var _directoryWithFiles = "docs\\"; using (var output = File.Open(_directoryWithFiles + "LargeTable.docx", FileMode.Create)) { using (var doc = DocX.Create(output)) { var tbl = doc.InsertTable(1, 18); var wholeWidth = doc.PageWidth - doc.MarginLeft - doc.MarginRight; var colWidth = wholeWidth / tbl.ColumnCount; var colWidths = new int[tbl.ColumnCount]; tbl.AutoFit = AutoFit.Contents; var r = tbl.Rows[0]; var cx = 0; foreach (var cell in r.Cells) { cell.Paragraphs.First().Append("Col " + cx); //cell.Width = colWidth; cell.MarginBottom = 0; cell.MarginLeft = 0; cell.MarginRight = 0; cell.MarginTop = 0; cx++; } tbl.SetBorder(TableBorderType.Bottom, BlankBorder); tbl.SetBorder(TableBorderType.Left, BlankBorder); tbl.SetBorder(TableBorderType.Right, BlankBorder); tbl.SetBorder(TableBorderType.Top, BlankBorder); tbl.SetBorder(TableBorderType.InsideV, BlankBorder); tbl.SetBorder(TableBorderType.InsideH, BlankBorder); doc.Save(); } } Console.WriteLine("\tCreated: docs\\LargeTable.docx\n"); } static void TableWithSpecifiedWidths() { Console.WriteLine("\tTableSpecifiedWidths()"); var _directoryWithFiles = "docs\\"; using (var output = File.Open(_directoryWithFiles + "TableSpecifiedWidths.docx", FileMode.Create)) { using (var doc = DocX.Create(output)) { var widths = new float[] { 200f, 100f, 300f }; var tbl = doc.InsertTable(1, widths.Length); tbl.SetWidths(widths); var wholeWidth = doc.PageWidth - doc.MarginLeft - doc.MarginRight; tbl.AutoFit = AutoFit.Contents; var r = tbl.Rows[0]; var cx = 0; foreach (var cell in r.Cells) { cell.Paragraphs.First().Append("Col " + cx); //cell.Width = colWidth; cell.MarginBottom = 0; cell.MarginLeft = 0; cell.MarginRight = 0; cell.MarginTop = 0; cx++; } //add new rows for (var x = 0; x < 5; x++) { r = tbl.InsertRow(); cx = 0; foreach (var cell in r.Cells) { cell.Paragraphs.First().Append("Col " + cx); //cell.Width = colWidth; cell.MarginBottom = 0; cell.MarginLeft = 0; cell.MarginRight = 0; cell.MarginTop = 0; cx++; } } tbl.SetBorder(TableBorderType.Bottom, BlankBorder); tbl.SetBorder(TableBorderType.Left, BlankBorder); tbl.SetBorder(TableBorderType.Right, BlankBorder); tbl.SetBorder(TableBorderType.Top, BlankBorder); tbl.SetBorder(TableBorderType.InsideV, BlankBorder); tbl.SetBorder(TableBorderType.InsideH, BlankBorder); doc.Save(); } } Console.WriteLine("\tCreated: docs\\TableSpecifiedWidths.docx\n"); }
文档加密
static void ProtectedDocument() { Console.WriteLine("\tHelloWorldPasswordProtected()"); // Create a new document. using (DocX document = DocX.Create(@"docs\HelloWorldPasswordProtected.docx")) { // Insert a Paragraph into this document. Paragraph p = document.InsertParagraph(); // Append some text and add formatting. p.Append("Hello World!^011Hello World!") .Font(new Font("Times New Roman")) .FontSize(32) .Color(WindowsColor.Blue) .Bold(); // Save this document to disk with different options // Protected with password for Read Only EditRestrictions erReadOnly = EditRestrictions.readOnly; document.AddProtection(erReadOnly, "oracle"); document.SaveAs(@"docs\\HelloWorldPasswordProtectedReadOnly.docx"); Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedReadOnly.docx\n"); // Protected with password for Comments EditRestrictions erComments = EditRestrictions.comments; document.AddProtection(erComments, "oracle"); document.SaveAs(@"docs\\HelloWorldPasswordProtectedCommentsOnly.docx"); Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedCommentsOnly.docx\n"); // Protected with password for Forms EditRestrictions erForms = EditRestrictions.forms; document.AddProtection(erForms, "oracle"); document.SaveAs(@"docs\\HelloWorldPasswordProtectedFormsOnly.docx"); Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedFormsOnly.docx\n"); // Protected with password for Tracked Changes EditRestrictions erTrackedChanges = EditRestrictions.trackedChanges; document.AddProtection(erTrackedChanges, "oracle"); document.SaveAs(@"docs\\HelloWorldPasswordProtectedTrackedChangesOnly.docx"); Console.WriteLine("\tCreated: docs\\HelloWorldPasswordProtectedTrackedChangesOnly.docx\n"); // But it‘s also possible to add restrictions without protecting it with password. // Protected with password for Read Only document.AddProtection(erReadOnly); document.SaveAs(@"docs\\HelloWorldWithoutPasswordReadOnly.docx"); Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordReadOnly.docx\n"); // Protected with password for Comments document.AddProtection(erComments); document.SaveAs(@"docs\\HelloWorldWithoutPasswordCommentsOnly.docx"); Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordCommentsOnly.docx\n"); // Protected with password for Forms document.AddProtection(erForms); document.SaveAs(@"docs\\HelloWorldWithoutPasswordFormsOnly.docx"); Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordFormsOnly.docx\n"); // Protected with password for Tracked Changes document.AddProtection(erTrackedChanges); document.SaveAs(@"docs\\HelloWorldWithoutPasswordTrackedChangesOnly.docx"); Console.WriteLine("\tCreated: docs\\HelloWorldWithoutPasswordTrackedChangesOnly.docx\n"); } }
缩进
private static void Indentation() { Console.WriteLine("\tIndentation()"); // Create a new document. using (DocX document = DocX.Create(@"docs\Indentation.docx")) { // Create a new Paragraph. Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3"); // Indent only the first line of the Paragraph p.IndentationFirstLine = 1.0f; // Save all changes made to this document. document.Save(); Console.WriteLine("\tCreated: docs\\Indentation.docx\n"); } }
时间: 2024-11-09 18:37:33