public static string GeneratePDF(float headerHeight, float footerHeight, string labelId, string strIsGeneratePDF, string strFileName, string hasOrientation, string timePeriod)
{
string fileName = string.Empty, tempFileName = string.Empty, pdfFilePath = string.Empty, serviceMaxFilePath = string.Empty;
var tempDataFileName = string.Empty; string OrgfileName = string.Empty;
StringCollection fileToDelete = new StringCollection();
try
{
pdfFilePath = SecurityProfile.FileSystemPath.Replace("\\", "/") + "/LogBookPdf/";
serviceMaxFilePath = pdfFilePath + strFileName + ".pdf";
if (!Directory.Exists(pdfFilePath))
Directory.CreateDirectory(pdfFilePath);
if (strIsGeneratePDF == "2")
OrgfileName = strFileName;
else
OrgfileName = Guid.NewGuid().ToString();
tempFileName = pdfFilePath + OrgfileName + ".pdf";
}
catch (Exception ex)
{
Valgenesis.ErrorLog.LOG.Write(ex);
}
try
{
int tempHeader = 0, tempFooter = 0;
string headerInfo = string.Empty, contentInfo = string.Empty, footerInfo = string.Empty;
if (HttpContext.Current.Session["LogBookPdfHeader"] != null)
{
headerInfo = HttpContext.Current.Session["LogBookPdfHeader"].ToString();
headerInfo = headerInfo.Replace("../LogManager/LogBookPdfTemp/", "").Replace("../LogManager/css/", "");
}
if (HttpContext.Current.Session["LogBookPdfContent"] != null)
{
contentInfo = HttpContext.Current.Session["LogBookPdfContent"].ToString();
contentInfo = contentInfo.Replace("../LogManager/LogBookPdfTemp/", "").Replace("../LogManager/css/", "");
}
if (HttpContext.Current.Session["LogBookPdfFooter"] != null)
{
footerInfo = HttpContext.Current.Session["LogBookPdfFooter"].ToString();
footerInfo = footerInfo.Replace("../LogManager/LogBookPdfTemp/", "").Replace("../LogManager/css/", "");
}
LMLabel objLMLabel = new LMLabel();
LMLabelFacade objLMLabelFacade = new LMLabelFacade();
objLMLabel = objLMLabelFacade.GetSystemDetails(Convert.ToInt32(labelId));
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
BlinkConverterSettings blinkSettings = new BlinkConverterSettings();
//blinkSettings.EnableRepeatTableHeader = true;
//blinkSettings.EnableRepeatTableFooter = true;
//blinkSettings.SplitTextLines = false;
//blinkSettings.SplitImages = false;
//added for language conversion
var profileCommon = ProfileBase.Create(HttpContext.Current.Request.IsAuthenticated ? HttpContext.Current.User.Identity.Name
: HttpContext.Current.Request.AnonymousID, HttpContext.Current.User.Identity.IsAuthenticated) as ProfileCommon;
if (profileCommon.CurrentCulture != null)
Localizer.SetCulture(profileCommon.CurrentCulture);
//PaperSize-1:A3-Landscape, PaperSize-2:A3-Portrait, PaperSize-3:A4-Landscape, PaperSize-4:A4-Portrait...
int paperSize = objLMLabel.PaperSize, pageSizeWidth = 1024;
if (hasOrientation == "true")
{
for (int k = 0; k < 3; k++)
{
tempDataFileName = "";
if (k == 0)
{
//fileName = "Landscape";
fileName = Guid.NewGuid().ToString() + "-Landscape";
tempDataFileName = SecurityProfile.FileSystemPath.Replace("\\", "/") + "/temp/" + fileName + ".pdf";
fileToDelete.Add(tempDataFileName);
blinkSettings.Orientation = PdfPageOrientation.Landscape;
pageSizeWidth = 1200;
}
else if (k == 1)
{
//fileName = "Portrait";
fileName = Guid.NewGuid().ToString() + "-Portrait";
tempDataFileName = SecurityProfile.FileSystemPath.Replace("\\", "/") + "/temp/" + fileName + ".pdf";
fileToDelete.Add(tempDataFileName);
blinkSettings.Orientation = PdfPageOrientation.Portrait;
pageSizeWidth = 1080;
}
else if (k == 2)
{
//fileName = "Original";
fileName = Guid.NewGuid().ToString() + "-Original";
tempDataFileName = SecurityProfile.FileSystemPath.Replace("\\", "/") + "/temp/" + fileName + ".pdf";
fileToDelete.Add(tempDataFileName);
if (paperSize == 1)
{
blinkSettings.Orientation = PdfPageOrientation.Landscape;
pageSizeWidth = 1200;
}
else if (paperSize == 2)
{
blinkSettings.Orientation = PdfPageOrientation.Portrait;
pageSizeWidth = 1080;
}
else if (paperSize == 3)
{
blinkSettings.Orientation = PdfPageOrientation.Landscape;
pageSizeWidth = 1200;
}
else if (paperSize == 4)
{
blinkSettings.Orientation = PdfPageOrientation.Portrait;
pageSizeWidth = 1080;
}
}
tempHeader = GetHeaderFooterHeight(Convert.ToInt32(headerHeight), Convert.ToInt32(footerHeight), "Header");
tempFooter = GetHeaderFooterHeight(Convert.ToInt32(headerHeight), Convert.ToInt32(footerHeight), "Footer");
blinkSettings.PdfHeader = HeaderHTMLtoPDF(headerInfo, tempHeader, paperSize, fileName);
blinkSettings.PdfFooter = FooterHTMLtoPDF(footerInfo, tempFooter, paperSize, fileName);
blinkSettings.BlinkPath = HttpContext.Current.Server.MapPath("~/BlinkBinaries");
blinkSettings.ViewPortSize = new Size(pageSizeWidth, 0);
htmlConverter.ConverterSettings = blinkSettings;
//Convert url to pdf
string htmlPath = BindHTML("Content", contentInfo, fileName);
PdfDocument doc = htmlConverter.Convert(htmlPath, sharedDirPath);
doc.DocumentInformation.Title = Localizer.getCaption("lmlogbook_pdf_name");
doc.Save(tempDataFileName);
if (objLMLabel.IsServiceMaxEnabled && strIsGeneratePDF != "2")
doc.Save(serviceMaxFilePath);
doc.Close(true);
}
PdfLoadedDocument Landscapefile = new PdfLoadedDocument(fileToDelete[0]);
PdfLoadedDocument Portraitfile = new PdfLoadedDocument(fileToDelete[1]);
PdfLoadedDocument Originalfile = new PdfLoadedDocument(fileToDelete[2]);
PdfDocument FinalDoc = new PdfDocument();
CalculateStartIndex(dtLmLabelControl, fileToDelete);
for (int page_Cnt = 0; page_Cnt < dtLmLabelControl.Rows.Count; page_Cnt++)
{
if (Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["Orientation"]) == 1)
{
if (!string.IsNullOrEmpty(dtLmLabelControl.Rows[page_Cnt]["StartIndex"].ToString()))
{
if (!string.IsNullOrEmpty(dtLmLabelControl.Rows[page_Cnt]["EndIndex"].ToString()))
FinalDoc.ImportPageRange(Portraitfile, Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["StartIndex"]), Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["EndIndex"]));
else
FinalDoc.ImportPageRange(Portraitfile, Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["StartIndex"]), Portraitfile.Pages.Count - 1);
}
}
else if (Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["Orientation"]) == 2)
{
if (!string.IsNullOrEmpty(dtLmLabelControl.Rows[page_Cnt]["StartIndex"].ToString()))
{
if (!string.IsNullOrEmpty(dtLmLabelControl.Rows[page_Cnt]["EndIndex"].ToString()))
FinalDoc.ImportPageRange(Landscapefile, Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["StartIndex"]), Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["EndIndex"]));
else
FinalDoc.ImportPageRange(Landscapefile, Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["StartIndex"]), Landscapefile.Pages.Count - 1);
}
}
else if (Convert.ToInt32(dtLmLabelControl.Rows[page_Cnt]["Orientation"]) == 0)
{
FinalDoc.ImportPageRange(Originalfile, 0, Originalfile.Pages.Count - 1);
break;
}
}
FinalDoc.Save(tempFileName);
Document pdfDocument = new Document(tempFileName);
for (int guid_rem = 0; guid_rem < dtLmLabelControl.Rows.Count; guid_rem++)
{
// Create a TextFragmentAbsorber object to find all instances of the search text
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(dtLmLabelControl.Rows[guid_rem]["Code"].ToString());
// Accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);
// Get the extracted text fragments
TextFragmentCollection textFragments = textFragmentAbsorber.TextFragments;
// Remove the text fragments
foreach (TextFragment textFragment in textFragments)
{
// Remove the text fragment
textFragment.Text = "";
}
}
pdfDocument.Save(tempFileName);
if (objLMLabel.IsServiceMaxEnabled && strIsGeneratePDF != "2")
pdfDocument.Save(serviceMaxFilePath);
DeleteTemFiles(fileToDelete);
fileToDelete.Clear();
}
else
{
tempHeader = GetHeaderFooterHeight(Convert.ToInt32(headerHeight), Convert.ToInt32(footerHeight), "Header");
tempFooter = GetHeaderFooterHeight(Convert.ToInt32(headerHeight), Convert.ToInt32(footerHeight), "Footer");
blinkSettings.PdfHeader = HeaderHTMLtoPDF(headerInfo, tempHeader, paperSize, OrgfileName);
blinkSettings.PdfFooter = FooterHTMLtoPDF(footerInfo, tempFooter, paperSize, OrgfileName);
blinkSettings.BlinkPath = HttpContext.Current.Server.MapPath("~/BlinkBinaries");
blinkSettings.ViewPortSize = new Size(pageSizeWidth, 0);
htmlConverter.ConverterSettings = blinkSettings;
if (paperSize == 1)
{
blinkSettings.Orientation = PdfPageOrientation.Landscape;
pageSizeWidth = 1200;
}
else if (paperSize == 2)
{
blinkSettings.Orientation = PdfPageOrientation.Portrait;
pageSizeWidth = 1080;
}
else if (paperSize == 3)
{
blinkSettings.Orientation = PdfPageOrientation.Landscape;
pageSizeWidth = 1200;
}
else if (paperSize == 4)
{
blinkSettings.Orientation = PdfPageOrientation.Portrait;
pageSizeWidth = 1080;
}
//Convert url to pdf
string htmlPath = BindHTML("Content", contentInfo, OrgfileName);
PdfDocument doc = htmlConverter.Convert(htmlPath, sharedDirPath);
doc.DocumentInformation.Title = Localizer.getCaption("lmlogbook_pdf_name");
doc.Save(tempFileName);
if (objLMLabel.IsServiceMaxEnabled && strIsGeneratePDF != "2")
doc.Save(serviceMaxFilePath);
}
AttachEvidence(tempFileName);
if (objLMLabel.PageNumberRequired > 0 && !string.IsNullOrEmpty(objLMLabel.PageNumberPosition))
IncludePageNumber(tempFileName, objLMLabel.PageNumberPosition);
if (!string.IsNullOrEmpty(timePeriod))
IncludeTimePeriod(tempFileName, timePeriod);
if (objLMLabel.StampTextRequired == 1)
IncludeStampText(tempFileName, objLMLabel.StampTextDetails);
HttpContext.Current.Session["IsLogBookPdfSaved"] = "1";
}
catch (Exception ex)
{
Valgenesis.ErrorLog.LOG.Write(ex);
}
return OrgfileName;
}
IME-LMLD-3368.01.pdf (354.1 KB)