I have some problem to solve my task, here i provide my problem is, hope you can help me to solve.
public FileStreamResult ItdExportV2CustomDocument(ItdExportCustomDocumentData itdExportCustomDocument)
{
WDocument wDocument = new WDocument();
IWParagraphStyle headerStyle = wDocument.AddParagraphStyle("Title_Issue_Type");
headerStyle.CharacterFormat.FontSize = 14f;
headerStyle.CharacterFormat.Bold = true;
headerStyle.ParagraphFormat.LineSpacing = 12f;
IWParagraphStyle cossosStyle = wDocument.AddParagraphStyle("Cossos_Style");
cossosStyle.CharacterFormat.FontSize = 12f;
cossosStyle.ParagraphFormat.AfterSpacing = 15;
cossosStyle.ParagraphFormat.BeforeSpacing = 10;
cossosStyle.CharacterFormat.Bold = true;
cossosStyle.ParagraphFormat.LineSpacing = 12f;
foreach (var itdExportCustomIssueTypeDocument in itdExportCustomDocument.ItdExportCustomIssueTypeDocumentData!)
{
IWSection section = wDocument.AddSection();
section.PageSetup.PageSize = PageSize.A4;
section.PageSetup.Margins.All = 72;
// section.PageSetup.FirstPageTray = PrinterPaperTray.EnvelopeFeed;
// section.PageSetup.OtherPagesTray = PrinterPaperTray.MiddleBin;
IWParagraph titleSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
titleSection.ApplyStyle("Title_Issue_Type");
titleSection.AppendText("ISSUE TYPE : " + itdExportCustomIssueTypeDocument.TitleIssueType + "\n");
if (!string.IsNullOrEmpty(itdExportCustomIssueTypeDocument.TitleIssueType) && itdExportCustomIssueTypeDocument.TitleIssueType!.Equals("ISSUE"))
{
int cossosCounter = 0;
foreach (var Cossos in itdExportCustomIssueTypeDocument.ItdExportCustomItemDocumentData!)
{
IWParagraph cossosSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
cossosSection.ApplyStyle("Cossos_Style");
cossosSection.AppendText(" " + ToRoman(cossosCounter + 1) + ".\t" + Cossos.ItdCoso);
cossosCounter++;
int itdCounter = 0;
foreach (var Itd in Cossos.ItdCososDetail!)
{
IWParagraph itdTitleSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
itdTitleSection.ApplyStyle("Cossos_Style");
itdTitleSection.AppendText(" \t" + (itdCounter + 1) + ".\t" + Itd.ItdGroupName + "\n");
itdCounter++;
if (!string.IsNullOrEmpty(Itd.ItdDescription))
{
string docPath = GetObjectFullPath(Itd.ItdDescription);
MemoryStream memoryStream = new MemoryStream();
if (CheckFileExist(docPath))
{
LoadAsync(docPath, memoryStream).Wait();
} else {
throw new FileNotFoundException("path: " + Itd.ItdDescription + " doesn't exists. please contact administrator");
}
WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
for (int i = 0; i < srcDocObj.Sections.Count; i++)
{
srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
srcDocObj.Sections[i].PageSetup.Margins.All = 72;
}
wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
srcDocObj.Close();
memoryStream.Dispose();
}
IWParagraph recomendationSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
recomendationSection.ApplyStyle("Cossos_Style");
recomendationSection.AppendText(" Rekomendasi : " + "\n");
if (!string.IsNullOrEmpty(Itd.Recomendation))
{
string docPath = GetObjectFullPath(Itd.Recomendation);
MemoryStream memoryStream = new MemoryStream();
if (CheckFileExist(docPath))
{
LoadAsync(docPath, memoryStream).Wait();
} else {
throw new FileNotFoundException("path: " + Itd.Recomendation + " doesn't exists. please contact administrator");
}
WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
for (int i = 0; i < srcDocObj.Sections.Count; i++)
{
srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
srcDocObj.Sections[i].PageSetup.Margins.All = 72;
}
wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
srcDocObj.Close();
memoryStream.Dispose();
}
IWParagraph tlukSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
tlukSection.ApplyStyle("Cossos_Style");
tlukSection.AppendText(" Tindak Lanjut Unit Kerja : " + "\n");
if (!string.IsNullOrEmpty(Itd.TindakLanjutUnitKerja))
{
string docPath = GetObjectFullPath(Itd.TindakLanjutUnitKerja);
MemoryStream memoryStream = new MemoryStream();
if (CheckFileExist(docPath))
{
LoadAsync(docPath, memoryStream).Wait();
} else {
throw new FileNotFoundException("path: " + Itd.TindakLanjutUnitKerja + " doesn't exists. please contact administrator");
}
WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
for (int i = 0; i < srcDocObj.Sections.Count; i++)
{
srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
srcDocObj.Sections[i].PageSetup.Margins.All = 72;
}
wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
srcDocObj.Close();
memoryStream.Dispose();
}
IWParagraph targetSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
targetSection.ApplyStyle("Cossos_Style");
targetSection.AppendText(" Target Penyelesaian : " + "\n");
if (!string.IsNullOrEmpty(Itd.TargetPenyelesaian))
{
string docPath = GetObjectFullPath(Itd.TargetPenyelesaian);
MemoryStream memoryStream = new MemoryStream();
if (CheckFileExist(docPath))
{
LoadAsync(docPath, memoryStream).Wait();
} else {
throw new FileNotFoundException("path: " + Itd.TargetPenyelesaian + " doesn't exists. please contact administrator");
}
WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
for (int i = 0; i < srcDocObj.Sections.Count; i++)
{
srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
srcDocObj.Sections[i].PageSetup.Margins.All = 72;
}
wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
srcDocObj.Close();
memoryStream.Dispose();
}
}
}
}
else
{
String? cosoTitle = null;
long interation = 1;
IWParagraph? cosoSection = null;
int itdCounter = 0;
foreach (var Itd in itdExportCustomIssueTypeDocument.ItdExportCustomItemDocumentData!)
{
IWParagraph itdTitleSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
itdTitleSection.ApplyStyle("Cossos_Style");
itdTitleSection.AppendText(" " + (itdCounter + 1) + ".\t" + Itd.ItdGroupName + "\n");
itdCounter++;
if (!string.IsNullOrEmpty(Itd.ItdDescription))
{
string docPath = GetObjectFullPath(Itd.ItdDescription);
MemoryStream memoryStream = new MemoryStream();
if (CheckFileExist(docPath))
{
LoadAsync(docPath, memoryStream).Wait();
} else {
throw new FileNotFoundException("path: " + Itd.ItdDescription + " doesn't exists. please contact administrator");
}
WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
for (int i = 0; i < srcDocObj.Sections.Count; i++)
{
srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
srcDocObj.Sections[i].PageSetup.Margins.All = 72;
}
wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
srcDocObj.Close();
memoryStream.Dispose();
}
IWParagraph recomendationSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
recomendationSection.ApplyStyle("Cossos_Style");
recomendationSection.AppendText(" Rekomendasi : " + "\n");
if (!string.IsNullOrEmpty(Itd.Recomendation))
{
string docPath = GetObjectFullPath(Itd.Recomendation);
MemoryStream memoryStream = new MemoryStream();
if (CheckFileExist(docPath))
{
LoadAsync(docPath, memoryStream).Wait();
} else {
throw new FileNotFoundException("path: " + Itd.Recomendation + " doesn't exists. please contact administrator");
}
WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
for (int i = 0; i < srcDocObj.Sections.Count; i++)
{
srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
srcDocObj.Sections[i].PageSetup.Margins.All = 72;
}
wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
srcDocObj.Close();
memoryStream.Dispose();
}
IWParagraph tlukSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
tlukSection.ApplyStyle("Cossos_Style");
tlukSection.AppendText(" Tindak Lanjut Unit Kerja : " + "\n");
if (!string.IsNullOrEmpty(Itd.TindakLanjutUnitKerja))
{
string docPath = GetObjectFullPath(Itd.TindakLanjutUnitKerja);
MemoryStream memoryStream = new MemoryStream();
if (CheckFileExist(docPath))
{
LoadAsync(docPath, memoryStream).Wait();
} else {
throw new FileNotFoundException("path: " + Itd.TindakLanjutUnitKerja + " doesn't exists. please contact administrator");
}
WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
for (int i = 0; i < srcDocObj.Sections.Count; i++)
{
srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
srcDocObj.Sections[i].PageSetup.Margins.All = 72;
}
wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
srcDocObj.Close();
memoryStream.Dispose();
}
IWParagraph targetSection = wDocument.Sections[wDocument.Sections.Count - 1].AddParagraph();
targetSection.ApplyStyle("Cossos_Style");
targetSection.AppendText(" Target Penyelesaian : " + "\n");
if (!string.IsNullOrEmpty(Itd.TargetPenyelesaian))
{
string docPath = GetObjectFullPath(Itd.TargetPenyelesaian);
MemoryStream memoryStream = new MemoryStream();
if (CheckFileExist(docPath))
{
LoadAsync(docPath, memoryStream).Wait();
} else {
throw new FileNotFoundException("path: " + Itd.TargetPenyelesaian + " doesn't exists. please contact administrator");
}
WDocument srcDocObj = new WDocument(memoryStream, WFormatType.Automatic);
srcDocObj.Sections[0].BreakCode = SectionBreakCode.NoBreak;
for (int i = 0; i < srcDocObj.Sections.Count; i++)
{
srcDocObj.Sections[i].PageSetup.PageSize = PageSize.A4;
srcDocObj.Sections[i].PageSetup.Margins.All = 72;
}
wDocument.ImportContent(srcDocObj, ImportOptions.UseDestinationStyles);
srcDocObj.Close();
memoryStream.Dispose();
}
}
}
}
FileStream? mergeStreamPath = null;
if (itdExportCustomDocument.SaveDocument != null && itdExportCustomDocument.SaveDocument == true)
{
string fullPath = GetObjectFullPath(itdExportCustomDocument.SaveDocumentTo!);
string mergeFileName = Path.GetFileName(itdExportCustomDocument.SaveDocumentTo!);
string tempMergePath = tempPath + "/" + DateTime.Now.ToString("yyMMddHHmm") + "-" + Guid.NewGuid().ToString();
string tmpMergeFullPath = tempMergePath + "/" + mergeFileName;
Directory.CreateDirectory(tempMergePath);
mergeStreamPath = new FileStream(tmpMergeFullPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
wDocument.Save(mergeStreamPath, WFormatType.Docx);
fileTransferUtility.UploadAsync(mergeStreamPath, bucketName, fullPath).Wait();
DeleteTemporayFiles(tempMergePath);
}
if (mergeStreamPath != null)
{
mergeStreamPath.Dispose();
}
Stream stream = new MemoryStream();
wDocument.Save(stream, WFormatType.Docx);
string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
stream.Position = 0;
return new FileStreamResult(stream, contentType)
{
FileDownloadName = "itd.docx"
};
}