-
Notifications
You must be signed in to change notification settings - Fork 6
/
OpenXML.Words.cs
1917 lines (1632 loc) · 86.7 KB
/
OpenXML.Words.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using DF = DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using WP = DocumentFormat.OpenXml.Wordprocessing;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using DWG = DocumentFormat.OpenXml.Office2010.Word.DrawingGroup;
using DWS = DocumentFormat.OpenXml.Office2010.Word.DrawingShape;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;
using FF = FileFormat.Words.IElements;
using OWD = OpenXML.Words.Data;
using OT = OpenXML.Templates;
using FileFormat.Words;
namespace OpenXML.Words
{
internal class OwDocument
{
private WordprocessingDocument _pkgDocument;
private WP.Body _wpBody;
private MemoryStream _ms;
private MainDocumentPart _mainPart;
private List<int> _IDs;
private NumberingDefinitionsPart _numberingPart;
private readonly object _lockObject = new object();
private OwDocument()
{
lock (_lockObject)
{
try
{
_ms = new MemoryStream();
_pkgDocument = WordprocessingDocument.Create(_ms, DF.WordprocessingDocumentType.Document, true);
_mainPart = _pkgDocument.AddMainDocumentPart();
_mainPart.Document = new WP.Document();
var tmp = new OT.DefaultTemplate();
tmp.CreateMainDocumentPart(_mainPart);
CreateProperties(_pkgDocument);
_numberingPart = _mainPart.NumberingDefinitionsPart;
if (_numberingPart != null)
{
_IDs = new List<int>();
foreach (var abstractNum in _numberingPart.Numbering.Elements<WP.AbstractNum>())
{
_IDs.Add(abstractNum.AbstractNumberId);
}
}
}
catch (Exception ex)
{
var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Initialize OOXML Element(s)");
throw new FileFormat.Words.FileFormatException(errorMessage, ex);
}
}
}
private OwDocument(WordprocessingDocument pkg)
{
lock (_lockObject)
{
try
{
//_ms = new MemoryStream();
_pkgDocument = pkg;
_mainPart = pkg.MainDocumentPart;
//_mainPart.Document = new WP.Document();
//var tmp = new OT.DefaultTemplate();
//tmp.CreateMainDocumentPart(_mainPart);
//CreateProperties(_pkgDocument);
_numberingPart = _mainPart.NumberingDefinitionsPart;
if (_numberingPart != null)
{
_IDs = new List<int>();
foreach (var abstractNum in _numberingPart.Numbering.Elements<WP.AbstractNum>())
{
_IDs.Add(abstractNum.AbstractNumberId);
}
}
}
catch (Exception ex)
{
var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Initialize OOXML Element(s)");
throw new FileFormat.Words.FileFormatException(errorMessage, ex);
}
}
}
#region Create Core Properties for OpenXML Word Document
internal void CreateProperties(WordprocessingDocument pkgDocument)
{
var corePart = pkgDocument.CoreFilePropertiesPart;
if (corePart != null)
{
pkgDocument.DeletePart(corePart);
}
var customPart = pkgDocument.CustomFilePropertiesPart;
if (customPart != null)
{
pkgDocument.DeletePart(customPart);
}
var coreProperties = new OT.CoreProperties();
var dictCoreProp = new Dictionary<string, string>
{
["Title"] = "Newly Created OWDocument",
["Subject"] = "WordProcessing OWDocument Generation",
["Keywords"] = "DOCX",
["Description"] = "A WordProcessing OWDocument Created from Scratch.",
["Creator"] = "FileFormat.Words"
};
var currentTime = System.DateTime.UtcNow;
dictCoreProp["Created"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
dictCoreProp["Modified"] = currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ");
coreProperties.CreateCoreFilePropertiesPart(pkgDocument.AddCoreFilePropertiesPart(), dictCoreProp);
var customProperties = new OT.CustomProperties();
customProperties.CreateExtendedFilePropertiesPart(pkgDocument.AddExtendedFilePropertiesPart());
}
#endregion
public static OwDocument CreateInstance()
{
return new OwDocument();
}
public static OwDocument CreateInstance(WordprocessingDocument pkg)
{
return new OwDocument(pkg);
}
#region Create OpenXML Word Document Contents Based on FileFormat.Words.IElements
#region Main Method
internal void CreateDocument(List<FF.IElement> lst)
{
try
{
_wpBody = _mainPart.Document.Body;
if (_wpBody == null)
throw new FileFormat.Words.FileFormatException("Package or Document or Body is null", new NullReferenceException());
var sectionProperties = _wpBody.Elements<WP.SectionProperties>().FirstOrDefault();
foreach (var element in lst)
{
switch (element)
{
case FF.Paragraph ffP:
{
var para = CreateParagraph(ffP);
_wpBody.InsertBefore(para, sectionProperties);
break;
}
case FF.Image ffImg:
{
var para = CreateImage(ffImg, _mainPart);
_wpBody.InsertBefore(para, sectionProperties);
break;
}
case FF.Shape ffShape:
{
var para = CreateShape(ffShape);
_wpBody.InsertBefore(para, sectionProperties);
break;
}
case FF.GroupShape ffGroupShape:
{
var para = CreateGroupShape(ffGroupShape);
_wpBody.InsertBefore(para, sectionProperties);
break;
}
case FF.Table ffTable:
{
var table = CreateTable(ffTable);
_wpBody.InsertBefore(table, sectionProperties);
break;
}
}
}
}
catch (Exception ex)
{
var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Initialize OOXML Element(s)");
throw new FileFormat.Words.FileFormatException(errorMessage, ex);
}
}
#endregion
#region Create OpenXML Paragraph
internal WP.Paragraph CreateParagraph(FF.Paragraph ffP)
{
lock (_lockObject)
{
try
{
var wpParagraph = new WP.Paragraph();
if (ffP.Style != null)
{
var paragraphProperties = new WP.ParagraphProperties();
var paragraphStyleId = new WP.ParagraphStyleId { Val = ffP.Style };
paragraphProperties.Append(paragraphStyleId);
#region Create List Paragraph
if (ffP.Style == "ListParagraph")
{
// Check if NumberingId already exists
var isExist = false;
if (_IDs != null)
{
foreach (var id in _IDs)
{
if (id == ffP.NumberingId)
{
isExist = true;
var numbering = _numberingPart.Numbering;
var abstractNum = numbering.Elements<WP.AbstractNum>().FirstOrDefault(an => an.AbstractNumberId == ffP.NumberingId);
if (abstractNum != null)
{
var level = abstractNum.Elements<WP.Level>().FirstOrDefault(l => l.LevelIndex == ffP.NumberingLevel - 1);
if (level != null)
{
if (ffP.IsAlphabeticNumber)
{
level.NumberingFormat.Val = WP.NumberFormatValues.LowerLetter;
level.LevelText.Val = string.Format("%{0}.", (int)ffP.NumberingLevel);
}
else if (ffP.IsRoman)
{
level.NumberingFormat.Val = WP.NumberFormatValues.LowerRoman;
level.LevelText.Val = string.Format("%{0}.", (int)ffP.NumberingLevel);
}
else if (ffP.IsBullet)
{
level.NumberingFormat.Val = WP.NumberFormatValues.Bullet;
level.LevelText.Val = "o";
}
if (ffP.IsNumbered)
{
level.NumberingFormat.Val = WP.NumberFormatValues.Decimal;
level.LevelText.Val = string.Format("%{0}.", string.Join(".%", Enumerable.Range(1, (int)ffP.NumberingLevel)));
}
numbering.Save();
}
}
}
}
}
if (!isExist)
{
if (ffP.NumberingId != null)
{
if (ffP.NumberingLevel == null)
ffP.NumberingLevel = 1;
if (ffP.IsAlphabeticNumber == false && ffP.IsBullet == false &&
ffP.IsNumbered == false && ffP.IsRoman == false)
ffP.IsNumbered = true;
var abstractNum = new WP.AbstractNum() { AbstractNumberId = ffP.NumberingId };
abstractNum.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
var multiLevelType = new WP.MultiLevelType() { Val = WP.MultiLevelValues.Multilevel };
multiLevelType.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
abstractNum.Append(multiLevelType);
var level = new WP.Level() { LevelIndex = 0 };
for (var i = 1; i <= 9; i++)
{
level = new WP.Level() { LevelIndex = i - 1 };
level.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
var numberingFormat = new WP.NumberingFormat();
var levelText = new WP.LevelText();
if (ffP.IsNumbered)
{
numberingFormat.Val = WP.NumberFormatValues.Decimal;
levelText.Val = string.Format("%{0}.", string.Join(".%", Enumerable.Range(1, i)));
}
else if (ffP.IsAlphabeticNumber)
{
numberingFormat.Val = WP.NumberFormatValues.LowerLetter;
levelText.Val = string.Format("%{0}.", i);
}
else if (ffP.IsRoman)
{
numberingFormat.Val = WP.NumberFormatValues.LowerRoman;
levelText.Val = string.Format("%{0}.", i);
}
else if (ffP.IsBullet)
{
numberingFormat.Val = WP.NumberFormatValues.Bullet;
levelText.Val = "o";
}
var previousParagraphProperties = new WP.PreviousParagraphProperties();
var indentation = new WP.Indentation() { Left = (i * 720).ToString(), Hanging = "360" };
previousParagraphProperties.Append(indentation);
level.Append(new WP.StartNumberingValue() { Val = 1 });
level.Append(numberingFormat);
level.Append(levelText);
level.Append(new WP.LevelJustification() { Val = WP.LevelJustificationValues.Left });
level.Append(previousParagraphProperties);
abstractNum.Append(level);
}
var numberingInstance = new WP.NumberingInstance() { NumberID = ffP.NumberingId };
var abstractNumId = new WP.AbstractNumId() { Val = ffP.NumberingId };
numberingInstance.Append(abstractNumId);
_numberingPart.Numbering.Append(abstractNum);
_numberingPart.Numbering.Append(numberingInstance);
_IDs.Add((int)ffP.NumberingId);
}
}
var numberingProperties = new WP.NumberingProperties();
var numberingLevelReference = new WP.NumberingLevelReference() { Val = ffP.NumberingLevel - 1 };
var numberingId = new WP.NumberingId() { Val = ffP.NumberingId };
numberingProperties.Append(numberingLevelReference);
numberingProperties.Append(numberingId);
paragraphProperties.Append(numberingProperties);
}
#endregion
// Create Borders
if (ffP.ParagraphBorder.Size > 0)
{
WP.ParagraphBorders paragraphBorders = new WP.ParagraphBorders();
WP.TopBorder topBorder = new WP.TopBorder()
{ Val = CreateBorder(ffP.ParagraphBorder.Width),
Color = ffP.ParagraphBorder.Color,
Size = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size,
Space = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size
};
WP.LeftBorder leftBorder = new WP.LeftBorder()
{
Val = CreateBorder(ffP.ParagraphBorder.Width),
Color = ffP.ParagraphBorder.Color,
Size = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size,
Space = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size
};
WP.BottomBorder bottomBorder = new WP.BottomBorder()
{
Val = CreateBorder(ffP.ParagraphBorder.Width),
Color = ffP.ParagraphBorder.Color,
Size = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size,
Space = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size
};
WP.RightBorder rightBorder = new WP.RightBorder()
{
Val = CreateBorder(ffP.ParagraphBorder.Width),
Color = ffP.ParagraphBorder.Color,
Size = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size,
Space = (DF.UInt32Value)(uint)ffP.ParagraphBorder.Size
};
paragraphBorders.Append(topBorder);
paragraphBorders.Append(leftBorder);
paragraphBorders.Append(bottomBorder);
paragraphBorders.Append(rightBorder);
paragraphProperties.Append(paragraphBorders);
}
// Create Justification
WP.JustificationValues justificationValue = CreateJustification(ffP.Alignment);
paragraphProperties.Append(new WP.Justification { Val = justificationValue });
// Create Indentation
CreateIndentation(paragraphProperties, ffP.Indentation);
wpParagraph.Append(paragraphProperties);
}
foreach (var ffR in ffP.Runs)
{
var wpRun = new WP.Run();
var runProperties = new WP.RunProperties();
if (ffR.FontFamily != null)
{
var runFont = new WP.RunFonts
{
Ascii = ffR.FontFamily,
HighAnsi = ffR.FontFamily,
ComplexScript = ffR.FontFamily,
EastAsia = ffR.FontFamily
};
runProperties.Append(runFont);
}
if (ffR.Color != null)
{
var color = new WP.Color { Val = ffR.Color };
runProperties.Append(color);
}
if (ffR.FontSize > 0)
{
var fontSize = new WP.FontSize { Val = (ffR.FontSize * 2).ToString() };
runProperties.Append(fontSize);
}
if (ffR.Bold)
{
runProperties.Append(new WP.Bold() { Val = new DF.OnOffValue(true) });
}
if (ffR.Italic)
{
runProperties.Append(new WP.Italic());
}
if (ffR.Underline)
{
var underline = new WP.Underline { Val = WP.UnderlineValues.Single };
runProperties.Append(underline);
}
var text = new WP.Text(ffR.Text);
wpRun.Append(runProperties, text);
wpParagraph.AppendChild(wpRun);
}
return wpParagraph;
}
catch (Exception ex)
{
var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Create Paragraph");
throw new FileFormat.Words.FileFormatException(errorMessage, ex);
}
}
}
private WP.JustificationValues CreateJustification(FF.ParagraphAlignment alignment)
{
switch (alignment)
{
case FF.ParagraphAlignment.Left:
return WP.JustificationValues.Left;
case FF.ParagraphAlignment.Center:
return WP.JustificationValues.Center;
case FF.ParagraphAlignment.Right:
return WP.JustificationValues.Right;
case FF.ParagraphAlignment.Justify:
return WP.JustificationValues.Both;
default:
return WP.JustificationValues.Left;
}
}
private WP.BorderValues CreateBorder(FF.BorderWidth borderWidth)
{
switch (borderWidth)
{
case FF.BorderWidth.Single:
return WP.BorderValues.Single;
case FF.BorderWidth.Double:
return WP.BorderValues.Double;
case FF.BorderWidth.Dotted:
return WP.BorderValues.Dotted;
case FF.BorderWidth.DotDash:
return WP.BorderValues.DotDash;
default:
return WP.BorderValues.Single;
}
}
private void CreateIndentation(WP.ParagraphProperties paragraphProperties, FF.Indentation ffIndentation)
{
var indentation = new WP.Indentation();
if (ffIndentation.Left > 0)
{
indentation.Left = (ffIndentation.Left * 1440).ToString();
}
if (ffIndentation.Right > 0)
{
indentation.Right = (ffIndentation.Right * 1440).ToString();
}
if (ffIndentation.FirstLine > 0)
{
indentation.FirstLine = (ffIndentation.FirstLine * 1440).ToString();
}
if (ffIndentation.Hanging > 0)
{
indentation.Hanging = (ffIndentation.Hanging * 1440).ToString();
}
paragraphProperties.Append(indentation);
}
#endregion
#region Create OpenXML Table
internal WP.Table CreateTable(FF.Table ffTable)
{
lock (_lockObject)
{
try
{
var rows = ffTable.Rows.Count;
var cols = ffTable.Rows[0].Cells.Count;
var wpTable = new WP.Table(
new WP.TableProperties(
new WP.TableStyle() { Val = ffTable.Style } // Specify the TableStyle ID you want to apply
)
);
var tableGrid = new WP.TableGrid();
for (var i = 0; i < cols; i++)
{
if (ffTable.Column.Width > 0)
tableGrid.Append(new WP.GridColumn { Width = ffTable.Column.Width.ToString() });
else
tableGrid.Append(new WP.GridColumn());
}
wpTable.Append(tableGrid);
for (var i = 0; i < rows; i++)
{
var wpRow = new WP.TableRow();
for (var j = 0; j < cols; j++)
{
var wpCell = new WP.TableCell();
var ffCell = ffTable.Rows[i].Cells[j];
foreach (var ffPara in ffCell.Paragraphs)
{
wpCell.Append(CreateParagraph(ffPara));
}
wpRow.Append(wpCell);
}
wpTable.Append(wpRow);
}
return wpTable;
}
catch (Exception ex)
{
var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Create Table");
throw new FileFormat.Words.FileFormatException(errorMessage, ex);
}
}
}
#endregion
#region Create OpenXML Image
internal WP.Paragraph CreateImage(FF.Image ffImg, MainDocumentPart mainPart)
{
lock (_lockObject)
{
try
{
var imageBytes = ffImg.ImageData;
var imagePart = mainPart.AddImagePart(ImagePartType.Png);
using (var partStream = imagePart.GetStream())
{
partStream.Write(imageBytes, 0, imageBytes.Length); // Write the image bytes to the partStream
}
float dpi = 96; // The DPI of the image (you may need to adjust this value)
//int widthInPixels;
//int heightInPixels;
const int maxDimension = 500;
var widthInPixels = (ffImg.Width > 0 && ffImg.Width <= maxDimension) ? ffImg.Width : maxDimension;
var heightInPixels = (ffImg.Height > 0 && ffImg.Height <= maxDimension) ? ffImg.Height : maxDimension;
var widthInInches = widthInPixels / dpi;
var heightInInches = heightInPixels / dpi;
var widthInEmu = (long)(widthInInches * 914400);
var heightInEmu = (long)(heightInInches * 914400);
//long widthInEMU = (long)widthInInches;
//long heightInEMU = (long)heightInInches;
// Define the reference of the image.
var element =
new WP.Drawing(
new DW.Inline(
//new DW.Extent() { Cx = ffIMG.Width*9525 , Cy = ffIMG.Height*9525 },
new DW.Extent() { Cx = widthInEmu, Cy = heightInEmu },
new DW.EffectExtent()
{
LeftEdge = 0L,
TopEdge = 0L,
RightEdge = 0L,
BottomEdge = 0L
},
new DW.DocProperties()
{
Id = (DF.UInt32Value)1U,
Name = "Picture 1"
},
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks() { NoChangeAspect = true }),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
new PIC.NonVisualPictureProperties(
new PIC.NonVisualDrawingProperties()
{
Id = (DF.UInt32Value)0U,
Name = "New Bitmap Image.jpg"
},
new PIC.NonVisualPictureDrawingProperties()),
new PIC.BlipFill(
new A.Blip(
new A.BlipExtensionList(
new A.BlipExtension()
{
Uri =
"{28A0092B-C50C-407E-A947-70E740481C1C}"
})
)
{
Embed = mainPart.GetIdOfPart(imagePart),
CompressionState =
A.BlipCompressionValues.Print
},
new A.Stretch(
new A.FillRectangle())),
new PIC.ShapeProperties(
new A.Transform2D(
new A.Offset() { X = 0L, Y = 0L },
//new A.Extents() { Cx = ffIMG.Width*9525, Cy = ffIMG.Height*9525 }
new A.Extents() { Cx = widthInEmu, Cy = heightInEmu }),
new A.PresetGeometry(
new A.AdjustValueList()
)
{ Preset = A.ShapeTypeValues.Rectangle }))
)
{ Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
)
{
DistanceFromTop = (DF.UInt32Value)0U,
DistanceFromBottom = (DF.UInt32Value)0U,
DistanceFromLeft = (DF.UInt32Value)0U,
DistanceFromRight = (DF.UInt32Value)0U,
EditId = "50D07946"
});
return new WP.Paragraph(new WP.Run(element));
}
catch (Exception ex)
{
var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Create Image");
throw new FileFormat.Words.FileFormatException(errorMessage, ex);
}
}
}
#endregion
#region Create OpenXML Shape
internal WP.Paragraph CreateShape(FF.Shape shape)
{
lock (_lockObject)
{
try
{
var paragraph = new WP.Paragraph();
var run = new WP.Run();
var runProperties = new WP.RunProperties();
var noProof = new WP.NoProof();
runProperties.Append(noProof);
var alternateContent = new DF.AlternateContent();
alternateContent.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
var alternateContentChoice = new DF.AlternateContentChoice() { Requires = "wps" };
alternateContentChoice.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
var drawing = new WP.Drawing();
drawing.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
var inline = new DW.Inline()
{ DistanceFromTop = (DF.UInt32Value)0U, DistanceFromBottom = (DF.UInt32Value)0U, DistanceFromLeft = (DF.UInt32Value)0U, DistanceFromRight = (DF.UInt32Value)0U, AnchorId = "27EE2959", EditId = "551435BE" };
inline.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
inline.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
var extent = new DW.Extent() { Cx = shape.Width * 9525, Cy = shape.Height * 9525 };
extent.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
var effectExtent = new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 13970L, BottomEdge = 13970L };
effectExtent.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
var docProperties = new DW.DocProperties() { Id = (DF.UInt32Value)1609145151U, Name = "Oval 1" };
docProperties.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
var nonVisualGraphicFrameDrawingProperties = new DW.NonVisualGraphicFrameDrawingProperties();
nonVisualGraphicFrameDrawingProperties.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
var graphic = new A.Graphic();
graphic.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
var graphicData = new A.GraphicData() { Uri = "http://schemas.microsoft.com/office/word/2010/wordprocessingShape" };
var wordprocessingShape = new DWS.WordprocessingShape();
wordprocessingShape.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
var nonVisualDrawingShapeProperties = new DWS.NonVisualDrawingShapeProperties();
var shapeProperties = new DWS.ShapeProperties();
var transform2D = new A.Transform2D();
var offset = new A.Offset() { X = shape.X * 9525, Y = shape.Y * 9525 };
var extents = new A.Extents() { Cx = shape.Width * 9525, Cy = shape.Height * 9525 };
transform2D.Append(offset);
transform2D.Append(extents);
var presetGeometry = new A.PresetGeometry() { Preset = CreateShapeType(shape.Type) }; //A.ShapeTypeValues.Ellipse };
var adjustValueList = new A.AdjustValueList();
presetGeometry.Append(adjustValueList);
var outline = new A.Outline();
shapeProperties.Append(transform2D);
shapeProperties.Append(presetGeometry);
shapeProperties.Append(outline);
var shapeStyle = new DWS.ShapeStyle();
var lineReference = new A.LineReference() { Index = (DF.UInt32Value)2U };
var schemeColor = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 };
var shade = new A.Shade() { Val = 50000 };
schemeColor.Append(shade);
lineReference.Append(schemeColor);
var fillReference = new A.FillReference() { Index = (DF.UInt32Value)1U };
schemeColor = new A.SchemeColor() { Val = A.SchemeColorValues.Accent1 };
fillReference.Append(schemeColor);
var effectReference = new A.EffectReference() { Index = (DF.UInt32Value)0U };
var rgbColorModelPercentage = new A.RgbColorModelPercentage() { RedPortion = 0, GreenPortion = 0, BluePortion = 0 };
effectReference.Append(rgbColorModelPercentage);
var fontReference = new A.FontReference() { Index = A.FontCollectionIndexValues.Minor };
schemeColor = new A.SchemeColor() { Val = A.SchemeColorValues.Light1 };
fontReference.Append(schemeColor);
shapeStyle.Append(lineReference);
shapeStyle.Append(fillReference);
shapeStyle.Append(effectReference);
shapeStyle.Append(fontReference);
var textBodyProperties = new DWS.TextBodyProperties() { Anchor = A.TextAnchoringTypeValues.Center };
wordprocessingShape.Append(nonVisualDrawingShapeProperties);
wordprocessingShape.Append(shapeProperties);
wordprocessingShape.Append(shapeStyle);
wordprocessingShape.Append(textBodyProperties);
graphicData.Append(wordprocessingShape);
graphic.Append(graphicData);
inline.Append(extent);
inline.Append(effectExtent);
inline.Append(docProperties);
inline.Append(nonVisualGraphicFrameDrawingProperties);
inline.Append(graphic);
drawing.Append(inline);
alternateContentChoice.Append(drawing);
var alternateContentFallback = new DF.AlternateContentFallback();
alternateContentFallback.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
alternateContent.Append(alternateContentChoice);
alternateContent.Append(alternateContentFallback);
run.Append(runProperties);
run.Append(alternateContent);
paragraph.Append(run);
return paragraph;
}
catch (Exception ex)
{
var errorMessage = OWD.OoxmlDocData.ConstructMessage(ex, "Create Shape");
throw new FileFormat.Words.FileFormatException(errorMessage, ex);
}
}
}
private A.ShapeTypeValues CreateShapeType(FF.ShapeType shapeType)
{
switch (shapeType)
{
case FF.ShapeType.Rectangle:
return A.ShapeTypeValues.Rectangle;
case FF.ShapeType.Triangle:
return A.ShapeTypeValues.Triangle;
case FF.ShapeType.Ellipse:
return A.ShapeTypeValues.Ellipse;
case FF.ShapeType.Diamond:
return A.ShapeTypeValues.Diamond;
case FF.ShapeType.Hexagone:
return A.ShapeTypeValues.Hexagon;
default:
return A.ShapeTypeValues.Ellipse;
}
}
#region "Create Group Shape with connector"
internal WP.Paragraph CreateGroupShape(FF.GroupShape groupShape)
{
lock (_lockObject)
{
try
{
if (groupShape.Shape2.X < (groupShape.Shape1.X+ groupShape.Shape1.Width))
throw new FileFormat.Words.FileFormatException("Invalid shape dimensions",
new ArgumentException());
var paragraph = new WP.Paragraph();
paragraph.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordml");
var run = new WP.Run();
var runProperties = new WP.RunProperties();
var noProof = new WP.NoProof();
runProperties.Append(noProof);
var alternateContent = new DF.AlternateContent();
alternateContent.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
var alternateContentChoice = new DF.AlternateContentChoice() { Requires = "wpg" };
alternateContentChoice.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
var drawing = new WP.Drawing();
drawing.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
var inline = new DW.Inline() { DistanceFromTop = (DF.UInt32Value)0U, DistanceFromBottom = (DF.UInt32Value)0U, DistanceFromLeft = (DF.UInt32Value)0U, DistanceFromRight = (DF.UInt32Value)0U, AnchorId = "24C249F3", EditId = "163BC827" };
inline.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
inline.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
var extent = new DW.Extent() { Cx = 3778250L, Cy = 622300L };
extent.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
var effectExtent = new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 12700L, BottomEdge = 25400L };
effectExtent.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
var docProperties = new DW.DocProperties() { Id = (DF.UInt32Value)122768519U, Name = "Group-" + groupShape.ElementId.ToString() };
docProperties.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
var nonVisualGraphicFrameDrawingProperties = new DW.NonVisualGraphicFrameDrawingProperties();
nonVisualGraphicFrameDrawingProperties.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
var graphic = new A.Graphic();
graphic.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
A.GraphicData graphicData = new A.GraphicData() { Uri = "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" };
var wordprocessingGroup = new DWG.WordprocessingGroup();
wordprocessingGroup.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
var nonVisualGroupDrawingShapeProperties = new DWG.NonVisualGroupDrawingShapeProperties();
var shape1 = groupShape.Shape1;
var shape2 = groupShape.Shape2;
var groupShapeProperties = new DWG.GroupShapeProperties();
var transformGroup = new A.TransformGroup();
/****** Group Offsets ******/
// var offset = new A.Offset() { X = 0L, Y = 0L };
// Group.X=shape1.x, Group.Y=shape1.y
var groupX = shape1.X * 9525;
var groupY = shape1.Y * 9525;
var groupOffset = new A.Offset()
{
X = groupX,
Y = groupY
};
/****** Group Extents ******/
// var extents = new A.Extents() { Cx = 3778250L, Cy = 622300L };
// Group.Width=278(shape2.X)-0(shape1.X)+118(shape2.Width)
var groupCx = (shape2.X - shape1.X + shape2.Width) * 9525;
// Group.Height=Shape1.Height
var groupCy = shape1.Height * 9525;
var groupExtents = new A.Extents() { Cx = groupCx, Cy = groupCy };
/****** Child Offset & Extents ******/
//var childOffset = new A.ChildOffset() { X=0L, Y=0L};
// Same as group.X and group.Y
var childOffset = new A.ChildOffset()
{
X = groupX,
Y = groupY
};
//var childExtents = new A.ChildExtents() { Cx = 3778250L, Cy = 622300L };
var childExtents = new A.ChildExtents()
{
Cx = groupCx,
Cy = groupCy
};
transformGroup.Append(groupOffset);
transformGroup.Append(groupExtents);
transformGroup.Append(childOffset);
transformGroup.Append(childExtents);
groupShapeProperties.Append(transformGroup);
/******************* shapes ****************/
var wordprocessingShape01 = CreatePartialShape(
shape1.ElementId, shape1.X, shape1.Y,
shape1.Width, shape1.Height, CreateShapeType(shape1.Type)
);
var wordprocessingShape02 = CreatePartialShape(
shape2.ElementId, shape2.X, shape2.Y,
shape2.Width, shape2.Height, CreateShapeType(shape2.Type)
);
/**************** connector *****************/
// Connector
var wordprocessingShape = new DWS.WordprocessingShape();
wordprocessingShape.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
var nonVisualDrawingProperties = new DWS.NonVisualDrawingProperties()
{
Id = (DF.UInt32Value)161453463U,
Name = "Connector: Elbow 161453463"
};
var nonVisualConnectorProperties = new DWS.NonVisualConnectorProperties();
//A.StartConnection startConnection1 = new A.StartConnection() { Id = (DF.UInt32Value)448142074U, Index = (DF.UInt32Value)3U };
A.StartConnection startConnection = new A.StartConnection()
{
Id = (DF.UInt32Value)(uint)shape1.ElementId,
Index = (DF.UInt32Value)3U
};
//A.EndConnection endConnection1 = new A.EndConnection() { Id = (DF.UInt32Value)1011268246U, Index = (DF.UInt32Value)2U };
A.EndConnection endConnection = new A.EndConnection()
{
Id = (DF.UInt32Value)(uint)shape2.ElementId,
Index = (DF.UInt32Value)2U
};
nonVisualConnectorProperties.Append(startConnection);
nonVisualConnectorProperties.Append(endConnection);
var shapeProperties = new DWS.ShapeProperties();
var transform2D = new A.Transform2D();
//var offset4 = new A.Offset() { X = 914400L, Y = 311150L };
// 96 (same as shape1.width),33 (half of shape1.height)
var connectorX = shape1.Width * 9525;
var connectorY = shape1.Height / 2 * 9525;
var offset4 = new A.Offset()
{
X = connectorX,
Y = connectorY
};
//var extents4 = new A.Extents() { Cx = 1733550L, Cy = 6350L };
// 182, 1
// connector.Cx = Group.Width - (shape1.Width+shape2.Width)
var connectorCx = groupCx - (connectorX + (shape2.Width * 9525));
var extents4 = new A.Extents() { Cx = connectorCx, Cy = 6350L };