pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Hewson <j...@jahewson.com>
Subject Re: Orientation printout - used 2.0.0.RC3 release
Date Thu, 17 Mar 2016 00:33:56 GMT

> On 15 Mar 2016, at 13:12, Tilman Hausherr <THausherr@t-online.de> wrote:
> 
> I recommend that you try with your own version of PDFPageable. I looked at the code and
IMHO the problem is that a rotation is set that you don't want. See in getPageFormat. I haven't
tested it, but I assume you should delete these lines:
> 
> 
>        if (mediaBox.getWidth() > mediaBox.getHeight())
>        {
>            // rotate
>            paper = new Paper();
>            paper.setSize(mediaBox.getHeight(), mediaBox.getWidth());
>            paper.setImageableArea(cropBox.getLowerLeftY(), cropBox.getLowerLeftX(),
>                    cropBox.getHeight(), cropBox.getWidth());
>            isLandscape = true;
>        }
>        else

You don’t want to do that though, because Java doesn’t handle landscape sized paper correctly.
So it’s necessary to always print in portrait, rotating any landscape pages first, and flagging
them as being landscape.

— John

> Here's the full code. If this doesn't get through, download the source code.
> 
> 
> public final class PDFPageable extends Book
> {
>    private final PDDocument document;
>    private final boolean showPageBorder;
>    private final float dpi;
>    private final Orientation orientation;
> 
>    /**
>     * Creates a new PDFPageable.
>     *
>     * @param document the document to print
>     */
>    public PDFPageable(PDDocument document)
>    {
>        this(document, Orientation.AUTO, false, 0);
>    }
> 
>    /**
>     * Creates a new PDFPageable with the given page orientation.
>     *
>     * @param document the document to print
>     * @param orientation page orientation policy
>     */
>    public PDFPageable(PDDocument document, Orientation orientation)
>    {
>        this(document, orientation, false, 0);
>    }
> 
>    /**
>     * Creates a new PDFPageable with the given page orientation and with optional page
borders
>     * shown. The image will be rasterized at the given DPI before being sent to the printer.
>     *
>     * @param document the document to print
>     * @param orientation page orientation policy
>     * @param showPageBorder true if page borders are to be printed
>     */
>    public PDFPageable(PDDocument document, Orientation orientation, boolean showPageBorder)
>    {
>        this(document, orientation, showPageBorder, 0);
>    }
> 
>    /**
>     * Creates a new PDFPageable with the given page orientation and with optional page
borders
>     * shown. The image will be rasterized at the given DPI before being sent to the printer.
>     *
>     * @param document the document to print
>     * @param orientation page orientation policy
>     * @param showPageBorder true if page borders are to be printed
>     * @param dpi if non-zero then the image will be rasterized at the given DPI
>     */
>    public PDFPageable(PDDocument document, Orientation orientation, boolean showPageBorder,
>                       float dpi)
>    {
>        this.document = document;
>        this.orientation = orientation;
>        this.showPageBorder = showPageBorder;
>        this.dpi = dpi;
>    }
> 
>    @Override
>    public int getNumberOfPages()
>    {
>        return document.getNumberOfPages();
>    }
> 
>    /**
>     * {@inheritDoc}
>     *
>     * Returns the actual physical size of the pages in the PDF file. May not fit the
local printer.
>     */
>    @Override
>    public PageFormat getPageFormat(int pageIndex)
>    {
>        PDPage page = document.getPage(pageIndex);
>        PDRectangle mediaBox = PDFPrintable.getRotatedMediaBox(page);
>        PDRectangle cropBox = PDFPrintable.getRotatedCropBox(page);
> 
>        // Java does not seem to understand landscape paper sizes, i.e. where width >
height, it
>        // always crops the imageable area as if the page were in portrait. I suspect
that this is
>        // a JDK bug but it might be by design, see PDFBOX-2922.
>        //
>        // As a workaround, we normalise all Page(s) to be portrait, then flag them as
landscape in
>        // the PageFormat.
>        Paper paper;
>        boolean isLandscape;
>        if (mediaBox.getWidth() > mediaBox.getHeight())
>        {
>            // rotate
>            paper = new Paper();
>            paper.setSize(mediaBox.getHeight(), mediaBox.getWidth());
>            paper.setImageableArea(cropBox.getLowerLeftY(), cropBox.getLowerLeftX(),
>                    cropBox.getHeight(), cropBox.getWidth());
>            isLandscape = true;
>        }
>        else
>        {
>            paper = new Paper();
>            paper.setSize(mediaBox.getWidth(), mediaBox.getHeight());
>            paper.setImageableArea(cropBox.getLowerLeftX(), cropBox.getLowerLeftY(),
>                    cropBox.getWidth(), cropBox.getHeight());
>            isLandscape = false;
>        }
> 
>        PageFormat format = new PageFormat();
>        format.setPaper(paper);
> 
>        // auto portrait/landscape
>        if (orientation == Orientation.AUTO)
>        {
>            if (isLandscape)
>            {
>                format.setOrientation(PageFormat.LANDSCAPE);
>            }
>            else
>            {
>                format.setOrientation(PageFormat.PORTRAIT);
>            }
>        }
>        else if (orientation == Orientation.LANDSCAPE)
>        {
>            format.setOrientation(PageFormat.LANDSCAPE);
>        }
>        else if (orientation == Orientation.PORTRAIT)
>        {
>            format.setOrientation(PageFormat.PORTRAIT);
>        }
> 
>        return format;
>    }
> 
>    @Override
>    public Printable getPrintable(int i)
>    {
>        if (i >= getNumberOfPages())
>        {
>            throw new IndexOutOfBoundsException(i + " >= " + getNumberOfPages());
>        }
>        return new PDFPrintable(document, Scaling.ACTUAL_SIZE, showPageBorder, dpi);
>    }
> }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message