Example usage for com.itextpdf.text.pdf PdfObject toPdf

List of usage examples for com.itextpdf.text.pdf PdfObject toPdf

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfObject toPdf.

Prototype

public void toPdf(PdfWriter writer, OutputStream os) throws IOException 

Source Link

Document

Writes the PDF representation of this PdfObject as an array of bytes to the writer.

Usage

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

/**
 * Overriding standard PdfObject.toPdf because we need sorted PdfDictionaries.
 *//*w  ww.ja v  a  2 s.c  o m*/
private static void toPdf(PdfObject object, PdfWriter writer, OutputStream os) throws IOException {
    if (object instanceof PdfDictionary) {
        os.write('<');
        os.write('<');

        List<PdfName> keys = new ArrayList<PdfName>(((PdfDictionary) object).getKeys());
        Collections.sort(keys);

        for (PdfName key : keys) {
            toPdf(key, writer, os);
            PdfObject value = ((PdfDictionary) object).get(key);
            int type = value.type();

            if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY && type != PdfObject.NAME
                    && type != PdfObject.STRING) {
                os.write(' ');
            }

            toPdf(value, writer, os);
        }

        os.write('>');
        os.write('>');
    } else {
        object.toPdf(writer, os);
    }
}