You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Pierre Antoine Guillaume <pi...@gmail.com> on 2016/05/02 15:14:58 UTC

Fwd: How to print PDF without magnifiying them ?

Hello, i'm new to using pdfbox and when I try to print (with a real
printer), the pdf is kind of magnified (every margin of the page is
smaller).

When I print into a virtual printer (pdfcreator) the newly generated pdf
seems to be the same. (I get the right page)

And when I print the PDF directly, I get the right page.

Have you any idea on how to correct this issue ?

I've tried to set the format to A4 through

            PrintRequestAttributeSet attr_set = new
HashPrintRequestAttributeSet();

            attr_set.add(MediaSizeName.ISO_A4);
            attr_set.add(Sides.ONE_SIDED);

But it won't change anything.

Thank you for reading.

Re: How to print PDF without magnifiying them ?

Posted by John Hewson <jo...@jahewson.com>.
> On 2 May 2016, at 06:59, Tilman Hausherr <TH...@t-online.de> wrote:
> 
> Please extend the PDFPageable class, and override getPrintable(). The current code is:
> 
>    @Override
>    public Printable getPrintable(int i)
>    {
>        if (i >= getNumberOfPages())
>        {
>            throw new IndexOutOfBoundsException(i + " >= " + getNumberOfPages());
>        }
>        return new PDFPrintable(document, Scaling.ACTUAL_SIZE, showPageBorder, dpi);
>    }
> 
> Have a look at the parameters for PDFPrintable() and use the ones that are best for you. I suspect you want Scaling.SCALE_TO_FIT.

No, don’t do that! The concept of a Pageable in the Java printing API is something which appears at its natural dimensions on the page, i.e. the document controls the page, not the user.

If the user wants to control the page, use a Printable directly, don’t use a Pageable at all.

— John

> Tilman
> 
> 
> Am 02.05.2016 um 15:32 schrieb Pierre Antoine Guillaume:
>> I'm using pdfbox 2.0.1 and awt.print / javax.print
>> 
>> http://pastebin.com/PNwT5RK4
>> 
>> 2016-05-02 15:25 GMT+02:00 Tilman Hausherr <TH...@t-online.de>:
>> 
>>> Please mention what version you are using, and post the code you have been
>>> using to print.
>>> 
>>> Tilman
>>> 
>>> 
>>> Am 02.05.2016 um 15:14 schrieb Pierre Antoine Guillaume:
>>> 
>>>> Hello, i'm new to using pdfbox and when I try to print (with a real
>>>> printer), the pdf is kind of magnified (every margin of the page is
>>>> smaller).
>>>> 
>>>> When I print into a virtual printer (pdfcreator) the newly generated pdf
>>>> seems to be the same. (I get the right page)
>>>> 
>>>> And when I print the PDF directly, I get the right page.
>>>> 
>>>> Have you any idea on how to correct this issue ?
>>>> 
>>>> I've tried to set the format to A4 through
>>>> 
>>>>              PrintRequestAttributeSet attr_set = new
>>>> HashPrintRequestAttributeSet();
>>>> 
>>>>              attr_set.add(MediaSizeName.ISO_A4);
>>>>              attr_set.add(Sides.ONE_SIDED);
>>>> 
>>>> But it won't change anything.
>>>> 
>>>> Thank you for reading.
>>>> 
>>>> 
>>> ---------------------------------------------------------------------
>>> 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
> 


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


Re: Fwd: How to print PDF without magnifiying them ?

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 03.05.2016 um 09:40 schrieb Pierre Antoine Guillaume:
> PDFPageable is final, would this work ?

Sorry...

>
>              PDFPageable p = new PDFPageable(pdf);
>              PDFPrintable printable = new
> PDFPrintable(pdf,Scaling.SCALE_TO_FIT);
>
>              job.setPageable(p);
>              job.setPrintable(printable);
>
> I mean, it seems to work properly and give the expexted result. But is
> there any trouble I don't know of, which I should worry about ?

I haven't heard of any troubles, but I'm really not the printing guy 
here. I found another example in the source code which uses a PDFPrintable:

     /**
      * Prints using a custom page size and custom margins.
      */
     private static void printWithPaper(PDDocument document)
             throws IOException, PrinterException
     {
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPageable(new PDFPageable(document));

         // define custom paper
         Paper paper = new Paper();
         paper.setSize(306, 396); // 1/72 inch
         paper.setImageableArea(0, 0, paper.getWidth(), 
paper.getHeight()); // no margins

         // custom page format
         PageFormat pageFormat = new PageFormat();
         pageFormat.setPaper(paper);

         // override the page format
         Book book = new Book();
         // append all pages
         book.append(new PDFPrintable(document), pageFormat, 
document.getNumberOfPages());
         job.setPageable(book);

         job.print();
     }


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


Re: Fwd: How to print PDF without magnifiying them ?

Posted by Pierre Antoine Guillaume <pi...@gmail.com>.
PDFPageable is final, would this work ?

            PDFPageable p = new PDFPageable(pdf);
            PDFPrintable printable = new
PDFPrintable(pdf,Scaling.SCALE_TO_FIT);

            job.setPageable(p);
            job.setPrintable(printable);

I mean, it seems to work properly and give the expexted result. But is
there any trouble I don't know of, which I should worry about ?

Thank you for your help

Re: Fwd: How to print PDF without magnifiying them ?

Posted by Tilman Hausherr <TH...@t-online.de>.
Please extend the PDFPageable class, and override getPrintable(). The 
current code is:

     @Override
     public Printable getPrintable(int i)
     {
         if (i >= getNumberOfPages())
         {
             throw new IndexOutOfBoundsException(i + " >= " + 
getNumberOfPages());
         }
         return new PDFPrintable(document, Scaling.ACTUAL_SIZE, 
showPageBorder, dpi);
     }

Have a look at the parameters for PDFPrintable() and use the ones that 
are best for you. I suspect you want Scaling.SCALE_TO_FIT.

Tilman


Am 02.05.2016 um 15:32 schrieb Pierre Antoine Guillaume:
> I'm using pdfbox 2.0.1 and awt.print / javax.print
>
> http://pastebin.com/PNwT5RK4
>
> 2016-05-02 15:25 GMT+02:00 Tilman Hausherr <TH...@t-online.de>:
>
>> Please mention what version you are using, and post the code you have been
>> using to print.
>>
>> Tilman
>>
>>
>> Am 02.05.2016 um 15:14 schrieb Pierre Antoine Guillaume:
>>
>>> Hello, i'm new to using pdfbox and when I try to print (with a real
>>> printer), the pdf is kind of magnified (every margin of the page is
>>> smaller).
>>>
>>> When I print into a virtual printer (pdfcreator) the newly generated pdf
>>> seems to be the same. (I get the right page)
>>>
>>> And when I print the PDF directly, I get the right page.
>>>
>>> Have you any idea on how to correct this issue ?
>>>
>>> I've tried to set the format to A4 through
>>>
>>>               PrintRequestAttributeSet attr_set = new
>>> HashPrintRequestAttributeSet();
>>>
>>>               attr_set.add(MediaSizeName.ISO_A4);
>>>               attr_set.add(Sides.ONE_SIDED);
>>>
>>> But it won't change anything.
>>>
>>> Thank you for reading.
>>>
>>>
>> ---------------------------------------------------------------------
>> 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


Re: Fwd: How to print PDF without magnifiying them ?

Posted by Pierre Antoine Guillaume <pi...@gmail.com>.
I'm using pdfbox 2.0.1 and awt.print / javax.print

http://pastebin.com/PNwT5RK4

2016-05-02 15:25 GMT+02:00 Tilman Hausherr <TH...@t-online.de>:

> Please mention what version you are using, and post the code you have been
> using to print.
>
> Tilman
>
>
> Am 02.05.2016 um 15:14 schrieb Pierre Antoine Guillaume:
>
>> Hello, i'm new to using pdfbox and when I try to print (with a real
>> printer), the pdf is kind of magnified (every margin of the page is
>> smaller).
>>
>> When I print into a virtual printer (pdfcreator) the newly generated pdf
>> seems to be the same. (I get the right page)
>>
>> And when I print the PDF directly, I get the right page.
>>
>> Have you any idea on how to correct this issue ?
>>
>> I've tried to set the format to A4 through
>>
>>              PrintRequestAttributeSet attr_set = new
>> HashPrintRequestAttributeSet();
>>
>>              attr_set.add(MediaSizeName.ISO_A4);
>>              attr_set.add(Sides.ONE_SIDED);
>>
>> But it won't change anything.
>>
>> Thank you for reading.
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Fwd: How to print PDF without magnifiying them ?

Posted by Tilman Hausherr <TH...@t-online.de>.
Please mention what version you are using, and post the code you have 
been using to print.

Tilman

Am 02.05.2016 um 15:14 schrieb Pierre Antoine Guillaume:
> Hello, i'm new to using pdfbox and when I try to print (with a real
> printer), the pdf is kind of magnified (every margin of the page is
> smaller).
>
> When I print into a virtual printer (pdfcreator) the newly generated pdf
> seems to be the same. (I get the right page)
>
> And when I print the PDF directly, I get the right page.
>
> Have you any idea on how to correct this issue ?
>
> I've tried to set the format to A4 through
>
>              PrintRequestAttributeSet attr_set = new
> HashPrintRequestAttributeSet();
>
>              attr_set.add(MediaSizeName.ISO_A4);
>              attr_set.add(Sides.ONE_SIDED);
>
> But it won't change anything.
>
> Thank you for reading.
>


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


Re: How to print PDF without magnifiying them ?

Posted by John Hewson <jo...@jahewson.com>.
> On 2 May 2016, at 06:14, Pierre Antoine Guillaume <pi...@gmail.com> wrote:
> 
> Hello, i'm new to using pdfbox and when I try to print (with a real
> printer), the pdf is kind of magnified (every margin of the page is
> smaller).

I suspect this is due to the margins which Java sets by default when printing.
You can bypass that by overriding the Paper size, see printWithPaper:

https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java <https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java>

> When I print into a virtual printer (pdfcreator) the newly generated pdf
> seems to be the same. (I get the right page)

Margins should be zero here, so you’re seeing what PDFBox intended without
Java changing things.

— John

> And when I print the PDF directly, I get the right page.
> 
> Have you any idea on how to correct this issue ?
> 
> I've tried to set the format to A4 through
> 
>            PrintRequestAttributeSet attr_set = new
> HashPrintRequestAttributeSet();
> 
>            attr_set.add(MediaSizeName.ISO_A4);
>            attr_set.add(Sides.ONE_SIDED);
> 
> But it won't change anything.
> 
> Thank you for reading.