//Nuget引用第三方插件 packagesiTextSharp.5.0.6libitextsharp.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Configuration;
using ESOP4Admin;
using Forms.BizProcess;
using iTextSharp.text.pdf;
using System.Reflection;
using iTextSharp.text;
namespace Forms
{
public partial class DownLoadForm : EsopPageCompanyAdmin
{
public const string BSNOTENAME = "B&S NOTE";
public byte[] getStorageFileByte(int companyID, string userName, string wFormType)
{
byte[] bytes = null;
if (companyID > 0 && !string.IsNullOrWhiteSpace(userName))
{
string ConnString = ConfigurationManager.ConnectionStrings["TCIT.Client"].ToString();
if (string.Equals(wFormType, "w8", StringComparison.InvariantCultureIgnoreCase))
{
FormW8BizProcess BizW8 = new FormW8BizProcess(ConnString);
DataTable dt = BizW8.GetUserFormData(companyID, userName);
if (dt.Rows.Count > 0)
{
string pdfTemplate = Path.Combine(Server.MapPath("~/Forms/PDF"), "FormW8.pdf");
bytes = this.BuildPdfW8(pdfTemplate, dt);
}
}
else if (string.Equals(wFormType, "w9", StringComparison.InvariantCultureIgnoreCase))
{
FormW9BizProcess BizW9 = new FormW9BizProcess(ConnString);
DataTable dt = BizW9.GetUserFormData(companyID, userName);
if (dt.Rows.Count > 0)
{
string pdfTemplate = Path.Combine(Server.MapPath("~/Forms/PDF"), "FormW9.pdf");
bytes = this.BuildPdfW9(pdfTemplate, dt);
}
}
else if (string.Equals(wFormType, "w8e", StringComparison.InvariantCultureIgnoreCase))
{
FormW8EBizProcess BizW8e = new FormW8EBizProcess(ConnString);
DataTable dt = BizW8e.GetUserFormData(companyID, userName);
if (dt.Rows.Count > 0)
{
string pdfTemplate = Path.Combine(Server.MapPath("~/Forms/PDF"), "FormW8E.pdf");
bytes = this.BuildPdfW8E(pdfTemplate, dt);
}
}
}
return bytes;
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (this.CurrentUser != null)
{
string UserName = this.CurrentUser.UserName;
int CompanyID = this.CurrentUser.CompanyID.Value;
string ConnString = ConfigurationManager.ConnectionStrings["TCIT.Client"].ToString();
EmployeeBizProcess empBiz = new EmployeeBizProcess(ConnString);
string FormType = Request["TYPE"];
if (Request["TYPE"] != null && Request["TYPE"].ToUpper() == "BSNOTE")
{
FormType = BSNOTENAME;
}
else
{
//不需要判斷使用者對應的表單
//FormType = empBiz.GetEmployeeFillFormType(CompanyID, UserName, FormType);
}
if (FormType.ToUpper() == "W8" || FormType.ToUpper() == "W-8BEN" || FormType.ToUpper() == "2")
{
FormW8BizProcess BizW8 = new FormW8BizProcess(ConnString);
DataTable dt = BizW8.GetUserFormData(CompanyID, UserName);
if (dt.Rows.Count > 0)
{
string pdfTemplate = Path.Combine(Server.MapPath("~/Forms/PDF"), "FormW8.pdf");
byte[] bytes = this.BuildPdfW8(pdfTemplate, dt);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=W-8BEN.pdf"); //+ DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
Response.BinaryWrite(bytes);
}
}
else if (FormType.ToUpper() == "W9" || FormType.ToUpper() == "W-9" || FormType == "3")
{
FormW9BizProcess BizW9 = new FormW9BizProcess(ConnString);
DataTable dt = BizW9.GetUserFormData(CompanyID, UserName);
if (dt.Rows.Count > 0)
{
string pdfTemplate = Path.Combine(Server.MapPath("~/Forms/PDF"), "FormW9.pdf");
byte[] bytes = this.BuildPdfW9(pdfTemplate, dt);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=W-9.pdf"); // + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
Response.BinaryWrite(bytes);
}
}
else if (FormType.ToUpper() == "W8E" || FormType.ToUpper() == "W-8BEN-E" || FormType.ToUpper() == "4")
{
FormW8EBizProcess W8EBiz = new FormW8EBizProcess(ConnString);
DataTable dt = W8EBiz.GetUserFormData(CompanyID, UserName);
if (dt.Rows.Count > 0)
{
string pdfTemplate = Path.Combine(Server.MapPath("~/Forms/PDF"), "FormW8E.pdf");
byte[] bytes = this.BuildPdfW8E(pdfTemplate, dt);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=W-8BEN-E.pdf");// + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
Response.BinaryWrite(bytes);
}
}
else if (FormType.ToUpper() == BSNOTENAME)
{
string employeeID = CurrentUser.EmployeeID;
FormW8BizProcess W8Biz = new FormW8BizProcess(ConnString);
DataTable dt = W8Biz.GetUserBSNOTEFormData(CompanyID, employeeID, BSNOTENAME);
string pdfTemplate = Path.Combine(Server.MapPath("~/Forms/PDF"), "BSNOTE.pdf");
if (dt.Rows.Count > 0)
{
byte[] bytes = this.BuildPdfBSNOTE(pdfTemplate, dt);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=Form_BSNOTE.pdf");// + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".pdf");
Response.BinaryWrite(bytes);
}
else
{
if (pdfTemplate != "")
{
FileStream fs = File.Open(pdfTemplate, FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=BSNOTE.pdf");
Response.BinaryWrite(bytes);
}
catch (Exception ex2)
{
throw ex2;
}
finally
{
fs.Close();
fs.Dispose();
}
}
}
}
}
}
catch (Exception ex)
{
FileLogger.Logger.WriteLog(ex.ToString(), FileLogger.Enum.LoggerType.错误日志, "");
}
finally
{
Response.Flush();
Response.End();
}
}
/// <summary>
/// 用 正则表达式 判断字符是不是包含汉字
/// </summary>
/// <param name="text">待判断字符或字符串</param>
/// <returns>真:是汉字;假:不是</returns>
public bool CheckStringChineseReg(string text)
{
if (!string.IsNullOrWhiteSpace(text))
{
for (int i = 0; i < text.Length; i++)
{
if (System.Text.RegularExpressions.Regex.IsMatch(text[i].ToString(), @"[u4e00-u9fbb]+$"))
return true;
}
}
return false;
}
#region 私有方法
/// <summary>
/// 拼接W8表单PDF
/// </summary>
/// <param name="PdfFilePath"></param>
/// <param name="dt"></param>
private byte[] BuildPdfW8(string PdfFilePath, DataTable dt)
{
PdfReader pdfReader = new PdfReader(PdfFilePath);
MemoryStream memoryStream = new MemoryStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, memoryStream);
AcroFields pdfFormFields = pdfStamper.AcroFields;
PdfContentByte over = pdfStamper.GetOverContent(1);//插入在第幾頁
//BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var fontName = "seguisb.ttf";
var inputSign = dt.Rows[0]["InputSign"].ToString();
//上面的seguisb.ttf字体是一种针对越南等语言包含特殊符号使用的字体,如Lê Quang Kiệt
//如果包含汉字的时候换一种字体,不然导出的pdf显示不出来,这两种字体在输入英语时,导出PDF是不受影响的
if (CheckStringChineseReg(inputSign))
fontName = "SIMHEI.TTF";
var fontPath = Path.Combine(Server.MapPath("~/Forms/Font"), fontName);
BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
over.SetColorFill(BaseColor.DARK_GRAY);
over.SetFontAndSize(bf, 8);
//jeff modified
//over.BeginText();
//var printName = dt.Rows[0]["EmployeeName"].ToString();
//// put the alignment and coordinates here
//over.ShowTextAligned(1, printName, 136, 108, 0);
//over.EndText();
if (dt.Rows[0]["ESignType"] != System.DBNull.Value)
{
switch (dt.Rows[0]["ESignType"].ToString())
{
case "0":
//輸入簽署
var InputSign = dt.Rows[0]["InputSign"].ToString();
over.BeginText();
over.ShowTextAligned(0, InputSign, 130, 75, 0);
//over.ShowTextAligned(0, InputSign, 0, 0, 0);
over.EndText();
break;
case "1":
//數位簽署
if (dt.Rows[0]["SignatureImageFile"] != System.DBNull.Value)
{
try
{
iTextSharp.text.Image sigImg = iTextSharp.text.Image.GetInstance((byte[])dt.Rows[0]["SignatureImageFile"]);
sigImg.ScalePercent(22f);
sigImg.SetAbsolutePosition(130, 75);
over.AddImage(sigImg);
}
catch (Exception ex)
{
TCGHL.Framework.Common.Logging.Logger.LogEvent("BuildPdfW8 Error: ", ex);
}
}
break;
}
}
this.AppendBottomLine(pdfStamper, pdfReader.GetPageSize(1), dt.Rows[0]["EmployeeName"].ToString(), dt.Rows[0]["ModifiedDate"].ToString(), dt.Rows[0]["UTCHours"].ToString(), dt.Rows[0]["IPAddress"].ToString(), pdfReader.NumberOfPages);
try
{
pdfFormFields.SetField("txtEmployeeName", dt.Rows[0]["EmployeeName"].ToString());
pdfFormFields.SetField("txtCountry", dt.Rows[0]["Country"].ToString());
pdfFormFields.SetField("txtResidenAddress1", dt.Rows[0]["ResidenAddress1"].ToString());
pdfFormFields.SetField("txtResidenAddress2", dt.Rows[0]["ResidenAddress2"].ToString());
pdfFormFields.SetField("txtResidenCountry", dt.Rows[0]["ResidenCountry"].ToString());
pdfFormFields.SetField("txtMailAddress1", dt.Rows[0]["MailAddress1"].ToString());
pdfFormFields.SetField("txtMailAddress2", dt.Rows[0]["MailAddress2"].ToString());
pdfFormFields.SetField("txtMailCountry", dt.Rows[0]["MailCountry"].ToString());
pdfFormFields.SetField("txtTaxpayerIdNumber", dt.Rows[0]["TaxpayerIdNumber"].ToString());
pdfFormFields.SetField("txtForeignTaxIdNumber", dt.Rows[0]["ForeignTaxIdNumber"].ToString());
pdfFormFields.SetField("txtReferenceNumber", dt.Rows[0]["ReferenceNumber"].ToString());
pdfFormFields.SetField("txtDateOfBirth", Convert.ToDateTime(dt.Rows[0]["DateOfBirth"]).ToString("MM-dd-yyyy"));
pdfFormFields.SetField("txtResidenceCountry", dt.Rows[0]["ResidenceCountry"].ToString());
pdfFormFields.SetField("txtArticle", dt.Rows[0]["Terms10_1"].ToString());
pdfFormFields.SetField("txtPercent", dt.Rows[0]["Terms10_2"].ToString());
pdfFormFields.SetField("txtIncome", dt.Rows[0]["Terms10_3"].ToString());
pdfFormFields.SetField("txtReason_a", dt.Rows[0]["Terms10_4"].ToString());
if (dt.Rows[0]["ESignType"] != System.DBNull.Value)
pdfFormFields.SetField("txtDate", DateTime.Now.ToString("MM-dd-yyyy"));
//PrintName
pdfFormFields.SetField("f_21", dt.Rows[0]["EmployeeName"].ToString());
pdfStamper.FormFlattening = false;
pdfStamper.Close();
byte[] bytes = memoryStream.GetBuffer();
memoryStream.Close();
return bytes;
}
catch (Exception ex)
{
throw ex;
}
finally
{
memoryStream.Close();
memoryStream.Dispose();
}
}
/// <summary>
/// 拼接W9表单PDF
/// </summary>
/// <param name="PdfFilePath"></param>
/// <param name="dt"></param>
/// <returns></returns>
private byte[] BuildPdfW9(string PdfFilePath, DataTable dt)
{
/// 注意:对于下载的pdf文件,用Adobe等工具读取pdf和用Chrome浏览器打开pdf时,对于pdf中的一组中的多个复选框是否选中是有区别的,
/// 因此代码对于设置pdf是否选中有2种设置,对于Chrome浏览器等,要用索引设置,如pdfFormFields.SetField($"chkTaxClassification[{taxCode - 1}]", taxCode.ToString());
/// 对于原生pdf文件,请使用pdfFormFields.SetField($"chkTaxClassification", taxCode.ToString());来设置是否选中
PdfReader pdfReader = new PdfReader(PdfFilePath);
MemoryStream memoryStream = new MemoryStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, memoryStream);
AcroFields pdfFormFields = pdfStamper.AcroFields;
PdfContentByte over = pdfStamper.GetOverContent(1);//插入在第幾頁
//BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var fontName = "seguisb.ttf";
var inputSign = dt.Rows[0]["InputSign"].ToString();
//上面的seguisb.ttf字体是一种针对越南等语言包含特殊符号使用的字体,如Lê Quang Kiệt
//如果包含汉字的时候换一种字体,不然导出的pdf显示不出来,这两种字体在输入英语时,导出PDF是不受影响的
if (CheckStringChineseReg(inputSign))
fontName = "SIMHEI.TTF";
var fontPath = Path.Combine(Server.MapPath("~/Forms/Font"), fontName);
BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
over.SetColorFill(BaseColor.DARK_GRAY);
over.SetFontAndSize(bf, 10);
if (dt.Rows[0]["ESignType"] != System.DBNull.Value)
{
switch (dt.Rows[0]["ESignType"].ToString())
{
case "0":
//輸入簽署
var InputSign = dt.Rows[0]["InputSign"].ToString();
over.BeginText();
over.ShowTextAligned(0, InputSign, 160, 230, 0);
over.EndText();
break;
case "1":
//數位簽署
if (dt.Rows[0]["SignatureImageFile"] != System.DBNull.Value)
{
try
{
iTextSharp.text.Image sigImg = iTextSharp.text.Image.GetInstance((byte[])dt.Rows[0]["SignatureImageFile"]);
sigImg.ScalePercent(22f);
sigImg.SetAbsolutePosition(140, 230);
over.AddImage(sigImg);
}
catch (Exception ex)
{
TCGHL.Framework.Common.Logging.Logger.LogEvent("BuildPdfW9 Error: ", ex);
}
}
break;
}
}
this.AppendBottomLine(pdfStamper, pdfReader.GetPageSize(1), dt.Rows[0]["EmployeeName"].ToString(), dt.Rows[0]["ModifiedDate"].ToString(), dt.Rows[0]["UTCHours"].ToString(), dt.Rows[0]["IPAddress"].ToString(), pdfReader.NumberOfPages);
try
{
pdfFormFields.SetField("txtEmployeeName", dt.Rows[0]["EmployeeName"].ToString());
pdfFormFields.SetField("txtBusinessName", dt.Rows[0]["BusinessName"].ToString());
//pdfFormFields.SetField("chkTaxClassification", dt.Rows[0]["TaxClassification"].ToString());
pdfFormFields.SetField("txtCSP", dt.Rows[0]["CSP"].ToString());
pdfFormFields.SetField("txtOther", dt.Rows[0]["Other"].ToString());
pdfFormFields.SetField("txtPayeeCode", dt.Rows[0]["PayeeCode"].ToString());
pdfFormFields.SetField("txtReportingCode", dt.Rows[0]["ReportingCode"].ToString());
pdfFormFields.SetField("txtAddress", dt.Rows[0]["Address"].ToString());
pdfFormFields.SetField("txtZipCode", dt.Rows[0]["ZipCode"].ToString());
pdfFormFields.SetField("txtRequesterProfile", dt.Rows[0]["RequesterProfile"].ToString());
pdfFormFields.SetField("txtAccountNumber", dt.Rows[0]["AccountNumber"].ToString());
if (dt.Rows[0]["ESignType"] != System.DBNull.Value)
pdfFormFields.SetField("txtDate", DateTime.Now.ToString("MM-dd-yyyy"));
int taxCode = 0;
Int32.TryParse(dt.Rows[0]["TaxClassification"].ToString(), out taxCode);
if (taxCode > 0)
{
//以下代码的作用是在Adobe等原始pdf读取工具使复选框被选中
pdfFormFields.SetField($"chkTaxClassification", taxCode.ToString());
//以下代码的作用是在谷歌,火狐等浏览器中查看pdf时,使复选框被选中
pdfFormFields.SetField($"chkTaxClassification[{taxCode - 1}]", taxCode.ToString());
}
string SSN = dt.Rows[0]["SSN"].ToString();
for (int i = 0; i < SSN.Length; i++)
{
pdfFormFields.SetField("txtSSN" + (i + 1), SSN.Substring(i, 1));
}
string EIN = dt.Rows[0]["EIN"].ToString();
for (int i = 0; i < EIN.Length; i++)
{
pdfFormFields.SetField("txtEIN" + (i + 1), EIN.Substring(i, 1));
}
pdfStamper.FormFlattening = false;
pdfStamper.Close();
byte[] bytes = memoryStream.GetBuffer();
memoryStream.Close();
return bytes;
}
catch (Exception ex)
{
throw ex;
}
finally
{
memoryStream.Close();
memoryStream.Dispose();
}
}
private string GetUTCTime(DateTime time, string utcHoures)
{
return time.ToString("MM/dd/yyyy") + " at " + time.ToString($"HH:mm:ss UTC{utcHoures}:00");
}
private string GetUTCHourWithSign(string utcHours)
{
return (utcHours.IndexOf('-') < 0 ? "+" : string.Empty) + utcHours;
}
private void AppendBottomLine(PdfStamper pdfStamper, Rectangle pdfPageSize, string name, string modifyDate, string utcHours, string ipaddress, int totalPages)
{
var isShowModifyInfo = System.Configuration.ConfigurationManager.AppSettings["isShowModifyInfo"]?.ToString() == "1" ? true : false;
if (!isShowModifyInfo)
{
return;
}
utcHours = this.GetUTCHourWithSign(utcHours);
var date = Convert.ToDateTime(modifyDate);
PdfContentByte over;
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var value = $"The agreement was accepted by {name} on {GetUTCTime(date, utcHours)} through IP address of {ipaddress}.";
for (int i = 1; i <= totalPages; i++)
{
over = pdfStamper.GetOverContent(i);//插入在第幾頁
over.SetColorFill(BaseColor.DARK_GRAY);
over.SetFontAndSize(bf, 8);
over.BeginText();
over.ShowTextAligned(PdfContentByte.ALIGN_CENTER, value, (pdfPageSize.Left + pdfPageSize.Right) / 2, 10, 0);
over.EndText();
}
}
/// <summary>
/// 拼接W8E表单PDF
/// </summary>
/// <param name="PdfFilePath"></param>
/// <param name="dt"></param>
/// <returns></returns>
private byte[] BuildPdfW8E(string PdfFilePath, DataTable dt)
{
/// 注意:对于下载的pdf文件,用Adobe等工具读取pdf和用Chrome浏览器打开pdf时,对于pdf中的一组中的多个复选框是否选中是有区别的,
/// 因此代码对于设置pdf是否选中有2种设置,对于Chrome浏览器等,要用索引设置,如pdfFormFields.SetField($"chkTaxClassification[{taxCode - 1}]", taxCode.ToString());
/// 对于原生pdf文件,请使用pdfFormFields.SetField($"chkTaxClassification", taxCode.ToString());来设置是否选中
var date = Convert.ToDateTime(dt.Rows[0]["ModifiedDate"]);
PdfReader pdfReader = new PdfReader(PdfFilePath);
MemoryStream memoryStream = new MemoryStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, memoryStream);
AcroFields pdfFormFields = pdfStamper.AcroFields;
PdfContentByte over = pdfStamper.GetOverContent(8);
//BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var fontName = "seguisb.ttf";
var inputSign = dt.Rows[0]["InputSign"].ToString();
//上面的seguisb.ttf字体是一种针对越南等语言包含特殊符号使用的字体,如Lê Quang Kiệt
//如果包含汉字的时候换一种字体,不然导出的pdf显示不出来,这两种字体在输入英语时,导出PDF是不受影响的
if (CheckStringChineseReg(inputSign))
fontName = "SIMHEI.TTF";
var fontPath = Path.Combine(Server.MapPath("~/Forms/Font"), fontName);
BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
over.SetColorFill(BaseColor.DARK_GRAY);
over.SetFontAndSize(bf, 8);
//over.BeginText();
//var printName = dt.Rows[0]["BeneficialOwner"].ToString();
//// put the alignment and coordinates here
//over.ShowTextAligned(1, printName, 410, 543, 0);
//over.EndText();
//over.BeginText();
//over.ShowTextAligned(2, date.ToString("MM-dd-yyyy"), 560, 543, 0);
//over.EndText();
if (dt.Rows[0]["ESignType"] != System.DBNull.Value)
{
switch (dt.Rows[0]["ESignType"].ToString())
{
case "0":
//輸入簽署
var InputSign = dt.Rows[0]["InputSign"].ToString();
over.BeginText();
over.ShowTextAligned(0, InputSign, 132, 123, 0);
over.EndText();
break;
case "1":
//數位簽署
if (dt.Rows[0]["SignatureImageFile"] != System.DBNull.Value)
{
try
{
iTextSharp.text.Image sigImg = iTextSharp.text.Image.GetInstance((byte[])dt.Rows[0]["SignatureImageFile"]);
sigImg.ScalePercent(22f);
sigImg.SetAbsolutePosition(130, 123);
over.AddImage(sigImg);
}
catch (Exception ex)
{
TCGHL.Framework.Common.Logging.Logger.LogEvent("BuildPdfW8E Error: ", ex);
}
}
break;
}
}
this.AppendBottomLine(pdfStamper, pdfReader.GetPageSize(1), dt.Rows[0]["BeneficialOwner"].ToString(), dt.Rows[0]["ModifiedDate"].ToString(), dt.Rows[0]["UTCHours"].ToString(), dt.Rows[0]["IPAddress"].ToString(), pdfReader.NumberOfPages);
try
{
pdfFormFields.SetField("f1_001", dt.Rows[0]["BeneficialOwner"].ToString());
pdfFormFields.SetField("f2_002", dt.Rows[0]["IncorporationCountry"].ToString());
pdfFormFields.SetField("f2_003", dt.Rows[0]["DisregardedEntity"].ToString());
//print Name
pdfFormFields.SetField("f8_31", dt.Rows[0]["BeneficialOwner"].ToString());
//pdf最后的日期
if (dt.Rows[0]["ESignType"] != System.DBNull.Value)
pdfFormFields.SetField("f8_32", DateTime.Now.ToString("MM-dd-yyyy"));
int EntityType = Convert.ToInt32(dt.Rows[0]["EntityType"]);
pdfFormFields.SetField("c1_001", EntityType.ToString());
switch (EntityType)
{
case 1:
pdfFormFields.SetField("c1_001[0]", "1");
break;
case 2:
pdfFormFields.SetField("c1_001[1]", "2");
break;
case 3:
pdfFormFields.SetField("c1_001[2]", "3");
break;
case 4:
pdfFormFields.SetField("c1_001[3]", "4");
break;
case 5:
pdfFormFields.SetField("c1_001[4]", "5");
break;
case 6:
pdfFormFields.SetField("c1_001[5]", "6");
break;
case 7:
pdfFormFields.SetField("c1_001[6]", "7");
break;
case 8:
pdfFormFields.SetField("c1_001[7]", "8");
break;
case 9:
pdfFormFields.SetField("c1_001[8]", "9");
break;
case 10:
pdfFormFields.SetField("c1_001[9]", "10");
break;
case 11:
pdfFormFields.SetField("c1_001[10]", "11");
break;
case 12:
pdfFormFields.SetField("c1_001[11]", "12");
break;
}
if (EntityType == 2 || EntityType == 3 || EntityType == 4 || EntityType == 5)
{
if (dt.Rows[0]["HybridTreaty"].ToString() != "")
{
int HybridTreaty = Convert.ToInt32(dt.Rows[0]["HybridTreaty"]);
if (HybridTreaty == 1)
{
pdfFormFields.SetField("c1_002[0]", "Yes");
pdfFormFields.SetField("c1_002", "Yes");
string TaxTreaty = dt.Rows[0]["TaxTreaty"].ToString();
if (TaxTreaty.ToLower().Contains("a"))
{
pdfFormFields.SetField("c2_006", "Yes");
}
if (TaxTreaty.ToLower().Contains("b"))
{
pdfFormFields.SetField("c2_007", "Yes");
//14小部分下面的复选框赋值
string PartDetail14 = dt.Rows[0]["PartDetail14"].ToString();
string PartDetail14Other = dt.Rows[0]["PartDetail14Other"].ToString();
int dtl14Option = 0;
if (!string.IsNullOrWhiteSpace(PartDetail14) && Int32.TryParse(PartDetail14, out dtl14Option))
{
//if (dtl14Option >= 1 && dtl14Option <= 10)
// pdfFormFields.SetField("c14_001", dtl14Option.ToString());
switch (dtl14Option)
{
case 1:
pdfFormFields.SetField("c14_001_1", "1");
break;
case 2:
pdfFormFields.SetField("c14_001_2", "2");
break;
case 3:
pdfFormFields.SetField("c14_001_3", "3");
break;
case 4:
pdfFormFields.SetField("c14_001_4", "4");
break;
case 5:
pdfFormFields.SetField("c14_001_5", "5");
break;
case 6:
pdfFormFields.SetField("c14_001_6", "6");
break;
case 7:
pdfFormFields.SetField("c14_001_7", "7");
break;
case 8:
pdfFormFields.SetField("c14_001_8", "8");
break;
case 9:
pdfFormFields.SetField("c14_001_9", "9");
break;
case 10:
pdfFormFields.SetField("c14_001_10", "10");
break;
}
}
if (!string.IsNullOrWhiteSpace(PartDetail14Other))
{
pdfFormFields.SetField("f14_001", PartDetail14Other);
}
}
if (TaxTreaty.ToLower().Contains("c"))
{
pdfFormFields.SetField("c2_008", "Yes");
}
pdfFormFields.SetField("f2_005", dt.Rows[0]["TaxTreatyCountry"].ToString());
pdfFormFields.SetField("f2_006", dt.Rows[0]["TaxTreatyEntry"].ToString());
pdfFormFields.SetField("f2_008", dt.Rows[0]["TaxTreatyType"].ToString());
pdfFormFields.SetField("f2_010", dt.Rows[0]["TaxTreatyReason"].ToString());
if (dt.Rows[0]["TaxTreatyRate"].ToString() != "")
{
pdfFormFields.SetField("f2_007", dt.Rows[0]["TaxTreatyRate"].ToString());
}
}
else
{
pdfFormFields.SetField("c1_002[1]", "No");
pdfFormFields.SetField("c1_002", "No");
}
}
}
int FATCAStatus = Convert.ToInt32(dt.Rows[0]["FATCAStatus"]);
//pdf复选框本身勾选赋值
if (FATCAStatus <= 13)
pdfFormFields.SetField("column1.c1_003", (FATCAStatus + 12).ToString());
else
pdfFormFields.SetField("column2.c1_003", (FATCAStatus + 12).ToString());
//用Chrome打开的pdf的复选框勾选赋值
switch (FATCAStatus)
{
case 1:
pdfFormFields.SetField("column1.c1_003[0]", "13");
break;
case 2:
pdfFormFields.SetField("column1.c1_003[1]", "14");
break;
case 3:
pdfFormFields.SetField("column1.c1_003[2]", "15");
break;
case 4:
pdfFormFields.SetField("column1.c1_003[3]", "16");
break;
case 5:
pdfFormFields.SetField("column1.c1_003[4]", "17");
break;
case 6:
pdfFormFields.SetField("column1.c1_003[5]", "18");
break;
case 7:
pdfFormFields.SetField("column1.c1_003[6]", "19");
break;
case 8:
pdfFormFields.SetField("column1.c1_003[7]", "20");
break;
case 9:
pdfFormFields.SetField("column1.c1_003[8]", "21");
break;
case 10:
pdfFormFields.SetField("column1.c1_003[9]", "22");
break;
case 11:
pdfFormFields.SetField("column1.c1_003[10]", "23");
break;
case 12:
pdfFormFields.SetField("column1.c1_003[11]", "24");
break;
case 13:
pdfFormFields.SetField("column1.c1_003[12]", "25");
break;
case 14:
pdfFormFields.SetField("column2.c1_003[0]", "26");
break;
case 15:
pdfFormFields.SetField("column2.c1_003[1]", "27");
break;
case 16:
pdfFormFields.SetField("column2.c1_003[2]", "28");
break;
case 17:
pdfFormFields.SetField("column2.c1_003[3]", "29");
break;
case 18:
pdfFormFields.SetField("column2.c1_003[4]", "30");
break;
case 19:
pdfFormFields.SetField("column2.c1_003[5]", "31");
break;
case 20:
pdfFormFields.SetField("column2.c1_003[6]", "32");
break;
case 21:
pdfFormFields.SetField("column2.c1_003[7]", "33");
break;
case 22:
pdfFormFields.SetField("column2.c1_003[8]", "34");
break;
case 23:
pdfFormFields.SetField("column2.c1_003[9]", "35");
break;
case 24:
pdfFormFields.SetField("column2.c1_003[10]", "36");
break;
case 25:
pdfFormFields.SetField("column2.c1_003[11]", "37");
break;
case 26:
pdfFormFields.SetField("column2.c1_003[12]", "38");
break;
case 27:
pdfFormFields.SetField("column2.c1_003[13]", "39");
break;
case 28:
pdfFormFields.SetField("column2.c1_003[14]", "40");
break;
case 29:
pdfFormFields.SetField("column2.c1_003[15]", "41");
break;
case 30:
pdfFormFields.SetField("column2.c1_003[16]", "42");
break;
case 31:
pdfFormFields.SetField("column2.c1_003[17]", "43");
break;
case 32:
pdfFormFields.SetField("column2.c1_003[18]", "44");
break;
}
if (FATCAStatus == 6)
{
//Part4,尚未取得全球中间机构识别号码的受赞助的外国金融机构
pdfFormFields.SetField("f2_011", dt.Rows[0]["SponsoredFFIName"].ToString());
int SponsoredFFI = Convert.ToInt32(dt.Rows[0]["SponsoredFFI"]);
if (SponsoredFFI == 1)
{
pdfFormFields.SetField("c2_009", "1");
}
else if (SponsoredFFI == 2)
{
pdfFormFields.SetField("c2_010", "2");
}
}
else if (FATCAStatus == 7)
{
//Part5,公认视同合规的非注册本地银行
int NonregisteringLocalBank = Convert.ToInt32(dt.Rows[0]["NonregisteringLocalBank"]);
if (NonregisteringLocalBank == 1)
{
pdfFormFields.SetField("c2_011", "Yes");
}
}
else if (FATCAStatus == 8)
{
//Part6,公认视同合规仅具低价值帐户的外国金融机构
int LowValueAccountsFFI = Convert.ToInt32(dt.Rows[0]["LowValueAccountsFFI"]);
if (LowValueAccountsFFI == 1)
{
pdfFormFields.SetField("c3_001", "Yes");
}
}
else if (FATCAStatus == 9)
{
//Part7,公认视同合规的受赞助且被紧密持有的投资工具
pdfFormFields.SetField("f3_001", dt.Rows[0]["InvestmentVehicleName"].ToString());
int InvestmentVehicle = Convert.ToInt32(dt.Rows[0]["InvestmentVehicle"]);
if (InvestmentVehicle == 1)
{
pdfFormFields.SetField("c3_002", "Yes");
}
}
else if (FATCAStatus == 10)
{
//Part8,公认视同合规的暂时性的债权投资法人
int InvestEntity = Convert.ToInt32(dt.Rows[0]["InvestEntity"]);
if (InvestEntity == 1)
{
pdfFormFields.SetField("c3_003", "Yes");
}
}
else if (FATCAStatus == 11)
{
//Part9,公认视同合规的投资顾问及投资经理
int InvestmentManagers = Convert.ToInt32(dt.Rows[0]["InvestmentManagers"]);
if (InvestmentManagers == 1)
{
pdfFormFields.SetField("c3_004", "Yes");
}
}
else if (FATCAStatus == 12)
{
//Part10,已提供所有人资讯之外国金融机构
string OwnerDocumentedFFI = dt.Rows[0]["OwnerDocumentedFFI"].ToString();
if (OwnerDocumentedFFI.ToLower().Contains("a"))
{
pdfFormFields.SetField("c3_005", "Yes");
}
if (OwnerDocumentedFFI.ToLower().Contains("b"))
{
pdfFormFields.SetField("c4_001", "1");
}
if (OwnerDocumentedFFI.ToLower().Contains("c"))
{
pdfFormFields.SetField("c4_002", "2");
}
if (OwnerDocumentedFFI.ToLower().Contains("d"))
{
pdfFormFields.SetField("c4_003", "2");
}
}
else if (FATCAStatus == 13)
{
//Part11,受限制的通路商
string RestrictedDistributor = dt.Rows[0]["RestrictedDistributor"].ToString();
if (RestrictedDistributor.ToLower().Contains("a"))
{
pdfFormFields.SetField("c4_004", "Yes");
}
if (RestrictedDistributor.ToLower().Contains("b"))
{
pdfFormFields.SetField("c4_005", "1");
}
if (RestrictedDistributor.ToLower().Contains("c"))
{
pdfFormFields.SetField("c4_006", "2");
}
}
else if (FATCAStatus == 14)
{
//Part12,在跨政府协议下免申报的外国金融机构
int NonreportingIGA = Convert.ToInt32(dt.Rows[0]["NonreportingIGA"]);
if (NonreportingIGA == 1)
{
pdfFormFields.SetField("c4_007", "Yes");
}
pdfFormFields.SetField("f4_001", dt.Rows[0]["NonreportingIGAContent1"].ToString());
pdfFormFields.SetField("f4_002", dt.Rows[0]["NonreportingIGAContent2"].ToString());
pdfFormFields.SetField("f4_003", dt.Rows[0]["NonreportingIGAGIIN"].ToString());
//新增的2个类型赋值
int modelType = 0, trusteeType = 0;
Int32.TryParse(dt.Rows[0]["PartDetail26ModelType"].ToString(), out modelType);
Int32.TryParse(dt.Rows[0]["PartDetail26TrusteeType"].ToString(), out trusteeType);
if (modelType > 0)
{
//pdfFormFields.SetField("c26_001", modelType.ToString());
switch (modelType)
{
case 1:
pdfFormFields.SetField("c26_001_1", "1");
break;
case 2:
pdfFormFields.SetField("c26_001_2", "2");
break;
}
}
if (trusteeType > 0)
{
//pdfFormFields.SetField("c26_002", trusteeType.ToString());
switch (trusteeType)
{
case 1:
pdfFormFields.SetField("c26_002_1", "1");
break;
case 2:
pdfFormFields.SetField("c26_002_2", "2");
break;
}
}
}
else if (FATCAStatus == 15)
{
//Part13,外国政府、美国属地政府或发行货币的外国中央银行
int ForeignGovernment = Convert.ToInt32(dt.Rows[0]["ForeignGovernment"]);
if (ForeignGovernment == 1)
{
pdfFormFields.SetField("c5_001", "Yes");
}
}
else if (FATCAStatus == 16)
{
//Part14,国际组织
int InternationalOrganization = Convert.ToInt32(dt.Rows[0]["InternationalOrganization"]);
if (InternationalOrganization == 1)
{
pdfFormFields.SetField("c5_002", "1");
}
else if (InternationalOrganization == 2)
{
pdfFormFields.SetField("c5_003", "2");
}
}
else if (FATCAStatus == 17)
{
//Part15,豁免的退休计划
int RetirementPlans = Convert.ToInt32(dt.Rows[0]["RetirementPlans"]);
if (RetirementPlans == 1)
{
pdfFormFields.SetField("c5_004", "1");
}
else if (RetirementPlans == 2)
{
pdfFormFields.SetField("c5_005", "2");
}
else if (RetirementPlans == 3)
{
pdfFormFields.SetField("c5_006", "3");
}
else if (RetirementPlans == 4)
{
pdfFormFields.SetField("c5_007", "4");
}
else if (RetirementPlans == 5)
{
pdfFormFields.SetField("c5_008", "5");
}
else if (RetirementPlans == 6)
{
pdfFormFields.SetField("c5_009", "6");
}
}
else if (FATCAStatus == 18)
{
//Part16,由免受扣缴之最终受益人完全持有之法人
int ExemptBeneficialOwners = Convert.ToInt32(dt.Rows[0]["ExemptBeneficialOwners"]);
if (ExemptBeneficialOwners == 1)
{
pdfFormFields.SetField("c6_001", "Yes");
}
}
else if (FATCAStatus == 19)
{
//Part17,美国属地金融机构
int FinancialInstitution = Convert.ToInt32(dt.Rows[0]["FinancialInstitution"]);
if (FinancialInstitution == 1)
{
pdfFormFields.SetField("c6_002", "Yes");
}
}
else if (FATCAStatus == 20)
{
//Part18,免受扣缴的非金融集团法人
int NonfinancialGroupEntity = Convert.ToInt32(dt.Rows[0]["NonfinancialGroupEntity"]);
if (NonfinancialGroupEntity == 1)
{
pdfFormFields.SetField("c6_003", "Yes");
}
}
else if (FATCAStatus == 21)
{
//Part19,免受扣缴的非金融新创公司
int NonfinancialCompany = Convert.ToInt32(dt.Rows[0]["NonfinancialCompany"]);
if (NonfinancialCompany == 1)
{
pdfFormFields.SetField("c6_007_0_", "Yes");
}
pdfFormFields.SetField("f6_001", Convert.ToDateTime(dt.Rows[0]["NonfinancialCompanyDate"]).ToString("MM-dd-yyyy"));
}
else if (FATCAStatus == 22)
{
//Part20,免受扣缴的清算中或破产的非金融法人
int NonfinancialLiquidation = Convert.ToInt32(dt.Rows[0]["NonfinancialLiquidation"]);
if (NonfinancialLiquidation == 1)
{
pdfFormFields.SetField("c6_008", "Yes");
}
pdfFormFields.SetField("f6_002", Convert.ToDateTime(dt.Rows[0]["NonfinancialLiquidationDate"]).ToString("MM-dd-yyyy"));
}
else if (FATCAStatus == 23)
{
//Part21,符合第501(c)节规范的组织
int Organization501c = Convert.ToInt32(dt.Rows[0]["Organization501c"]);
if (Organization501c == 1)
{
pdfFormFields.SetField("c6_009", "Yes");
}
pdfFormFields.SetField("f6_003", Convert.ToDateTime(dt.Rows[0]["Organization501cDate"]).ToString("MM-dd-yyyy"));
}
else if (FATCAStatus == 24)
{
//Part22,非营利组织
int NonProfitOrganization = Convert.ToInt32(dt.Rows[0]["NonProfitOrganization"]);
if (NonProfitOrganization == 1)
{
pdfFormFields.SetField("c6_010", "Yes");
}
}
else if (FATCAStatus == 25)
{
//Part23,股票在证券市场正常交易的非金融外国法人或其关系企业
int TradedCorporation = Convert.ToInt32(dt.Rows[0]["TradedCorporation"]);
if (TradedCorporation == 1)
{
pdfFormFields.SetField("c7_001", "1");
pdfFormFields.SetField("f7_001", dt.Rows[0]["TradedMarketName"].ToString());
}
else if (TradedCorporation == 2)
{
pdfFormFields.SetField("c7_002", "2");
pdfFormFields.SetField("f7_002", dt.Rows[0]["TradedCorporationName"].ToString());
pdfFormFields.SetField("f7_003", dt.Rows[0]["TradedMarketName"].ToString());
}
}
else if (FATCAStatus == 26)
{
//Part24,免受扣缴的美国属地非金融外国法人
int TerritoryNFFE = Convert.ToInt32(dt.Rows[0]["TerritoryNFFE"]);
if (TerritoryNFFE == 1)
{
pdfFormFields.SetField("c7_003", "Yes");
}
}
else if (FATCAStatus == 27)
{
//Part25,积极的非金融外国法人
int ActiveNFFE = Convert.ToInt32(dt.Rows[0]["ActiveNFFE"]);
if (ActiveNFFE == 1)
{
pdfFormFields.SetField("c7_004", "Yes");
}
}
else if (FATCAStatus == 28)
{
//Part26,消极的非金融外国法人
string PassiveNFFE = dt.Rows[0]["PassiveNFFE"].ToString();
if (PassiveNFFE.ToLower().Contains("a"))
{
pdfFormFields.SetField("c7_005", "Yes");
}
if (PassiveNFFE.ToLower().Contains("b"))
{
pdfFormFields.SetField("c7_006", "1");
}
if (PassiveNFFE.ToLower().Contains("c"))
{
pdfFormFields.SetField("c7_007", "2");
pdfFormFields.SetField("f8_002", dt.Rows[0]["USOwnerName1"].ToString());
pdfFormFields.SetField("f8_003", dt.Rows[0]["USOwnerAddress1"].ToString());
pdfFormFields.SetField("f8_004", dt.Rows[0]["USOwnerTIN1"].ToString());
pdfFormFields.SetField("f8_006", dt.Rows[0]["USOwnerName2"].ToString());
pdfFormFields.SetField("f8_007", dt.Rows[0]["USOwnerAddress2"].ToString());
pdfFormFields.SetField("f8_008", dt.Rows[0]["USOwnerTIN2"].ToString());
pdfFormFields.SetField("f8_010", dt.Rows[0]["USOwnerName3"].ToString());
pdfFormFields.SetField("f8_011", dt.Rows[0]["USOwnerAddress3"].ToString());
pdfFormFields.SetField("f8_012", dt.Rows[0]["USOwnerTIN3"].ToString());
pdfFormFields.SetField("f8_014", dt.Rows[0]["USOwnerName4"].ToString());
pdfFormFields.SetField("f8_015", dt.Rows[0]["USOwnerAddress4"].ToString());
pdfFormFields.SetField("f8_016", dt.Rows[0]["USOwnerTIN4"].ToString());
pdfFormFields.SetField("f8_018", dt.Rows[0]["USOwnerName5"].ToString());
pdfFormFields.SetField("f8_019", dt.Rows[0]["USOwnerAddress5"].ToString());
pdfFormFields.SetField("f8_020", dt.Rows[0]["USOwnerTIN5"].ToString());
pdfFormFields.SetField("f8_022", dt.Rows[0]["USOwnerName6"].ToString());
pdfFormFields.SetField("f8_023", dt.Rows[0]["USOwnerAddress6"].ToString());
pdfFormFields.SetField("f8_024", dt.Rows[0]["USOwnerTIN6"].ToString());
pdfFormFields.SetField("f8_026", dt.Rows[0]["USOwnerName7"].ToString());
pdfFormFields.SetField("f8_027", dt.Rows[0]["USOwnerAddress7"].ToString());
pdfFormFields.SetField("f8_028", dt.Rows[0]["USOwnerTIN7"].ToString());
pdfFormFields.SetField("f8_030", dt.Rows[0]["USOwnerName8"].ToString());
pdfFormFields.SetField("f8_031", dt.Rows[0]["USOwnerAddress8"].ToString());
pdfFormFields.SetField("f8_032", dt.Rows[0]["USOwnerTIN8"].ToString());
pdfFormFields.SetField("f8_034", dt.Rows[0]["USOwnerName9"].ToString());
pdfFormFields.SetField("f8_035", dt.Rows[0]["USOwnerAddress9"].ToString());
pdfFormFields.SetField("f8_036", dt.Rows[0]["USOwnerTIN9"].ToString());
}
}
else if (FATCAStatus == 29)
{
//Part27,免受扣缴的关系企业间的外国金融机构
int InterAffiliateFFI = Convert.ToInt32(dt.Rows[0]["InterAffiliateFFI"]);
if (InterAffiliateFFI == 1)
{
pdfFormFields.SetField("c3_008", "Yes");
}
}
else if (FATCAStatus == 31)
{
//Part28,受赞助的直接申报的非金融外国法人
pdfFormFields.SetField("Page8.f4_002", dt.Rows[0]["DirectReportingNFFEName"].ToString());
int DirectReportingNFFE = Convert.ToInt32(dt.Rows[0]["DirectReportingNFFE"]);
if (DirectReportingNFFE == 1)
{
pdfFormFields.SetField("Page8.c3_008", "Yes");
}
}
pdfFormFields.SetField("f1_005", dt.Rows[0]["ResidenceAddress1"].ToString());
pdfFormFields.SetField("f1_006", dt.Rows[0]["ResidenceAddress2"].ToString());
pdfFormFields.SetField("f1_007", dt.Rows[0]["ResidenceAddressCountry"].ToString());
pdfFormFields.SetField("f1_008", dt.Rows[0]["MailingAddress1"].ToString());
pdfFormFields.SetField("f1_009", dt.Rows[0]["MailingAddress2"].ToString());
pdfFormFields.SetField("f1_010", dt.Rows[0]["MailingAddressCountry"].ToString());
pdfFormFields.SetField("f1_011", dt.Rows[0]["TaxpayerIdentificationNumber"].ToString());
pdfFormFields.SetField("f1_013", dt.Rows[0]["ReferenceNumber"].ToString());
string GIIN = dt.Rows[0]["GIIN"].ToString().Trim();
if (GIIN != "")
{
//pdfFormFields.SetField("c1_004", "1");
pdfFormFields.SetField("f1_012", GIIN);
}
string ForeignTIN = dt.Rows[0]["ForeignTIN"].ToString().Trim();
if (ForeignTIN != "")
{
//pdfFormFields.SetField("c1_005", "2");
pdfFormFields.SetField("f1_012[1]", ForeignTIN);
}
if (dt.Rows[0]["EntityFATCAStatus"].ToString().Trim() != "")
{
int EntityFATCAStatus = Convert.ToInt32(dt.Rows[0]["EntityFATCAStatus"]);
switch (EntityFATCAStatus)
{
case 1:
pdfFormFields.SetField("c2_001", "1");
break;
case 2:
pdfFormFields.SetField("c2_003", "2");
break;
case 3:
pdfFormFields.SetField("c2_005", "2");
break;
case 4:
pdfFormFields.SetField("c2_002", "3");
break;
case 5:
pdfFormFields.SetField("c2_004", "4");
break;
}
}
pdfFormFields.SetField("f2_001", dt.Rows[0]["EntityAddress1"].ToString());
pdfFormFields.SetField("Page2.f2_002", dt.Rows[0]["EntityAddress2"].ToString());
pdfFormFields.SetField("Page2.f2_003", dt.Rows[0]["EntityCountry"].ToString());
pdfFormFields.SetField("f2_004", dt.Rows[0]["EntityGIIN"] == System.DBNull.Value ? string.Empty : dt.Rows[0]["EntityGIIN"].ToString());
int Certification = Convert.ToInt32(dt.Rows[0]["Certification"]);
if (Certification == 1)
{
pdfFormFields.SetField("c8_001", "2");
}
pdfStamper.FormFlattening = false;
pdfStamper.Close();
byte[] bytes = memoryStream.GetBuffer();
memoryStream.Close();
return bytes;
}
catch (Exception ex)
{
throw ex;
}
finally
{
memoryStream.Close();
memoryStream.Dispose();
}
}
/// <summary>
/// 拼接BSNOTE表单PDF
/// </summary>
/// <param name="PdfFilePath"></param>
/// <param name="dt"></param>
/// <returns></returns>
private byte[] BuildPdfBSNOTE(string PdfFilePath, DataTable dt)
{
PdfReader pdfReader = new PdfReader(PdfFilePath);
MemoryStream memoryStream = new MemoryStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, memoryStream);
AcroFields pdfFormFields = pdfStamper.AcroFields;
try
{
var font = GetBaseFont();
foreach (KeyValuePair<string, AcroFields.Item> Item in pdfReader.AcroFields.Fields)
{
string ParamName = Item.Key;
pdfFormFields.SetFieldProperty(ParamName, "textfont", font, null);
}
pdfFormFields.SetField("SOLD_NOTE", dt.Rows[0]["PurchaserName"].ToString());
pdfFormFields.SetField("BOUGHT_NOTE", dt.Rows[0]["SellerName"].ToString());
pdfFormFields.SetField("Name_of_Company_in_which_the_shares_to_be_transferred", dt.Rows[0]["CompanyName"].ToString());
pdfFormFields.SetField("Name_of_Company_in_which_the_shares_to_be_transferred_2", dt.Rows[0]["CompanyName"].ToString());
pdfStamper.FormFlattening = false;
pdfStamper.Close();
byte[] bytes = memoryStream.GetBuffer();
memoryStream.Close();
return bytes;
}
catch (Exception ex)
{
throw ex;
}
finally
{
memoryStream.Close();
memoryStream.Dispose();
}
}
#endregion
private BaseFont GetBaseFont()
{
BaseFont _baseFont = null;
try
{
var file1 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"biniTextAsian.dll");
var file2 = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"biniTextAsian.dll");
if (File.Exists(file1)) { BaseFont.AddToResourceSearch(Assembly.LoadFile(file1)); }
if (File.Exists(file2)) { BaseFont.AddToResourceSearch(Assembly.LoadFile(file2)); }
if (!File.Exists(file1) || !File.Exists(file2))
{
throw new FileNotFoundException("Can't find iTextAsian.dll or iTextAsianCmaps.dll");
}
_baseFont = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, false);
}
catch (Exception ex2)
{
TCGHL.Framework.Common.Logging.Logger.LogEvent("pdf basefont", ex2);
//var ttfPath = @"C:WindowsFontsSTSONG.ttf";
var ttfPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"..FontsSTSONG.ttf";
if (File.Exists(ttfPath))
{
try
{
_baseFont = BaseFont.CreateFont(ttfPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}
catch (Exception ex3)
{
_baseFont = BaseFont.CreateFont(ttfPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
}
else
{
throw new FileNotFoundException(@"Can't find C:WindowsFontsSTSONG.ttf");
}
}
return _baseFont;
}
}
}