Link to home
Start Free TrialLog in
Avatar of Crystal Rouse
Crystal RouseFlag for United States of America

asked on

Exporting data using iTextSharp cuts off text fields.

We use iTextSharp to create PDFs throughout our site.  We have a Comments field that can grow.
It doesn't look like iTextSharp supports dynamic attributes - or at least the way we are using it with a pre-defined template.

sample of the code for the comments section:

  stamper.AcroFields.SetField("Comments", sb.ToString());
  stamper.AcroFields.SetFieldProperty("Comments", "textsize", 6f, null);
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi Crystal,

I think I responded to this question but it seems ... I am getting old n missing out stuff :P or maybe my laptop crashed in between.

Is it possible for you to share a sample template and code that you are using. No need to send everything, just post the relevant code and I will try to figure it out.

This generally happens when the engine is not able to figure out some aspects of the font/s used. I would also suggest to copy the comments and put them in a texblock (and remove the field entirely) before you flatten the PDF.

Regards,
Chinmay.
Avatar of Crystal Rouse

ASKER

Thanks for any help or tips or suggestions!  I've attached a sample form with most of the data removed except for the Comments Field.

Here is more of the code for the pdf.cs
using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace master.MYSITE.Models
{
    public class PDF
    {
        private myDB myDB = new myDB();

        public PDF(int id)
        {
            request = (from x in myDB.tbl_Request
                       where x.ID == id
                       select x).Single();
        }

        private tbl_Request request { get; set; }

        public MemoryStream form()
        {
            using (PdfReader reader = new PdfReader(System.Web.HttpContext.Current.Server.MapPath("~/mypath/Form.pdf")))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (PdfStamper stamper = new PdfStamper(reader, ms))
                    {
                        stamper.AcroFields.SetField("Title", request.Title);
                        stamper.AcroFields.SetFieldProperty("Title", "textsize", 8f, null);
                                               
                         StringBuilder sb = new StringBuilder();

                        foreach (tbl_Note note in request.tbl_Note.OrderByDescending(x => x.OriginationDate))
                        {
                            sb.Append(note.OriginatorString() + " ");
                            sb.Append(note.dateString() + " - ");
                            sb.Append(note.Content + "\n" + "\n");
                        }

                        stamper.AcroFields.SetField("Comments", sb.ToString());
                        stamper.AcroFields.SetFieldProperty("Comments", "textsize", 6f, null);
                                                                    
                        stamper.FormFlattening = true;

                        return ms;
                    }
                }
            }
        }

        public MemoryStream fullPDF()
        {
            var final = new MemoryStream();

            MemoryStream formMS = new MemoryStream(form().ToArray());
          
            var copy = new PdfCopyFields(final);

            formMS.Position = 0;
            copy.AddDocument(new PdfReader(formMS));
            formMS.Dispose();

            copy.AddDocument(new PdfReader());

            copy.Close();

            return final;
        }

        }
    }
}

My Controller:

[HttpGet]
		public FileStreamResult DownloadPDF(int id)
		{
			tbl_Request cr = (from x in myDB.tbl_Request
									where x.ID == id
									select x).Single();

			PDF pdf = new PDF(id);

			MemoryStream stream = new MemoryStream(pdf.fullPDF().ToArray());
            string pdfTitle = PDF_NAME + ".pdf";
            return File(stream, "application/pdf", pdfTitle);
		}

Open in new window

Here is the attached sample form.
Form.pdf
Hi Crystal,

I tried with your code - got rid of lot of stuff as I was doing it from a WPF app.

Here is my code and I could reproduce what you are facing but I am not sure if we have a way out.

using (PdfReader reader = new PdfReader(@"Form.pdf"))
            {
                using (PdfStamper stamper = new PdfStamper(reader, new FileStream("Test.pdf", FileMode.Create)))
                {
                    stamper.AcroFields.SetField("Title", "Title");
                    stamper.AcroFields.SetFieldProperty("Title", "textsize", 8f, null);

                    StringBuilder sb = new StringBuilder();

                    for (int i = 0; i < 100; i++)
                    {
                        sb.Append("Line 1");
                    }
                    sb.Append("Line 2" + Environment.NewLine);
                    sb.Append("Line 3" + Environment.NewLine);
                    sb.Append("Line 4" + Environment.NewLine);

                    stamper.AcroFields.SetField("Comments", sb.ToString());
                    stamper.AcroFields.SetFieldProperty("Comments", "textsize", 0f, null);
                    stamper.AcroFields.GetFieldItem("Comments");

                    stamper.FormFlattening = true;
                }
            }

Open in new window


1. Do you think the generated PDFs will be needed for something else? I mean even when you flatten the field, it is still a field so be design, Acrobat locks it out.
2. If you don't flatten the file, you can see how it works.

Now, the easy way out is to create a text block, draw a  border around it - copy content of the Comments field and remove the field.

Is that something that will work for you? Otherwise I think it will be better to raise it to their support page.  Another thing you could try is to resize the field before flattening.

Regards,
Chinmay.
Thanks I appreciate this!  I'm still working on it and it may be awhile since it's not a high priority task for me right now. I'm basically researching to see if I can do anything about it.  I'll close this question for now and may re-open another one later if your suggestions don't work for me.

Thanks again!
Fair enough. The textblock will definitely work - I assure you. I am just not sure if there is any other processing that needs to be done after the flattening of the fields. If that is the case, then we might have to come up with something clever.
Thanks!  I'll let you know what I figure out.  Hoping to get to work on this again soon.
ASKER CERTIFIED SOLUTION
Avatar of Crystal Rouse
Crystal Rouse
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial