Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6787707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:23:53+00:00 2026-05-26T17:23:53+00:00

I have code (in a .ashx-file) that generates a PDF file from a PDF

  • 0

I have code (in a .ashx-file) that generates a PDF file from a PDF template. The generated pdf gets personalized with a name and a code. I use iTextSharp to do so.

This is the code:

using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
    using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
    {
        var pdfReader = new PdfReader(existingFileStream);
        var stamper = new PdfStamper(pdfReader, newFileStream);

        var form = stamper.AcroFields;
        var fieldKeys = form.Fields.Keys;

        form.SetField("Name", name);
        form.SetField("Code", code);

        stamper.FormFlattening = true;

        stamper.Close();
        pdfReader.Close();
    }

context.Response.AppendHeader("content-disposition", "inline; filename=zenith_coupon.pdf");
context.Response.TransmitFile(fileNameNew);
context.Response.ContentType = "application/pdf";

This works, but it saves the file on the server. I don’t want to do that because there’re going to be a lot of people downloading the PDF file and the server will be full in no time.

So my question is, how can I generate a PDF with iTextSharp without saving it and put it to the user?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:23:54+00:00Added an answer on May 26, 2026 at 5:23 pm

    You can use any Stream (for example MemoryStream) for the intermediate PDF (in your code currently named newFileStream) if you don’t want to save it as a file – for sample code see http://www.developerfusion.com/code/6623/dynamically-generating-pdfs-in-net/ and http://forums.asp.net/t/1093198.aspx/1.

    Just remember to rewind (i.e. set Position = 0) the MemoryStream before transmitting it to the client (for example by Response.Write or CopyTo (Response.OutputStream) )…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ashx page handling file/attachment requests last line of code is System.Web.HttpContext.Current.Response.WriteFile(filename)
I have download the code for OpenFileDialog (File Upload function) from Silverlight tutorials website.
I have a handler (.ashx) file that I'm using to deal with Requests. Depending
I have code that references a web service, and I'd like the address of
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code that looks like the following, which works fine for displaying the
I have the following code: $.ajax({ type: GET, url: AJAX.ashx?job=fetch_calendar, dataType: html, success: function
I have created a few small flash widgets that stream .mp3 audio from an
I visit my ASHX file, and it outputs a PDF perfectly. If I visit
I have created an ashx file to generate image thumbnails on the fly. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.