-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
690 lines (547 loc) · 30 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
using GemBox.Document;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Paragraph = GemBox.Document.Paragraph;
using Run = GemBox.Document.Run;
namespace JobYestWORDConsole
{
class Program
{
static void Main(string[] args)
{
// Init(); // тестовое создание документа и запись в него
//TestDoc(); //Запись в уже созданный документ
// TestOpenXML(); //библиотека OpenXML
// GemBoxDocument();
// GemBoxDocument2();
// GemBoxDocument0();
//GemBoxDocumentTest();
// GetBoxDoc("Writing.docx");
// GetBoxDocPDF("Writing.docx");
// GetBoxCreateWord();
// GetBoxCreateWord2(); // Метод работает!!!!!
//Печать на принтере нарямую из кода
// JobPrintDoc.TestPrint();
// JobPrintDoc.ConverdToBase64String();
string pathFolder = @"C:\\1\\";
string videoUrl = "https://www.youtube.com/watch?v=lzm5llVmR2E";
string nameFile = "test";
RabotaYoutube.SaveMP3(pathFolder, videoUrl, nameFile);
Console.WriteLine("Для выхода нажмите любую клавишу!");
Console.ReadKey();
}
/// <summary>
/// Тестовой метод 2
/// </summary>
static void GetBoxCreateWord2()
{
string[] temZnach = new string[] { "Gthdjt", "Массив представляет", "мы можем" };
string temp = "";
// создание ссылок дирикторий(папок для картинки). Корневая папка
var originalDirectory = new DirectoryInfo(string.Format(@"~Archive_Documents\\Uploads"));
//Бесплатное Лицензия
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Создание нового дока.
var document = new DocumentModel();
try
{
for (int i = 0; i < temZnach.Length; i++)
{
temp += (temZnach[i] + Environment.NewLine);
document.Content.LoadText(temp);
};
document.Save("Archive_Documents/TestSaveDoc.docx");
// document.Save("`Archive_Documents/TestSaveDoc.pdf");
}
catch (Exception ex)
{
Console.WriteLine("Ошибка при работе" + ex);
}
}
/// <summary>
/// Тестовой вариант
/// </summary>
static void GetBoxCreateWord()
{
string[] temZnach = new string[] { "Gthdjt", "Массив представляет", "мы можем" };
string temp = "";
//Лицензия
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Создание нового дока.
var document = new DocumentModel();
try
{
for (int i = 0; i < temZnach.Length; i++)
{
// temp += (temZnach[i] + Environment.NewLine);
temp += (temZnach[i] + "\t\n");
// temp += (temZnach[i]);
document.Content.LoadText(temp);
};
//document.Sections.Add(
// new Section(document,
// new Paragraph(document,
// new Run(document, temp),
// new SpecialCharacter(document, SpecialCharacterType.LineBreak)
// // new Run(document, ""/*"***"*//*"\xFC" + "\xF0" + "\x32"*/) { CharacterFormat = { FontName = "Wingdings", Size = 48 } }
// )));
//// Добавление нового раздела с двумя абзацами, содержащими некоторый текст и символы
//document.Sections.Add(
//new Section(document,
// new Paragraph(document,
// new Run(document, "Это наш первый абзац с символами, добавленными в новую строку."),
// new SpecialCharacter(document, SpecialCharacterType.LineBreak),
// new Run(document, ""/*"***"*//*"\xFC" + "\xF0" + "\x32"*/) { CharacterFormat = { FontName = "Wingdings", Size = 48 } }),
// new Paragraph(document, "Это наш второй абзац."))
//);
// Save Word document to file's path.
document.Save("TestSaveDoc.docx");
document.Save("TestSaveDoc.pdf");
}
catch (Exception ex)
{
Console.WriteLine("Ошибка при работе" + ex);
}
}
static void GetBoxDocPDF(string ebillpath)
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
try
{
DocumentModel doc = new DocumentModel();
CharacterFormat charFormat = doc.DefaultCharacterFormat;
charFormat.Size = 10;
charFormat.FontName = "Courier New";
ParagraphFormat parFormat = doc.DefaultParagraphFormat;
parFormat.SpaceAfter = 0;
parFormat.LineSpacing = 1;
// It seems you want to skip first line with 'clearedtop'.
// So maybe you could just use this instead.
string text = string.Concat(File.ReadLines(ebillpath).Skip(1));
doc.Content.LoadText(text);
Section section = doc.Sections[0];
PageMargins margins = section.PageSetup.PageMargins;
margins.Bottom = 36;
margins.Top = 36;
margins.Right = 36;
margins.Left = 36;
doc.Save(@"test.pdf");
}
catch (Exception ex)
{
Console.WriteLine("Ошибка при работе" + ex);
}
}
static void GetBoxDoc(string ebillpath)
{
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
try
{
using (var sr = new StreamReader (ebillpath))
{
var doc = new DocumentModel();
doc.DefaultCharacterFormat.Size = 10;
doc.DefaultCharacterFormat.FontName = "Courier New";
var section = new Section(doc);
doc.Sections.Add(section);
string line;
var clearedtop = false;
while ((line = sr.ReadLine()) != null)
{
if (string.IsNullOrEmpty(line) && !clearedtop)
{
continue;
}
clearedtop = true;
Paragraph paragraph2 = new Paragraph(doc, new Run(doc, line));
section.Blocks.Add(paragraph2);
}
PageSetup pageSetup = new PageSetup(); // section.PageSetup;
var pm = new PageMargins();
pm.Bottom = 36;
pm.Top = 36;
pm.Right = 36;
pm.Left = 36;
pageSetup.PageMargins = pm;
doc.Save(@"test.pdf");
}
}
catch (Exception ex)
{
Console.WriteLine("Ошибка при работе"+ ex);
}
}
static void GemBoxDocumentTest()
{
string[] temZnach = new string[] { "Gthdjt", "Массив представляет", "мы можем" };
//Лицензии
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// обьект для работы с вордом
DocumentModel document = new DocumentModel();
//настройки шрифта
document.DefaultCharacterFormat.Size = 25;
//производные элементы, имеющие определенный набор свойств,
//используемых для определения страниц
Section section = new Section(document);
//Добавляем текущий обьект в документ
document.Sections.Add(section);
//Представляет собой абзац содержания в документе.
Paragraph paragraph = new Paragraph(document);
//Документ.Секции блоков.
section.Blocks.Add(paragraph);
for (int i = 0; i < temZnach.Length; i++)
{
Run run = new Run(document, temZnach[i]);
// document.Content.LoadText(temZnach[i], new CharacterFormat() { FontName = "Arial" });
paragraph.Inlines.Add(run);
}
// Представляет область текста с общим набором свойств.
// Run run = new Run(document, "Programming language: C++, C# and Java");
//Абзац в строчках.
// paragraph.Inlines.Add(run);
document.Save("Doc1.docx");
}
static void GemBoxDocument0()
{
string[] temZnach = new string[] { "Gthdjt", "Массив представляет", "мы можем" };
try
{
// Бесплатная версия.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Создаем новый докумен
var document = new DocumentModel();
for (int i = 0; i< temZnach.Length; i++ )
{
document.Content.LoadText(temZnach[i], new CharacterFormat() { FontName = "Arial" });
}
// Добавьте в документ обычный текст
//document.Content.LoadText("Внутри все очень просто, но удобно....", new CharacterFormat() { FontName = "Arial" });
// Вставьте текст в формате RTF в начале документа.
var position = document.Content.Start.LoadText(@"{\rtf1\ansi\deff0{\fonttbl{\f0 Arial Black;}}{\colortbl ;\red255\green128\blue64;}\f0\cf1 Это богатый форматированный текст 1.}",
LoadOptions.RtfDefault);
// Вставьте текст в формате HTML после предыдущего текста.
position.LoadText("<p style='font-family:Arial Narrow;color:royalblue;'>Это еще один богатый форматированный текст 2 .</p>",
LoadOptions.HtmlDefault);
// Сохраните документ Word в путь к файлу.
document.Save("Writing.docx");
}
catch (Exception ex)
{
Console.WriteLine("Ошибка при работе \t\n" + ex);
}
}
static void GemBoxDocument1()
{
string[] temZnach = new string[] { "Gthdjt", "Массив представляет", "мы можем" };
// If using Professional version, put your serial key below.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
//Создаем новый пустой документ
DocumentModel document = new DocumentModel();
//секция документа.
Section section = new Section(document);
// Запысываем пустую секцию в документ
document.Sections.Add(section);
//Представляет собой абзац содержания в документе.
Paragraph paragraph = new Paragraph(document);
Paragraph paragraph2 = new Paragraph(document);
// добавляем блок в секцию
section.Blocks.Add(paragraph);
section.Blocks.Add(paragraph2);
// Инициализирует новый экземпляр класса Run с указанным текстом.
Run run = new Run(document, "Hello World!");
Run run2 = new Run(document, temZnach.ToString());
paragraph.Inlines.Add(run);
paragraph.Inlines.Add(run2);
document.Save("TestGemBox.docx");
}
static void GemBoxDocument3()
{
DocumentModel doc = null;
try
{
string CreateDoc = @"c:\1\TestGemBox.docx";
doc = new DocumentModel();
doc.Save(CreateDoc);
// Путь до файла
string destFileName2 = @"c:\1\TestGemBox2.docx";
//// Create a new empty Word file.
//var doc = new DocumentModel();
// Обязательная строка, указываем, что мы используем лимитированную версию
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Загружаем в память наш документ
doc = DocumentModel.Load(CreateDoc);
string[] data = new string[] { "Alex", "Gulynin", "27" };
//Коллекция закладок
BookmarkCollection wBookmarks = doc.Bookmarks;
// ContentRange - это область содержимого в документе
ContentRange wRange;
int i = 0;
// Пробегаем по всем закладкам в документе
foreach (Bookmark mark in doc.Bookmarks)
{
// Получаем содержимое закладки
wRange = mark.GetContent(false);
// Загружаем туда нужный текст
wRange.LoadText(data[i].ToString());
i++;
}
// Сохраняем изменения в нашем документе
doc.Save(destFileName2);
doc = null;
}
catch (Exception ex)
{
doc = null;
Console.WriteLine("Во время выполнения программы произошла ошибка! Текст ошибки: {0}", ex.Message);
Console.ReadLine();
}
}
static void GemBoxDocument2()
{
// Set license key to use GemBox.Document in Free mode.
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
// Create a new empty Word file.
var doc = new DocumentModel();
// Add a new document content.
doc.Sections.Add(new Section(doc, new Paragraph(doc, "Hello world!")));
// Save to DOCX and PDF files.
doc.Save("Document.docx");
//doc.Save("Document.pdf");
}
#region Тестовые методы
//static void Init()
//{/// <summary>
// /// Основной объект Application, который является предком всех остальных объектов
// /// </summary>
// Microsoft.Office.Interop.Word.Application winword = new Microsoft.Office.Interop.Word.Application();
// object missing = System.Reflection.Missing.Value;
// /// <summary>
// /// Чтобы открыть существующий документ или создать новый, необходимо создать новый объект Document.
// /// </summary>
// Microsoft.Office.Interop.Word.Document document =
// winword.Documents.Add(ref missing, ref missing, ref missing, ref missing);
// try
// {
// /// <summary>
// /// Добавление текста в документ
// /// </summary>
// //Добавление текста в документ
// document.Content.SetRange(0, 0);
// document.Content.Text = "Реквизиты клиента." + Environment.NewLine;
// /// <summary>
// /// Добавление колонтитулов
// /// </summary>
// //Добавление верхнего колонтитула
// foreach (Microsoft.Office.Interop.Word.Section section in document.Sections)
// {
// Microsoft.Office.Interop.Word.Range headerRange =
// section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
// headerRange.Fields.Add(headerRange, Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage);
// headerRange.ParagraphFormat.Alignment =
// Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
// headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
// headerRange.Font.Size = 10;
// headerRange.Text = "Сеть центров по выдаче денежных займов размещает условия предоставления денежных средств частным лицам." +
// "Имеется калькулятор расчёта кредитования с расчетом процентов и конечной общей стоимости"
// + Environment.NewLine + "https://kassaone.ru";
// }
// //Добавление нижнего колонтитула
// foreach (Microsoft.Office.Interop.Word.Section wordSection in document.Sections)
// {
// Microsoft.Office.Interop.Word.Range footerRange =
// wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
// footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
// footerRange.Font.Size = 10;
// footerRange.ParagraphFormat.Alignment =
// Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
// footerRange.Text = "Ваш Банк" + Environment.NewLine + "https://kassaone.ru";
// }
// /// <summary>
// /// применить к тексту определенный стиль.
// /// </summary>
// //Добавление текста со стилем Заголовок 1
// Microsoft.Office.Interop.Word.Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
// object styleHeading1 = "123";
// para1.Range.set_Style(styleHeading1);
// para1.Range.Text = "Ваша заявка будет расмотрена. ";
// para1.Range.InsertParagraphAfter();
// //Сохранение документа
// //var temn = Assembly.GetExecutingAssembly().Location;
// var path = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
// string s = Environment.CurrentDirectory;
// // object filename = @"C:temp1.docx";
// object filename = path;
// document.SaveAs(ref filename);
// // document.SaveAs2(true);
// //Закрытие текущего документа
// document.Close(ref missing, ref missing, ref missing);
// document = null;
// //Закрытие приложения Word
// winword.Quit(ref missing, ref missing, ref missing);
// winword = null;
// winword.Quit();
// Console.WriteLine($"Вроде документ сформировался!!!{ Environment.NewLine}");
// }
// catch (Exception ex)
// {
// Console.WriteLine($"Произошла ошибка!!!{ Environment.NewLine}. {ex}");
// }
// finally
// {
// //winword.Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
// //System.Runtime.InteropServices.Marshal.ReleaseComObject(winword);
// //Закрытие текущего документа
// document.Close(ref missing, ref missing, ref missing);
// document = null;
// // Закрытие приложения Word
// winword.Quit(ref missing, ref missing, ref missing);
// winword = null;
// }
//}
///// <summary>
///// Запись(обновление) в уже созданный документ Word с помощью Microsoft.Office.Interop.Word
///// </summary>
//static void TestDoc()
//{
// Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
// try
// {
// Microsoft.Office.Interop.Word.Document doc = ap.Documents.Open(@"c:MyWord.docx", ReadOnly: false, Visible: false);
// doc.Activate();
// Microsoft.Office.Interop.Word.Selection sel = ap.Selection;
// if (sel != null)
// {
// switch (sel.Type)
// {
// case Microsoft.Office.Interop.Word.WdSelectionType.wdSelectionIP:
// sel.TypeText(DateTime.Now.ToString());
// sel.TypeParagraph();
// sel.TypeText("Microsoft Word");
// sel.TypeParagraph();
// break;
// default:
// Console.WriteLine("Тип выбора не обрабатывается; запись не выполняется");
// break;
// }
// // Remove all meta data.
// doc.RemoveDocumentInformation(Microsoft.Office.Interop.Word.WdRemoveDocInfoType.wdRDIAll);
// ap.Documents.Save(NoPrompt: true, OriginalFormat: true);
// }
// else
// {
// Console.WriteLine("Можете приобрести выбор...не пишу, чтобы сделать документ..");
// }
// ap.Documents.Close(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
// }
// catch (Exception ex)
// {
// Console.WriteLine("Исключение: " + ex.Message); // Could be that the document is already open (/) or Word is in Memory(?)
// }
// finally
// {
// ((Microsoft.Office.Interop.Word._Application)ap).Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);
// System.Runtime.InteropServices.Marshal.ReleaseComObject(ap);
// }
//}
//static void TestOpenXML()
//{
// try
// {
// // Получаем массив байтов из нашего файла
// byte[] textByteArray = File.ReadAllBytes(@"C:\MyWord.docx");
// // Массив данных
// string[] data = new string[3] { "27", "Гулынин", "Алексей" };
// // Начинаем работу с потоком
// using (MemoryStream stream = new MemoryStream())
// {
// // Записываем в поток наш word-файл
// stream.Write(textByteArray, 0, textByteArray.Length);
// // Открываем документ из потока с возможностью редактирования
// using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
// {
// // Ищем все закладки в документе
// var bookMarks = FindBookmarks(doc.MainDocumentPart.Document);
// int i = 0;
// foreach (var end in bookMarks)
// {
// // В документе встречаются какие-то служебные закладки
// // Таким способом отфильтровываем всё ненужное
// // end.Key содержит имена наших закладок
// if (end.Key != "Name" && end.Key != "Age" && end.Key != "Surname") continue;
// // Создаём текстовый элемент
// var textElement = new Text(data[i].ToString());
// // Далее данный текст добавляем в закладку
// var runElement = new Run(textElement);
// end.Value.InsertAfterSelf(runElement);
// i++;
// }
// }
// // Записываем всё в наш файл
// File.WriteAllBytes(@"D:\Test.docx", stream.ToArray());
// }
// }
// catch (Exception ex)
// {
// Console.WriteLine("Во время выполнения программы произошла ошибка! Текст ошибки: {0}", ex.Message);
// Console.ReadLine();
// }
//}
// Получаем все закладки в документе
// bStartWithNoEnds - словарь, который будет содержать только начало закладок,
// чтобы потом по ним находить соответствующие им концы закладок
// documentPart - элемент Word-документа
// outs - конечный результат
//private static Dictionary<string, BookmarkEnd> FindBookmarks(OpenXmlElement documentPart, Dictionary<string, BookmarkEnd> outs = null, Dictionary<string, string> bStartWithNoEnds = null)
//{
// if (outs == null) { outs = new Dictionary<string, BookmarkEnd>(); }
// if (bStartWithNoEnds == null) { bStartWithNoEnds = new Dictionary<string, string>(); }
// // Проходимся по всем элементам на странице Word-документа
// foreach (var docElement in documentPart.Elements())
// {
// // BookmarkStart определяет начало закладки в рамках документа
// // маркер начала связан с маркером конца закладки
// if (docElement is BookmarkStart)
// {
// var bookmarkStart = docElement as BookmarkStart;
// // Записываем id и имя закладки
// bStartWithNoEnds.Add(bookmarkStart.Id, bookmarkStart.Name);
// }
// // BookmarkEnd определяет конец закладки в рамках документа
// if (docElement is BookmarkEnd)
// {
// var bookmarkEnd = docElement as BookmarkEnd;
// foreach (var startName in bStartWithNoEnds)
// {
// // startName.Key как раз и содержит id закладки
// // здесь проверяем, что есть связь между началом и концом закладки
// if (bookmarkEnd.Id == startName.Key)
// // В конечный массив добавляем то, что нам и нужно получить
// outs.Add(startName.Value, bookmarkEnd);
// }
// }
// // Рекурсивно вызываем данный метод, чтобы пройтись по всем элементам
// // word-документа
// FindBookmarks(docElement, outs, bStartWithNoEnds);
// }
// return outs;
//}
//static void Init33()
//{
// string filePath = @"C:\MyWord1234.docx";
// DocX doc = DocX.Create(filePath);
// Paragraph p1 = template.InsertParagraph();
// p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word.");
// p1.AppendLine("Here is example with question mark?");
// p1.AppendLine();
// p1.AppendLine("Can you help me figure it out?");
// p1.AppendLine();
#endregion
}
}