C# PdfStamper未向PDF添加新文本

C# PdfStamper未向PDF添加新文本,c#,pdf,itext,pdfstamper,C#,Pdf,Itext,Pdfstamper,我试图添加新的文本到现有的PDF文件,但它没有添加。从下面的代码中,它不会显示任何错误,但不会添加文本 我还看了下面的一些例子 如果有什么事情我做得不对,你能指导我吗 这是我用来将文本写入pdf的代码。 else { if (document.State != DocumentState.Signed) document.State = DocumentState.Signed; do

我试图添加新的文本到现有的PDF文件,但它没有添加。从下面的代码中,它不会显示任何错误,但不会添加文本

我还看了下面的一些例子

如果有什么事情我做得不对,你能指导我吗

这是我用来将文本写入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表格

在评论过程中,被操纵的PDF被存储在
PDFFile文档的
EditedFileData
属性中

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

new FileStream(Convert.ToBase64String(document.EditedFileData), FileMode.Create))
PdfStamper
的此目标流获取
文档的当前内容。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();
}

您知道您正在将结果存储在一个文件名奇怪的文件中!?此外,
System.Drawing.Point
似乎没有零参数构造函数,因此在
System.Drawing.Point=new Point()中实例化哪个
Point
?@mkl我不知道,你能开车送我到正确的方向吗?我正在使用System.Drawing.Point和页面大小。我不确定这是否是错误的,或者是否有更好的方法。
Point
类的问题只是一个次要问题,因为您实际上忽略了
Point
x
y
,您的
ColumnText.ShowTextAligned
调用具有硬编码坐标。更重要的是另一个观察结果,您的代码没有将结果存储在
PDFFile文档
对象中,而是存储在当前文件夹中的一个文件中,该文件的名称很奇怪,可能太长了。让我们从这一点开始,您的意思是我应该更改并使其与此ColumnText类似。ShowTextAligned(pbover,Element.ALIGN\u LEFT,new短语(FirstName,字体),point.X,point.Y,0);“让我们从点开始,你的意思是我应该改变…”-不,我的意思是你应该解释你在
System.Drawing.point=new point()中实例化的
point
类因为据我所知,
System.Drawing.Point
没有零参数构造函数。除非
new Point()
引用了另一个类。我添加了此代码,但它似乎没有写入文档。我尝试调试以检查是否传递了一些空值。我在阅读了一个示例后尝试添加此代码,但无效。stamper.Writer.CloseStream=true;document.EditedFileData=memoryStream.ToArray();压模关闭();在母版关闭之前调用
ToArray
,没有任何意义,您将得到一个不完整的pdf。我测试了答案中的代码,它更新了
文档。EditedFileData
带有三份Director1副本的pdf。如果您的情况不是这样,则会出现其他问题。您在
文档中得到pdf吗.EditedFileData
?它与
文档.OriginalFileData
中的文件不同吗?(检查文件大小和内容。)我得到的文档与原始文档不同,但没有新添加的文本。我可以查看签名文档,但没有测试,我添加了,肯定有什么事情我无法理解。请放下Skype手柄,以便我们可以继续聊天,如果可以的话。请共享一个示例输入和输出文档请不要说明问题以供分析。对不起,没有免费的私人咨询。