157 lines
6.0 KiB
C#
157 lines
6.0 KiB
C#
using SushiBarBusinessLogic.OfficePackage.HelpersEnum;
|
|
using SushiBarBusinessLogic.OfficePackage.HelpersModels;
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
using DocumentFormat.OpenXml.Wordprocessing;
|
|
using DocumentFormat.OpenXml;
|
|
using Document = DocumentFormat.OpenXml.Wordprocessing.Document;
|
|
using TableCell = DocumentFormat.OpenXml.Drawing.TableCell;
|
|
using TableRow = DocumentFormat.OpenXml.Drawing.TableRow;
|
|
using Text = DocumentFormat.OpenXml.Wordprocessing.Text;
|
|
|
|
namespace SushiBarBusinessLogic.OfficePackage.Implements
|
|
{
|
|
public class SaveToWord : AbstractSaveToWord
|
|
{
|
|
private WordprocessingDocument? _wordDocument;
|
|
private Body? _docBody;
|
|
private static JustificationValues GetJustificationValues(WordJustificationType type)
|
|
{
|
|
return type switch
|
|
{
|
|
WordJustificationType.Both => JustificationValues.Both,
|
|
WordJustificationType.Center => JustificationValues.Center,
|
|
WordJustificationType.Right => JustificationValues.Right,
|
|
_ => JustificationValues.Left,
|
|
};
|
|
}
|
|
|
|
private static SectionProperties CreateSectionProperties()
|
|
{
|
|
var properties = new SectionProperties();
|
|
var pageSize = new PageSize
|
|
{
|
|
Orient = PageOrientationValues.Portrait
|
|
};
|
|
properties.AppendChild(pageSize);
|
|
return properties;
|
|
}
|
|
|
|
private static ParagraphProperties? CreateParagraphProperties(WordTextProperties? paragraphProperties)
|
|
{
|
|
if (paragraphProperties == null)
|
|
{
|
|
return null;
|
|
}
|
|
var properties = new ParagraphProperties();
|
|
properties.AppendChild(new Justification()
|
|
{
|
|
Val =
|
|
GetJustificationValues(paragraphProperties.JustificationType)
|
|
});
|
|
properties.AppendChild(new SpacingBetweenLines
|
|
{
|
|
LineRule = LineSpacingRuleValues.Auto
|
|
});
|
|
properties.AppendChild(new Indentation());
|
|
var paragraphMarkRunProperties = new ParagraphMarkRunProperties();
|
|
if (!string.IsNullOrEmpty(paragraphProperties.Size))
|
|
{
|
|
paragraphMarkRunProperties.AppendChild(new FontSize
|
|
{
|
|
Val =
|
|
paragraphProperties.Size
|
|
});
|
|
}
|
|
properties.AppendChild(paragraphMarkRunProperties);
|
|
return properties;
|
|
}
|
|
protected override void CreateWord(WordInfo info)
|
|
{
|
|
_wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document);
|
|
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
|
|
mainPart.Document = new Document();
|
|
_docBody = mainPart.Document.AppendChild(new Body());
|
|
}
|
|
protected override void CreateParagraph(WordParagraph? paragraph)
|
|
{
|
|
if (_docBody == null || paragraph == null)
|
|
{
|
|
return;
|
|
}
|
|
var docParagraph = new Paragraph();
|
|
|
|
docParagraph.AppendChild(CreateParagraphProperties(paragraph.TextProperties));
|
|
foreach (var run in paragraph.Texts)
|
|
{
|
|
var docRun = new Run();
|
|
var properties = new RunProperties();
|
|
properties.AppendChild(new FontSize { Val = run.Item2.Size });
|
|
if (run.Item2.Bold)
|
|
{
|
|
properties.AppendChild(new Bold());
|
|
}
|
|
docRun.AppendChild(properties);
|
|
docRun.AppendChild(new Text
|
|
{
|
|
Text = run.Item1,
|
|
Space = SpaceProcessingModeValues.Preserve
|
|
});
|
|
docParagraph.AppendChild(docRun);
|
|
}
|
|
_docBody.AppendChild(docParagraph);
|
|
}
|
|
protected override void SaveWord(WordInfo info)
|
|
{
|
|
if (_docBody == null || _wordDocument == null)
|
|
{
|
|
return;
|
|
}
|
|
_docBody.AppendChild(CreateSectionProperties());
|
|
_wordDocument.MainDocumentPart!.Document.Save();
|
|
_wordDocument.Close();
|
|
}
|
|
|
|
protected override void CreateTable(List<TableRow?>? rows)
|
|
{
|
|
if (_docBody == null || rows == null)
|
|
{
|
|
return;
|
|
}
|
|
var table = new Table();
|
|
var tblProp = new TableProperties(
|
|
new TableBorders(
|
|
new TopBorder { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackDashes), Size = 3 },
|
|
new BottomBorder { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackDashes), Size = 3 },
|
|
new LeftBorder { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackDashes), Size = 3 },
|
|
new RightBorder { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackDashes), Size = 3 },
|
|
new InsideHorizontalBorder { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackDashes), Size = 3 },
|
|
new InsideVerticalBorder { Val = new EnumValue<BorderValues>(BorderValues.BasicBlackDashes), Size = 3 }
|
|
)
|
|
);
|
|
table.AppendChild(tblProp);
|
|
table.Append(rows);
|
|
_docBody.AppendChild(table);
|
|
}
|
|
|
|
protected override TableRow? CreateRow(List<TableCell?>? cells)
|
|
{
|
|
if (cells == null) return null;
|
|
var row = new TableRow();
|
|
row.Append(cells);
|
|
return row;
|
|
}
|
|
|
|
protected override TableCell? CreateCell(WordCell? cell)
|
|
{
|
|
if (cell == null) return null;
|
|
var createCell = new TableCell(new Paragraph(
|
|
new Run(
|
|
new Text(cell.Text))));
|
|
createCell.Append(
|
|
new TableCellProperties(
|
|
new TableCellWidth { Width = cell.Width.ToString() }));
|
|
return createCell;
|
|
}
|
|
}
|
|
}
|