どんな領域の仕事でもWordドキュメントは欠かせないものでしょう。しかしネットで検索できる多くのソフトの機能は不完全であるか、同一のものを重複しています。ようやく完全な機能を備えたソフトを見つけたら有料です。でも安心して、ここで無料かつ我々の要求をかなえるC#ライブラリーがでました、そして、以下はその強力な無料ライブラリーをとことん紹介しますね。
まずはどうやって手に入れますか?
無料版Spire.Docコンポーネントをダウンロード:Free Spire.Doc for.Net
またこの文章から直接MSIファイルをダウンロードすることもできます、これはソースコードも提供します。
Wordドキュメントへの操作まとめ
1、 既に存在しているWordドキュメントを開くこと、これはWordドキュメントを処理するもっとも必要と基礎的な手順です。このコンポーネントは3つの方法を提供しています。
方法1 既知の特定なWordドキュメントから新しいDocumentクラスのインスタンスを初期化します
Document document = new Document(@"..\..\Sample.docx");
方法2 フォルダからWordドキュメントをロードします
Document document = new Document();
document.LoadFromFile(@"..\..\Sample.docx");
方法3 ストリームファイルからWordドキュメントをロードします
Stream stream = File.OpenRead(@"..\..\Sample.docx");
Document document = new Document(stream);
2、 どうやってテーブルを作成しますか?
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace word
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();
Table table = section.AddTable(true);
table.ResetCells(2, 3);
TableRow row = table.Rows[0];
row.IsHeader = true;
Paragraph para = row.Cells[0].AddParagraph();
TextRange TR = para.AppendText("Item");
para = row.Cells[1].AddParagraph();
TR = para.AppendText("Description");
para = row.Cells[2].AddParagraph();
TR = para.AppendText("Qty");
document.SaveToFile("WordTable.docx");
System.Diagnostics.Process.Start("WordTable.docx");
}
}
}
行の高さと列の幅を設定することもできます。
3、 どうやってハイパーリンクを追加しますか?ここで、EmailリンクとWebmailリンクという二つの種類のハイパーリンクを追加することができます
using Spire.Doc;
using Spire.Doc.Documents;
namespace word
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();
//URLハイパーリンクを挿入する
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("ホームページ");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
// emailアドレスハイパーリンクを挿入する
paragraph = section.AddParagraph();
paragraph.AppendText("お問い合わせ");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink);
document.SaveToFile("Hyperlink.docx");
System.Diagnostics.Process.Start("Hyperlink.docx");
}
}
}
4.どうやって注釈を追加しますか?
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace word
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Home Page of ");
TextRange textRange = paragraph.AppendText("e-iceblue");
Comment comment1 = paragraph.AppendComment("www.e-iceblue.com");
comment1.AddItem(textRange);
comment1.Format.Author = "Blue";
comment1.Format.Initial = "Ice";
document.SaveToFile("Comment.docx");
System.Diagnostics.Process.Start("Comment.docx");
}
}
}
5.どうやってブックマークを追加しますか?
using Spire.Doc;
namespace wordBookmark
{
class Bookmark
{
static void Main(string[] args)
{
//ドキュメントをロードします
Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\Workexample.docx");
//ブックマークを挿入します
Section section = document.Sections[0];
section.Paragraphs[3].AppendBookmarkStart("Workexample");
section.Paragraphs[4].AppendBookmarkEnd("Wordexample");
//ドキュメントを保存して起動します
document.SaveToFile("Bookmark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bookmark.docx");
}
}
}
6.メールをマージします
using Spire.Doc;
namespace wordBookmark
{
class Bookmark
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile("Fax.docx");
string[] filedNames = new string[] { "Contact Name", "Fax", "Date" };
string[] filedValues = new string[] { "Nakamura", "+1 (69) 123456", System.DateTime.Now.Date.ToString() };
document.MailMerge.Execute(filedNames, filedValues);
document.SaveToFile("MailMerge.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("MailMerge.doc");
}
}
}
7.Wordドキュメントをマージします
using Spire.Doc;
namespace wordBookmark
{
class Bookmark
{
private static object document;
static void Main(string[] args)
{
//二つのドキュメントをロードします
Document DocOne = new Document();
DocOne.LoadFromFile(@"C:\Users\Administrator\Desktop\WorkTable.docx", FileFormat.Docx);
Document DocTwo = new Document();
DocTwo.LoadFromFile(@"C:\Users\Administrator\Desktop\WorkTable2.docx", FileFormat.Docx);
//マージします
foreach (Section sec in DocTwo.Sections)
{
DocOne.Sections.Add(sec.Clone());
}
//ドキュメントを保存して起動します
DocOne.SaveToFile("Merge.docx", FileFormat.Docx);
}
}
}
8.ドキュメントを保護します。パスワードを設置するか透かしを追加して保護することができます。透かしにはテキストかまたは画像もサポートされています。
using Spire.Doc;
using Spire.Doc.Documents;
namespace wordBookmark
{
class Bookmark
{
private static object document;
static void Main(string[] args)
{
//パスワードを利用して保護します
document.Encrypt("eiceblue");
//テキスト透かしを追加します
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = "Microsoft";
txtWatermark.FontSize = 90;
txtWatermark.Layout = WatermarkLayout.Diagonal;
document.Watermark = txtWatermark;
//画像透かしを追加します
PictureWatermark picture = new PictureWatermark();
picture.Picture = System.Drawing.Image.FromFile(@"..\imagess.jpeg");
picture.Scaling = 250;
document.Watermark = picture;
}
}
}
9. 変換機能は、Wordドキュメントを扱う場合の最も一般的な操作でしょう。Spire.doc for .NETの無料バージョンを使用すると、変換が非常に簡単になります。同様のコードを3行含める限り、WordをPDF、HTML、画像などの他の一般的に使用される形式に変換できます。
WordをPDFに
document.SaveToFile("Target PDF.PDF", FileFormat.PDF);
Wordを画像に
Image image = document.SaveToImages(0, ImageType.Bitmap);
image.Save("Sample.tiff", ImageFormat.Tiff);
WordをHTMLに
document.SaveToFile("Target HTML.html", FileFormat.Html);
WordDocViewer(""Target HTML.html");
結論:
これは本当に無料かつ強力なC#Wordコンポーネントであり、Word automatioなしでもちゃんと作業できます、そしていかなる第三方の機能を含めています。それでは今回の紹介はここまで、読んでいただきありがとうございます!