-2

I want to return the Document object from below code. At present I get a document has no pages exception.

private static Document GeneratePdfAcroFields(PdfReader reader, Document docReturn)
    {

         if (File.Exists(System.Configuration.ConfigurationSettings.AppSettings["TEMP_PDF"]))
            File.Delete(System.Configuration.ConfigurationSettings.AppSettings["TEMP_PDF"]);


        PdfStamper stamper = new PdfStamper(reader, new FileStream(System.Configuration.ConfigurationSettings.AppSettings["TEMP_PDF"],FileMode.Create));
        AcroFields form = stamper.AcroFields;

        ///INSERTING TEXT DYNAMICALLY JUST FOR EXAMPLE.
        form.SetField("topmostSubform[0].Page16[0].topmostSubform_0_\\.Page78_0_\\.TextField3_9_[0]", "This value was dynamically added.");
        stamper.FormFlattening = false;

        stamper.Close();

        FileStream fsRead = new FileStream(System.Configuration.ConfigurationSettings.AppSettings["TEMP_PDF"], FileMode.Open);

        Document docret = new Document(reader.GetPageSizeWithRotation(1));

        return docret;
    }
DevXR
  • 3
  • 2
  • Your question doesn't really make sense. You are using `PdfStamper` to fill out fill and maybe to stamp some text on an existing PDF. So far, so good. But why do you need a `Document` instance? `Document` is for creating PDFs from scratch. It should be obvious that the document you're creating from scratch had no pages. – Bruno Lowagie Dec 07 '14 at 08:55
  • Hi Bruno, thanks for your quick response. Actually I want to load the instance of file created by PDF stamper into Document object. The windows application that I am working with requires an Document instance so that finally created pdf can be rendered to the user screen. I am using PDFStamper as my pdf has acroform. Earlier function was using AddTemplate & Imported page and was returning a document but the form fields were getting flattened (disabled) after generation. Please help. – DevXR Dec 07 '14 at 09:05
  • 1
    Loading a file into an iText `Document` object is impossible. I have no knowledge of any Windows application that requires an iText `Document` object to render a PDF to the screen, because iText doesn't do rendering. I think you are confusing the iText `Document` class with some other product. I would be very surprised if somebody used iText `Document` class to *render* a document. That design would be seriously flawed. What is the context of your application? Is it a web application or a desktop application? – Bruno Lowagie Dec 07 '14 at 11:42
  • No Bruno, itext is not used for rendering the pdf, the pdf is rendered via simple c# to open file from a directory. Its a desktop application. I am adding some content to an acroforms pdf document using itext and then saving the document at a new location. However the acro fields become flat. Let me try the code that you pointed to: http://stackoverflow.com/questions/26174675/copy-pdf-with- annotations-using-itext – DevXR Dec 07 '14 at 12:06
  • 1
    Just to reiterate what @BrunoLowagie is saying, passing `Document` objects around almost never makes sense. Despite what the name might sound like, a `Document` doesn't represent a PDF in any way. Calling `ToString()` or `GetBytes()` (if that method actually existed) wouldn't get you a PDF. A `Document` is just a one-way funnel for passing human-friendly commands over to an engine that actually writes raw PDF tokens. The engine, however, is also not even a PDF. The only thing that truly is a PDF is the raw bytes of the stream that is being written to. – Chris Haas Dec 08 '14 at 14:40
  • Thanks Chris for making it so clear. – DevXR Dec 11 '14 at 08:32

1 Answers1

0

Thanks Chris.

Just to reiterate what @BrunoLowagie is saying, passing Document objects around almost never makes >sense. Despite what the name might sound like, a Document doesn't represent a PDF in any way. Calling >ToString() or GetBytes() (if that method actually existed) wouldn't get you a PDF. A Document is just a >one-way funnel for passing human-friendly commands over to an engine that actually writes raw PDF >tokens. The engine, however, is also not even a PDF. The only thing that truly is a PDF is the raw >bytes of the stream that is being written to. – Chris Haas

DevXR
  • 3
  • 2