- Code: Select all
public byte[] ConvertToPDFBinary(string domainName, byte[] binaryWordData, string assignerSignager, string assignerBranch, string assignerInstitution, int strategyId)
{
byte[] bytePDF = null;
try
{
var serverTempFolder = ApplicationConfiguration.GetSysConfiguration(Constants.ServerTempFolder);
if (string.IsNullOrEmpty(serverTempFolder))
throw new InvalidOperationException("ServerTempFolder is not configured for domain [" + domainName + "]");
var pathFileTemplate = Path.Combine(serverTempFolder, "LetterTemplate.PDF");
using (var document = new Document())
{
using (Stream s = new MemoryStream(binaryWordData))
{
document.LoadFromStream(s, FileFormat.Docx);
//replace dummy data
ReplaceDummyData(document, assignerSignager, assignerBranch);
var isEmbedFonts = GetIsEmbedFonts(strategyId);
var toPdfParams = new ToPdfParameterList();
if (isEmbedFonts)
{
toPdfParams = GetToPdfParameterList();
}
else
{
toPdfParams.UsePSCoversion = true;
}
document.SaveToFile(pathFileTemplate, toPdfParams);
var PDFVersion = GetPDFVersion();
if (PDFVersion != Spire.Pdf.PdfVersion.Version1_5)
{
using (var documentPDF = new Spire.Pdf.PdfDocument())
{
documentPDF.LoadFromFile(pathFileTemplate);
documentPDF.FileInfo.Version = PDFVersion;
documentPDF.SaveToFile(pathFileTemplate);
}
}
bytePDF = File.ReadAllBytes(pathFileTemplate);
}
}
}
catch (Exception ex)
{
Log.Error(ex);
LogErrorToDB("ConvertToPDFBinary", ex.Message, Constants.SEV_URGENT, Constants.LEVEL_ERROR);
bytePDF = null;
}
return bytePDF;
}
public static ToPdfParameterList GetToPdfParameterList()
{
var fontsPath = Path.Combine(HttpContext.Current.Server.MapPath(Constants.FontFolderPath));
var toPdfParams = new ToPdfParameterList()
{
UsePSCoversion = true
};
using (var fontCollection = new PrivateFontCollection())
{
var dInfo = new DirectoryInfo(fontsPath);
var fonts = new List<PrivateFontPath>();
if (dInfo.Exists)
{
var fInfos = dInfo.GetFiles();
for (var i = 0; i < fInfos.Length; i++)
{
var f = fInfos[i];
if (f.Extension.EndsWith("ttf") || f.Extension.EndsWith("otf") ||
f.Extension.EndsWith("fon") || f.Extension.EndsWith("fnt"))
{
fontCollection.AddFontFile(f.FullName);
fonts.Add(new PrivateFontPath(fontCollection.Families[i].Name, f.FullName));
}
}
if (fonts.Count > 0)
{
toPdfParams.UsePSCoversion = false;
toPdfParams.PrivateFontPaths = fonts;
}
}
}
return toPdfParams;
}