PdfStamper not adding new text to assisting PDF

972 Views Asked by At

I am trying to add new text to an existing PDF file but it is not adding. From the code below it doesn't show any error but the text is not added.

I have also looked at some examples below Example1 Example2

Can you please guide me if there's something I am not doing it right?

This is the code am using to write text to the 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 Table enter image description here

1

There are 1 best solutions below

18
mkl On

In the course of the comments it turned out that the manipulated PDF is to be stored in the EditedFileData property of the PDFFile document.

The OP's code instead stored the result PDF somewhere in the file system using weird names:

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

This target stream of the PdfStamper takes the current content of the document.EditedFileData property, base64 encodes it, and uses it as name of a file in the file system to which the PDF is written.

To store the manipulated PDF in the document.EditedFileData property, one should instead use a MemoryStream as target stream of the PdfStamper and eventually put the memory stream contents into the property in question, i.e.:

// 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();
}