PdfStamper未将新文本添加到辅助PDF中

问题描述 投票:0回答:1

我正在尝试将新文本添加到现有的PDF文件,但未添加。在下面的代码中,它没有显示任何错误,但未添加文本。

我也在下面看了一些例子Example1Example2

如果有什么我做不正确的事情,您能指导我吗?

这是用于将文本写入pdf的代码。

      else
        {
            if (document.State != DocumentState.Signed)
                document.State = DocumentState.Signed;

            document.ActionedUser = user;
            document.ActionDate = DateTime.Now;

      //this return bytes and it changes to document.SignedFileData = memoryStream.ToArray() and that makes it to loose original data
            document.SignedFileData = response.Document.SignedFileBytes;
            #region

            int numberOfPages;

            // create a MemoryStream to write the stamping result to
            using (MemoryStream memoryStream = new MemoryStream())
            {
                //create PdfReader object to read from the existing document
                using (PdfReader reader = new PdfReader(document.EditedFileData))
                // create a PdfStamper object to manipulate the PDF in the reader and write to the MemoryStream 
                using (PdfStamper stamper = new PdfStamper(reader, memoryStream))
                {
                    numberOfPages = reader.NumberOfPages;
                    reader.SelectPages("1-100");

                    // PdfContentByte from stamper to add content to the pages over the original content
                    PdfContentByte pbover = stamper.GetOverContent(numberOfPages);
                    iTextSharp.text.Font font = new iTextSharp.text.Font(null, 10, iTextSharp.text.Font.NORMAL, BaseColor.RED);

                    string FisrtName = "Testing";
                    string Position = "Testing";
                    string Signature = "Testing"; ;
                    string SignatureDate = DateTime.Now.ToString();

                    ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase(FisrtName, font), 240, 715, 0);
                    ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, 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);
                }
            }

            #endregion
            // Store the manipulated PDF in the EditedFileData property
            document.SignedFileData = memoryStream.ToArray();


            Context.SaveChanges();
            return new SignatureSoapResponse() { Success = true, Message = document.Id.ToString() };
        }

PDF表enter image description here

c# pdf itext pdfstamper
1个回答
0
投票

在评论过程中,发现操纵的PDF将存储在EditedFileDataPDFFile document属性中。

OP的代码改为使用奇怪的名称将结果PDF存储在文件系统中的某个位置:

new FileStream(Convert.ToBase64String(document.EditedFileData), FileMode.Create))

PdfStamper的此目标流获取document.EditedFileData属性的当前内容,对其进行base64编码,并将其用作要写入PDF的文件系统中文件的名称。

要在document.EditedFileData属性中存储经过处理的PDF,应改为使用MemoryStream作为PdfStamper的目标流,并最终将内存流内容放入相关属性,即:

// create a MemoryStream to write the stamping result to
using (MemoryStream memoryStream = new MemoryStream())
{
    //create PdfReader object to read from the existing document
    using (PdfReader reader = new PdfReader(document.OriginalFileData))
    // create a PdfStamper object to manipulate the PDF in the reader and write to the MemoryStream 
    using (PdfStamper stamper = new PdfStamper(reader, memoryStream))
    {
        numberOfPages = reader.NumberOfPages;
        reader.SelectPages("1-100");

        // PdfContentByte from stamper to add content to the pages over the original content
        PdfContentByte pbover = stamper.GetOverContent(numberOfPages);
        iTextSharp.text.Font font = new iTextSharp.text.Font(null, 16, iTextSharp.text.Font.BOLD, BaseColor.RED);

        string FisrtName = "Director1";
        string Position = "Director1";
        string Signature = "Director1";
        string SignatureDate = DateTime.Now.ToString();

        ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, new Phrase(FisrtName, font), 230, 650, 0);
        ColumnText.ShowTextAligned(pbover, Element.ALIGN_LEFT, 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);
    }

    // Store the manipulated PDF in the EditedFileData property
    document.EditedFileData = memoryStream.ToArray();
}
© www.soinside.com 2019 - 2024. All rights reserved.