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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:03:14+00:00 2026-05-31T06:03:14+00:00

Some background to this question can be found at Check printing with Java/JSP and

  • 0

Some background to this question can be found at Check printing with Java/JSP and Create a "print-only" PDF with itext

I have been able to successfully create and open a PDF with a print dialog using iText-2.0.8 and the following code:

String outputFile = "firstdoc.pdf";
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
writer.setOpenAction(new PdfAction(PdfAction.PRINTDIALOG)); 
document.open();
document.add(new Paragraph("TEST"));
document.close();

I have also been able to use flying-saucer to generate a PDF from XHTML using the following code:

String inputFile = "firstdoc.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";

OutputStream os = new FileOutputStream(outputFile);

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);

os.close();

However, I can’t seem to get the two to work together…

I would like to create the PDF using flying-saucer as in the 2nd code block and I would like to set the open action of that PDF to PdfAction.PRINTDIALOG.

How can I get these two sets of code to work together such that a flying-saucer created PDF opens with a print dialog initially?

  • 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-31T06:03:16+00:00Added an answer on May 31, 2026 at 6:03 am

    Figured it out…

    In case someone else needs this in the future, you can just use PdfStamper to modify a PDF that has already been created.

    Here’s the full code that worked for me:

    import java.io.*;
    
    import com.lowagie.text.Document;
    import com.lowagie.text.DocumentException;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.PdfWriter;
    import com.lowagie.text.pdf.PdfAction;
    import com.lowagie.text.pdf.PdfReader;
    import com.lowagie.text.pdf.PdfStamper;
    
    import org.xhtmlrenderer.pdf.ITextRenderer;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FirstDoc {
    
        public static void main(String[] args) throws IOException, DocumentException {
    
            String inputFile = "firstdoc.xhtml";
            String url = new File(inputFile).toURI().toURL().toString();
            String outputFile = "firstdoc.pdf";
    
            OutputStream os = new FileOutputStream(outputFile);
    
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(url);
            renderer.layout();
            renderer.createPDF(os);
    
            os.close();
    
            PdfReader reader = new PdfReader(outputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("firstdocprint.pdf"));
            stamper.setPageAction(PdfWriter.PAGE_OPEN, new PdfAction(PdfAction.PRINTDIALOG), 1); 
            stamper.close();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some background information, you can skip this part for the actual question this is
This is a Java syntax question, but just for some background. Using android, I
This question is related to another I ask recently, it can be found here
I apologize for the length of this question, but some background explanation is required.
For some reason the background does not appear. This is the CSS div.sidebar_beige {
I am looking for some help. I have found this script that sort of
I must apologize if this is a duplicate question, but I can't seem to
How fast can I replace characters in a string? So the background on this
Background: I found this article on JavaWorld, where Allen Holub explains an alternative to
This question is about constructing custom expression trees in .NET using the operators found

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.