如何在不创建新PDF文件的情况下向现有PDF文件添加新文本

时间:2020-05-14 07:30:03

标签: c# pdf itext pdfstamper

我正在开发一个应用程序,该应用程序从另一个系统接收带有内容(数据)的PDF文件,以供客户进行数字签名。我的任务是在签名后添加客户详细信息和时间戳,而不会丢失当前数据或不创建新的pdf文件。 (日期时间,名称,姓氏等)。

我在测试应用程序上遵循了一些示例(请参见下文),并且工作正常。

How to update a PDF without creating a new PDF?

ITextSharp insert text to an existing 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);

        }
  }

我在上面的代码上遇到的错误:

enter image description here

0 个答案:

没有答案