我正在开发一个应用程序,该应用程序从另一个系统接收包含内容(数据)的 PDF 文件,供客户进行数字签名。我的任务是在签名后添加客户详细信息和时间戳,而不会丢失当前数据或创建新的 pdf 文件。(日期时间、姓名、姓氏等)。
我在测试应用程序上遵循了一些示例(见下文),它工作正常。
问题是它正在寻找一个不是我想要的新文件。
如何在不创建新 pdf 的情况下修改/添加文本到现有 pdf 文件?在搜索了两天后,我对如何归档这个有点迷茫
文件被编码
var file = new PDFFile();
------
------
file.OriginalFileData = Convert.FromBase64String(model.FileData);
//Model
public string FileData { get; set; }
PDF 文件实体
public class PDFFile
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]
public int Id { get; set; }
public string FileName { get; set; }
public string Description { get; set; }
public DateTime DateCreated { get; set; }
public string OriginatingSystem { get; set; }
public string OriginatorName { get; set; }
public string FileLocator { get; set; }
public DateTime? ExpiryDate { get; set; }
public Customer Customer { get; set; }
[InverseProperty("AssignedDocuments")]
public ApplicationUser User { get; set; }
public int NumPages { get; set; }
public DocumentState State { get; set; }
[InverseProperty("ActionedDocuments")]
public ApplicationUser ActionedUser { get; set; }
public DateTime? ActionDate { get; set; }
public byte[] OriginalFileData { get; set; }
public byte[] EditedFileData { get; set; }
public byte[] SignedFileData { get; set; }
public virtual ICollection<PDFFileMetaData> Metadata { get; set; }
}
//PDFFileMetaData
public class PDFFileMetaData
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity), Key]
public int Id { get; set; }
[InverseProperty("Metadata")]
public PDFFile Document { get; set; }
public string Key { get; set; }
public string Data { get; set; }
}
我的代码:
public SoapResponse SignCustomerDocument(Customer customer, PDFFile document, ApplicationUser user)
{
var logger = log4net.LogManager.GetLogger(this.GetType());
string timePoint = DateTime.Now.ToString("yyyyMMdd'T'HHmmss'.'fffffff");
string transactionId = "SP_" + timePoint + ".D" + document.Id + ".C" + customer.Id;
logger.InfoFormat("Sending signing request {0}", transactionId);
var signReq = new PdfSigningRequest();
signReq.Echo = transactionId;
signReq.TransactionId = transactionId;
if (signReq.Document == null)
signReq.Document = new PdfSigningDocument();
byte[] fileData = PDFEditorHelper.EditPdf(document, document.OriginalFileData, user.InitialImage);
byte[] editedPDF = this.EditSignablePDF(document, fileData);
signReq.Document.FileBytes = editedPDF;
signReq.Document.Identifier = transactionId;
if (signReq.Document.PdfSigningParameters == null)
signReq.Document.PdfSigningParameters = new PdfSigningParameters();
//variables
int numberOfPages;
//create PdfReader object to read from the existing document
using (PdfReader reader = new PdfReader(document.OriginalFileData))
//create PdfStamper object to write to get the pages from reader
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(Convert.ToString(document.EditedFileData), FileMode.Create)))
{
numberOfPages = reader.NumberOfPages;
//select two pages from the original document
reader.SelectPages("1-100");
//gettins the page size in order to substract from the iTextSharp coordinates
var pageSize = reader.GetPageSize(1);
// PdfContentByte from stamper to add content to the pages over the original content
PdfContentByte pbover = stamper.GetOverContent(18);
//add content to the page using ColumnText
iTextSharp.text.Font font = new iTextSharp.text.Font();
font.Size = 12;
//setting up the X and Y coordinates of the document
System.Drawing.Point point = new Point();
int x = point.X;
int y = point.Y;
y = (int)(pageSize.Height - y);
string FisrtName = "Test1";
string Position = "Test2";
string Signature = "Test3";
string SignatureDate = DateTime.Now.ToString();
ColumnText.ShowTextAligned(pbover, Element.ALIGN_UNDEFINED, new Phrase(FisrtName, font), 230, 650, 0);
ColumnText.ShowTextAligned(pbover, Element.ALIGN_JUSTIFIED, new Phrase(Position, font), 230, 628, 0);
ColumnText.ShowTextAligned(pbover, PdfContentByte.ALIGN_LEFT, new Phrase(Signature, font), 230, 600, 0);
ColumnText.ShowTextAligned(pbover, PdfContentByte.ALIGN_LEFT, new Phrase(SignatureDate, font), 230, 574, 0);
}
}
我在上面的代码中得到的错误: