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 9123025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:14:26+00:00 2026-06-17T06:14:26+00:00

I have created pdf using IText library Now I want when the user clicks

  • 0

I have created pdf using IText library
Now I want when the user clicks on the button ,it should prompt a save dialog box to the user to allow to browse the destination location to store the file.

Now My code looks like

    Document document = new Document();
    try {

                    FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.reset();  
        response.setContentType("application/pdf");

        response.setHeader("Content-Disposition", "attachment; filename=ABC.pdf");
        response.setHeader("Cache-Control", "no-cache");  
        response.setDateHeader("Expires", 0);  
        response.setHeader("Pragma", "No-cache");
        PdfWriter.getInstance(document, response.getOutputStream());

        document.addTitle("Form");
        document.addSubject("Form");

        document.open();

        Paragraph paragraph12 = new Paragraph("FORM",

        FontFactory.getFont(FontFactory.HELVETICA,

        18, Font.BOLD, new CMYKColor(0, 255, 255, 17)));

        Chapter chapter1 = new Chapter(paragraph12, 1);
        paragraph12.setSpacingAfter(25);
        paragraph12.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph12);
        // Centered

        Chunk underline = new Chunk(AppNo);
        underline.setUnderline(0.2f, -2f);

        Paragraph paragraph = new Paragraph("Application No:");
        paragraph.add(underline);
        paragraph.setAlignment(Element.ALIGN_LEFT);
        paragraph.setSpacingAfter(20);
        document.add(paragraph);
        // Left
        paragraph = new Paragraph("To,");
        paragraph.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph);
        // Left with indentation
        paragraph = new Paragraph(
                "");
        paragraph.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph);

        Paragraph paragraph2 = new Paragraph(
                "Subject: ");

        paragraph2.setSpacingAfter(15);
        paragraph2.setSpacingBefore(15);
        paragraph2.setAlignment(Element.ALIGN_LEFT);
        paragraph2.setIndentationRight(50);
        document.add(paragraph2);



        Paragraph paragraph3 = new Paragraph("I  ");
        String laName=deaclarationOpr.getDeclarationDetailsobj()
                .getLaName();
        if(laName!=null){
        Chunk name = new Chunk(laName);
        name.setUnderline(0.2f, -2f);
        paragraph3.add(name);
        }
        paragraph3.add("  request you to process the Application  ");
        paragraph3.add(underline);
        paragraph3.add("  for product  ");


        String productName=deaclarationOpr
                .getDeclarationDetailsobj().getProductName();
        if(productName!=null){
        Chunk product = new Chunk(productName);
        product.setUnderline(0.2f, -2f);
        paragraph3.add(product);
        }
        paragraph3
                .add("");

        paragraph3.setSpacingAfter(15);
        paragraph3.setSpacingBefore(15);
        paragraph3.setAlignment(Element.ALIGN_LEFT);
        paragraph3.setIndentationLeft(50);
        paragraph3.setIndentationRight(50);
        document.add(paragraph3);

        Paragraph paragraph4 = new Paragraph(
                "");





        String code=deaclarationOpr
                .getDeclarationDetailsobj().getAdvisorCode();
        if(code!=null){
        Chunk advisorCode = new Chunk(code);
        advisorCode.setUnderline(0.2f, -2f);
        paragraph4.add(advisorCode);
        }
        paragraph4
                .add("");

        paragraph4.setSpacingAfter(15);
        paragraph4.setSpacingBefore(15);
        paragraph4.setAlignment(Element.ALIGN_LEFT);
        paragraph4.setIndentationLeft(50);
        paragraph4.setIndentationRight(50);
        document.add(paragraph4);

        Paragraph paragraph5 = new Paragraph(
                "");

        paragraph5.setSpacingAfter(15);
        paragraph5.setSpacingBefore(15);
        paragraph5.setAlignment(Element.ALIGN_LEFT);
        paragraph5.setIndentationLeft(50);
        paragraph3.setIndentationRight(50);
        document.add(paragraph5);

        Paragraph paragraph6 = new Paragraph(
                "");

        paragraph6.setSpacingAfter(15);
        paragraph6.setSpacingBefore(15);
        paragraph6.setAlignment(Element.ALIGN_LEFT);
        paragraph6.setIndentationLeft(50);
        paragraph6.setIndentationRight(50);
        document.add(paragraph6);

        Paragraph paragraph7 = new Paragraph("Date:      ");
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        // get current date time with Date()
        Date date = new Date();
        paragraph7.add(dateFormat.format(date));
        paragraph7.setSpacingBefore(15);
        paragraph7.setIndentationLeft(50);
        paragraph7.setIndentationRight(50);
        paragraph7.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph7);

        Paragraph paragraph8 = new Paragraph("Name of the Proposer:      ");

        String propoer=deaclarationOpr
                .getDeclarationDetailsobj().getProposerName();
        if(propoer!=null){
        Chunk proposerName = new Chunk(propoer);
        proposerName.setUnderline(0.2f, -2f);
        paragraph8.add(proposerName);
        }
        paragraph8.setAlignment(Element.ALIGN_LEFT);
        paragraph8.setIndentationLeft(50);
        paragraph8.setIndentationRight(50);
        document.add(paragraph8);

        Paragraph paragraph9 = new Paragraph("Place:      ");
        paragraph9.setIndentationLeft(50);
        paragraph9.setIndentationRight(50);
        paragraph9.setAlignment(Element.ALIGN_LEFT);
        document.add(paragraph9);


        document.close();

context.responseComplete();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Following filters are defined in web.xml

<filter>
        <display-name>RichFaces Filter</display-name>
        <filter-name>richfaces</filter-name>
        <filter-class>org.ajax4jsf.Filter</filter-class>
        <init-param>
            <param-name>createTempFiles</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>maxRequestSize</param-name>
            <param-value>20000000</param-value>
        </init-param>
        <init-param>
            <param-name>enable-cache</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>richfaces</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

And the above code is written in the method of Backing Bean class

and invoked

<tr>
<td align="center"><a4j:commandButton
value="Print" styleClass="button"
action="#{declarationBB.convertToPdf}"></a4j:commandButton></td>
</tr>

I get below in the new page

%PDF-1.4 %��3 0 obj <>stream x��V����Ժ@��rs�0��m|셒F6[�T���H[��E D'��7�� 
  • 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-17T06:14:27+00:00Added an answer on June 17, 2026 at 6:14 am

    Just write the PDF to the HTTP response instead of to a local file.

    Replace

    PdfWriter.getInstance(document, new FileOutputStream("D:/Files/ABC.pdf"));
    

    by

    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=\"ABC.pdf\"");
    PdfWriter.getInstance(document, response.getOutputStream());
    
    // ... (your PDF building code here)
    
    context.responseComplete();
    

    and invoke it in action method associated with download button. The Content-Disposition header with value of attachment will force a Save As dialogue (unless the browser is configured to automatically open PDF files in Adobe Reader!).

    See also:

    • How to provide a file download from a JSF backing bean?

    Update: as per your update, you’re invoking the download by ajax.

    <a4j:commandButton value="Print" styleClass="button" action="#{declarationBB.convertToPdf}"></a4j:commandButton>
    

    This won’t work. You can’t download files by ajax. Ajax is executed by JavaScript, but it has due to obvious security reasons no facilities to programmatically force a Save As dialogue with asynchronously obtained/generated content in some variable. Make it a normal request instead. Use <h:commandButton> instead.

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

Sidebar

Related Questions

I'm using html2fpdf for creating PDF documents. Now once I have created that, I
I have created a pdf with an image in it using itextsharp library and
I have created a webview to display the pdf, now using the gesture recognizer
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have created a pdf version of our rails application using the Prawn plugin,
I have a PDF created in memory using iTextSharp and contained in a MemoryStream.
Hi I have created a PDF file with an image in it, I want
I have created new PDFs many times using Zend and PHP. But Now I
So I am writing to a pdf through java using iText. I want to
I have created a pdf using itextsharp . The requirement is that while sending

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.