2

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?

Community
  • 1
  • 1
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104

3 Answers3

3

You can implement org.xhtmlrenderer.pdf.PDFCreationListener of flying saucer and write your print dialog related code in preWrite(ITextRenderer iTextRenderer, int pageCount) method.

@Override    
public void preWrite(ITextRenderer iTextRenderer, int pageCount)
{
     iTextRenderer.getOutputDevice().getWriter().setOpenAction(new PdfAction(PdfAction.PRINTDIALOG));
}

Add this listener in ITextRenderer

ITextRenderer renderer = new ITextRenderer();
renderer.setListener(PDFCreationListener implemetation class);
    renderer.setDocumentFromString(htmlContent);
    renderer.layout();
    renderer.createPDF(os);

Hope this helps.

John
  • 541
  • 3
  • 6
  • 19
2

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();
    }
}
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
  • 1
    Thanks a lot, your post helped me solve my problem. However there is no need to create the 2nd PDF file, if the only thing you want is to print the PDF. I did below to avoid creating 2 PDFs: OutputStream os = new FileOutputStream(outputFile); ByteArrayOutputStream outputStreamForPrinter = new ByteArrayOutputStream(); PdfReader reader = new PdfReader(os.toByteArray()); PdfStamper stamper = new PdfStamper(reader, outputStreamForPrinter); – sunny_dev Oct 15 '14 at 12:01
0

Modification to flying saucer document.

PDDocument document = ...;

PDPage firstPage = document.getPage(0);
PDPageAdditionalActions actions = firstPage.getActions();
actions.setO(new PDActionJavaScript("this.print(true);\r")); // O for open
ThomasRS
  • 8,215
  • 5
  • 33
  • 48