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

  • SEARCH
  • Home
  • 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 8730609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:03:32+00:00 2026-06-13T09:03:32+00:00

I am using iTextSharp to generate PDF documents, and save them to disk like

  • 0

I am using iTextSharp to generate PDF documents, and save them to disk like this:

        Document document = new Document(PageSize.LETTER, 50, 50, 80, 50);
        FileStream fs = new FileStream(Ruta, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
        PdfWriter PDFWriter = PdfWriter.GetInstance(document, fs);

        EsquemaFormato PageEventHandler = new EsquemaFormato();
        PageEventHandler.Title = TitleMiddle;
        PageEventHandler.TitleHeaderFont = FontFactory.GetFont(BaseFont.HELVETICA, 12, Font.NORMAL);
        PageEventHandler.TitleRightHeaderFont = FontFactory.GetFont(BaseFont.HELVETICA, 9, Font.NORMAL);
        PageEventHandler.HeaderLeft = "Nombre";
        PageEventHandler.HeaderRight = TitleRight;
        PageEventHandler.RutaImagen = RutaImagen;
        PageEventHandler.Cuerpo = Cuerpo;
        PDFWriter.PageEvent = PageEventHandler;

        document.Open();
        document.Close();

Based on http://www.mazsoft.com/blog/post/2008/04/30/Code-sample-for-using-iTextSharp-PDF-library.aspx

I open any PDF file I just created, but when I try to close it causes a “Do you want to Save?” dialog to appear. This does not happen with PDFs that are not generated with iTextSharp.

What am I missing? I’ve seen solutions using Response but I don´t use it.
Thanks in advance.

Update:
Here is a sample http://dl.dropbox.com/u/4582874/test.pdf

EDIT:
By following Bruno’s link I found this post: iTextSharp + FileStream = Corrupt PDF file

That did the trick!
Here is my working code now:

    public void CreatePDF(string TitleMiddle, string Ruta, string RutaImagen, string TitleRight, string Cuerpo)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Document document = new Document(PageSize.LETTER, 50, 50, 80, 50);
            PdfWriter PDFWriter = PdfWriter.GetInstance(document, ms);

            EsquemaFormato PageEventHandler = new EsquemaFormato();
            PageEventHandler.Title = TitleMiddle;
            PageEventHandler.TitleHeaderFont = FontFactory.GetFont(BaseFont.HELVETICA, 12, Font.NORMAL);
            PageEventHandler.TitleRightHeaderFont = FontFactory.GetFont(BaseFont.HELVETICA, 9, Font.NORMAL);
            PageEventHandler.HeaderLeft = "Nombre";
            PageEventHandler.HeaderRight = TitleRight;
            PageEventHandler.RutaImagen = RutaImagen;
            PageEventHandler.Cuerpo = Cuerpo;
            PDFWriter.PageEvent = PageEventHandler;

            document.Open();
            document.Close();
            byte[] content = ms.ToArray();
            using (FileStream fs = File.Create(Ruta))
            {
                fs.Write(content, 0, (int)content.Length);
            }
        }
    }
  • 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-06-13T09:03:33+00:00Added an answer on June 13, 2026 at 9:03 am

    Have you tried it without the FileAccess.ReadWrite, FileShare.ReadWrite properties?

    I create PDF’s using itextSharp everyday and have never ran into this issue – main difference between your code and mine is i don’t have these.

    I suspect the FileShare is leaving the document somewhat open, waiting for someone to finalise closing it with a save?

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

Sidebar

Related Questions

I have a project which needs to generate PDF documents. I am using iTextSharp.
I am using iTextsharp to generate a PDF document from C# application. I am
Using iTextSharp to add form fields to pdf files, then saving them as new
I am using iTextSharp to generate PDF out of HTML. I can save the
I'm using iTextSharp library, specifically to view .xml documents and transform them into .pdf
I'm using iTextSharp to convert large images to PDF documents. This works, but the
I'm using iTextSharp to print a PDF document. Everything goes ok until I have
I've recently started using iTextSharp to generate PDF reports from data. It works quite
I'm using itextsharp to join mutiple pdf documents and add a footer. My code
Im using ITextSharp to generate a PDF, and have a situation where a need

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.