Files
csharpcode/omegapro/Omega/Classes/Utilities.cs
2025-08-02 05:20:17 +07:00

210 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;
using Datalib.EntityClasses;
using Datalib.FactoryClasses;
using Datalib.HelperClasses;
using Datalib.Linq;
using Datalib.DatabaseSpecific;
using SD.LLBLGen.Pro.ORMSupportClasses;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using Power= OpenXmlPowerTools;
using Sys=System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;
using System.IO;
namespace Omega
{
class Utilities
{
public static void ConverttoPdf(string input, string output)
{
// Create an instance of Word.exe
Word._Application oWord = new Word.Application();
// Make this instance of word invisible (Can still see it in the taskmgr).
oWord.Visible = false;
// Interop requires objects.
object oMissing = System.Reflection.Missing.Value;
object isVisible = true;
object readOnly = false;
object oInput = input;
object oOutput = output;
object oFormat = WdSaveFormat.wdFormatPDF;
// Load a document into our instance of word.exe
Word._Document oDoc = oWord.Documents.Open(ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Make this document the active document.
oDoc.Activate();
// Save this document in Word 2003 format.
oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Always close Word.exe.
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
}
public static void PrintQuiz(TblQuizEntity CurrQuiz)
{
List <Power.Source> sources = new List<Power.Source>();
string SubjectId = "xxxx";
string FieldId = "xxxx";
int QuestionNum = 1;
Boolean Renumering = false;
string prefix = "";
LinqMetaData meta = new LinqMetaData(new DataAccessAdapter());
byte[] header = Convert.FromBase64String(meta.TblQuizTemplate.Where(m => m.QuizTemplateId == CurrQuiz.QuizTemplateId).FirstOrDefault().HeaderDes);
Power.WmlDocument headercon = new Power.WmlDocument(Guid.NewGuid().ToString("N"), header);
Power.OpenXmlMemoryStreamDocument modifystream = new Power.OpenXmlMemoryStreamDocument(headercon);
WordprocessingDocument modifywDoc =modifystream.GetWordprocessingDocument();
Power.TextReplacer.SearchAndReplace(modifywDoc, "{year}", CurrQuiz.QuizYear.ToString(), false);
Power.TextReplacer.SearchAndReplace(modifywDoc, "{QuizCode}", CurrQuiz.QuizCode, false);
Power.TextReplacer.SearchAndReplace(modifywDoc, "{QuizTime}", CurrQuiz.QuizTime.ToString(), false);
sources.Add(new Power.Source(modifystream.GetModifiedWmlDocument(), false));
byte[] footer = Convert.FromBase64String(meta.TblQuizTemplate.Where(m => m.QuizTemplateId == CurrQuiz.QuizTemplateId).FirstOrDefault().FooterDes);
Renumering = meta.TblQuizTemplate.Where(m => m.QuizTemplateId == CurrQuiz.QuizTemplateId).FirstOrDefault().Renumering;
var result = (from q in meta.TblQuestion join m in meta.TblQuizQuestion.Where(m => m.QuizId == CurrQuiz.QuizId) on q.QuestionId equals m.QuestionId select new { SubjectId = q.SubjectId, FieldID = q.FieldId, con = q.Content }).ToList().OrderBy(m => m.SubjectId).ThenBy(m => m.FieldID).ToList();
foreach (var q in result)
{
if(q.SubjectId!=SubjectId)
{
SubjectId = q.SubjectId;
QuestionNum = 1;
byte[] SubjectDes = Convert.FromBase64String(meta.TblSubject.Where(m => m.SubjectId == q.SubjectId).FirstOrDefault().SubjectDes);
sources.Add(new Power.Source(new Power.WmlDocument(Guid.NewGuid().ToString("N"), SubjectDes), false));
}
if (q.FieldID != FieldId)
{
try
{
FieldId = q.FieldID;
if (Renumering) QuestionNum = 1;
prefix = meta.TblField.Where(m => m.SubjectId == q.SubjectId).Where(m => m.FieldId == FieldId).FirstOrDefault().Prefix;
byte[] FieldDes = Convert.FromBase64String(meta.TblField.Where(m => m.SubjectId == q.SubjectId).Where(m => m.FieldId == q.FieldID).FirstOrDefault().Description);
sources.Add(new Power.Source(new Power.WmlDocument(Guid.NewGuid().ToString("N"), FieldDes), false));
}
catch (Exception err)
{
Sys.MessageBox.Show(q.SubjectId + " " + q.FieldID);
}
}
byte[] buf = Convert.FromBase64String(q.con);
Power.WmlDocument con = new Power.WmlDocument(Guid.NewGuid().ToString("N"), buf);
modifystream = new Power.OpenXmlMemoryStreamDocument(con);
modifywDoc = modifystream.GetWordprocessingDocument();
Power.TextReplacer.SearchAndReplace(modifywDoc, "{$}", prefix + (QuestionNum++).ToString() , false);
sources.Add(new Power.Source(modifystream.GetModifiedWmlDocument(), false));
}
sources.Add(new Power.Source(new Power.WmlDocument(CurrQuiz.QuizTemplateId, footer), false));
Power.WmlDocument FinalDoc = Power.DocumentBuilder.BuildDocument(sources);
string OutDocFile = SettingInfo.OutputFolder + @"\"+ CurrQuiz.QuizCode.ToString() + ".docx";
string OutPdfFile = SettingInfo.OutputFolder + @"\"+ CurrQuiz.QuizCode.ToString() + ".pdf";
FinalDoc.SaveAs(OutDocFile);
Utilities.ConverttoPdf(OutDocFile, OutPdfFile);
Forms.frmPDFViewer frm = new Forms.frmPDFViewer();
frm.pdffile = OutPdfFile;
frm.LoadData();
frm.Show();
}
public static void PrintQuizAnswer(TblQuizEntity CurrQuiz)
{
List<Power.Source> sources = new List<Power.Source>();
string SubjectId = "xxxx";
string FieldId = "xxxx";
int QuestionNum = 1;
Boolean Renumering = false;
string prefix = "";
LinqMetaData meta = new LinqMetaData(new DataAccessAdapter());
byte[] header = Convert.FromBase64String(meta.TblQuizTemplate.Where(m => m.QuizTemplateId == CurrQuiz.QuizTemplateId).FirstOrDefault().HeaderDes);
Power.WmlDocument headercon = new Power.WmlDocument(Guid.NewGuid().ToString("N"), header);
Power.OpenXmlMemoryStreamDocument modifystream = new Power.OpenXmlMemoryStreamDocument(headercon);
WordprocessingDocument modifywDoc = modifystream.GetWordprocessingDocument();
Power.TextReplacer.SearchAndReplace(modifywDoc, "{year}", CurrQuiz.QuizYear.ToString(), false);
Power.TextReplacer.SearchAndReplace(modifywDoc, "{QuizCode}", CurrQuiz.QuizCode, false);
Power.TextReplacer.SearchAndReplace(modifywDoc, "{QuizTime}", CurrQuiz.QuizTime.ToString(), false);
sources.Add(new Power.Source(modifystream.GetModifiedWmlDocument(), false));
byte[] footer = Convert.FromBase64String(meta.TblQuizTemplate.Where(m => m.QuizTemplateId == CurrQuiz.QuizTemplateId).FirstOrDefault().FooterDes);
Renumering = meta.TblQuizTemplate.Where(m => m.QuizTemplateId == CurrQuiz.QuizTemplateId).FirstOrDefault().Renumering;
var result = (from q in meta.TblQuestion join m in meta.TblQuizQuestion.Where(m => m.QuizId == CurrQuiz.QuizId) on q.QuestionId equals m.QuestionId select new { SubjectId = q.SubjectId, FieldID = q.FieldId, con = q.Answer}).ToList().OrderBy(m => m.SubjectId).ThenBy(m => m.FieldID).ToList();
foreach (var q in result)
{
if (q.SubjectId != SubjectId)
{
SubjectId = q.SubjectId;
QuestionNum = 1;
byte[] SubjectDes = Convert.FromBase64String(meta.TblSubject.Where(m => m.SubjectId == q.SubjectId).FirstOrDefault().SubjectDes);
sources.Add(new Power.Source(new Power.WmlDocument(Guid.NewGuid().ToString("N"), SubjectDes), false));
}
if (q.FieldID != FieldId)
{
FieldId = q.FieldID;
if (Renumering) QuestionNum = 1;
prefix = meta.TblField.Where(m => m.SubjectId == q.SubjectId).Where(m => m.FieldId == FieldId).FirstOrDefault().Prefix;
byte[] FieldDes = Convert.FromBase64String(meta.TblField.Where(m => m.SubjectId == q.SubjectId).Where(m => m.FieldId == q.FieldID).FirstOrDefault().Description);
sources.Add(new Power.Source(new Power.WmlDocument(Guid.NewGuid().ToString("N"), FieldDes), false));
}
string prefixTemplate = Sys.Application.StartupPath + @"/templates/prefix.docx";
Power.WmlDocument con = new Power.WmlDocument(prefixTemplate);
modifystream = new Power.OpenXmlMemoryStreamDocument(con);
modifywDoc = modifystream.GetWordprocessingDocument();
Power.TextReplacer.SearchAndReplace(modifywDoc, "question", prefix + (QuestionNum++).ToString(), false);
sources.Add(new Power.Source(modifystream.GetModifiedWmlDocument(), false));
byte[] buf = Convert.FromBase64String(q.con);
Power.WmlDocument ans = new Power.WmlDocument(Guid.NewGuid().ToString("N"), buf);
sources.Add(new Power.Source(ans, false));
}
//sources.Add(new Power.Source(new Power.WmlDocument(CurrQuiz.QuizTemplateId, footer), false));
Power.WmlDocument FinalDoc = Power.DocumentBuilder.BuildDocument(sources);
string OutDocFile = SettingInfo.OutputFolder + @"\A" + CurrQuiz.QuizCode.ToString() + ".docx";
string OutPdfFile = SettingInfo.OutputFolder + @"\A" + CurrQuiz.QuizCode.ToString() + ".pdf";
FinalDoc.SaveAs(OutDocFile);
Utilities.ConverttoPdf(OutDocFile, OutPdfFile);
Forms.frmPDFViewer frm = new Forms.frmPDFViewer();
frm.pdffile = OutPdfFile;
frm.LoadData();
frm.Show();
}
}
}