0
我有一個從HTML頁面接受數據,並將該數據標記到PDF並將PDF保存到第三方成像系統的過程。iTextSharp - 填寫PDF多次併合併爲一個pdf
該HTML頁面接受最多8個用戶的信息。但是,PDF只能容納4個用戶。因此,如果輸入的信息超過4個,我需要填寫PDF兩次並將這兩個PDF合併到一個文件中以提交給成像系統。
我試過嵌套流,pdfReaders,pdfStampers,但最終的PDF總是損壞。任何幫助將不勝感激。提前致謝。
下面的代碼我目前是處理衝壓PDF如果有1 - 4用戶進入:
var pdfTemplate = ConfigurationManager.AppSettings["PdfFileLocation"] + formName + ".pdf";
var pdfReader = new PdfReader(pdfTemplate);
using (var stream = new MemoryStream())
{
using (var pdfStamper = form == null
? new PdfStamper(pdfReader, stream, '\0', false)
: processType == 1 // printing form
? new PdfStamper(pdfReader, new FileStream(Path.GetTempPath() + "UserForm_" + UserNumber + ".pdf", FileMode.Create)) // temporarily save form
: new PdfStamper(pdfReader, stream, '\0', false)) // use a stream if submitting
{
var overContent1 = pdfStamper.GetOverContent(1);
var overContent2 = pdfStamper.GetOverContent(2);
var overContent3 = pdfStamper.GetOverContent(3);
var overContent4 = pdfStamper.GetOverContent(4);
var pdfFormFields = pdfStamper.AcroFields;
... Code to fill out other pages of the PDF goes here ...
#region User Form Page #3
foreach (var user in users)
{
var eligibility = _sharedFormService.GetEligibility(user.Eligibility);
pdfFormFields.SetField("User" + nameof(user.Name) + user.Id, user.Name);
pdfFormFields.SetField("User" + nameof(user.Eligibility) + user.Id, eligibility);
pdfFormFields.SetField("User" + nameof(user.Title) + user.Id, user.Title);
pdfFormFields.SetField("User" + nameof(user.Type) + user.Id, user.Type);
pdfFormFields.SetField("User" + nameof(user.FamilyMemberName) + user.Id, user.FamilyMemberName);
try
{
if (user.Signature != null)
{
var sigImage = _imageHelpers.LoadImage(user.Signature);
var image = Image.GetInstance(sigImage, System.Drawing.Imaging.ImageFormat.Gif);
var sigPostitions = pdfFormFields.GetFieldPositions("User" + nameof(user.Signature) + user.Id)[0].position;
image.Transparency = new int[] { 255, 255 };
image.SetAbsolutePosition(sigPostitions.Left, sigPostitions.Bottom);
image.ScalePercent(15);
overContent3.AddImage(image);
}
}
catch (DocumentException dex)
{
response.Success = false;
response.ErrorMessage += "StampPdf - UserSignature" + user.Id + ": " + (dex.InnerException != null
? dex.InnerException.Message
: dex.Message) + ", ";
}
catch (IOException ioex)
{
response.Success = false;
response.ErrorMessage += "StampPdf - UserSignature" + user.Id + ": " + (ioex.InnerException != null
? ioex.InnerException.Message
: ioex.Message) + ", ";
}
}
#endregion
pdfStamper.FormFlattening = true;
}
response.Stream = stream.ToArray();
}