-1

I try draw a A5 rentangle in a PDF with Itext using Rectangle class and Utilities.milimetersToPoints method but when i print the PDF and measure the rectangle the measurments is not the A5 dimensions.

public static boolean createPDF(String pathPDF) {

    Document document = new Document(PageSize.A4);

    try {
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pathPDF));
        writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
        document.open();

        createRectangle(writer, 30.75f, 11, 148.5f, 210, Color.RED);
        document.close();

    } catch (Exception e) {
        logger.error("Error , e);
        return false;
    } 

    return true;

}

private static void createRectangle(PdfWriter writer, float x, float y, float width, float height, Color color) {
    float posX = Utilities.millimetersToPoints(x);
    float posY = Utilities.millimetersToPoints(y);

    float widthX = Utilities.millimetersToPoints(width+x);
    float heightY = Utilities.millimetersToPoints(height+y);

    Rectangle rect = new Rectangle(posX, posY, widthX, heightY);

    PdfContentByte canvas = writer.getDirectContent();
    rectangle.setBorder(Rectangle.BOX);
    rectangle.setBorderWidth(1);
    rectangle.setBorderColor(color);
    canvas.rectangle(rectangle);

}

what i'm doing wrong?

I use Itext 2.1.7 Many Thanks

Javetor
  • 25
  • 1
  • 4
  • Which are your scaling options during printing? Please share a sample file to inspect. – mkl Jun 22 '15 at 12:24
  • I dont scale the rectangle. It's in a Itext Document with PageSize.A4 dimensions. – Javetor Jun 22 '15 at 12:32
  • Please share a sample file to inspect. – mkl Jun 22 '15 at 12:34
  • Sorry i cant share documents from this pc. Is posible that the dpi affect this? – Javetor Jun 22 '15 at 12:36
  • Then please supply a [sscce](http://sscce.org/) allowing the generation of such a PDF. – mkl Jun 22 '15 at 12:39
  • I dont have access to many webs and sscce is one of them :( I go to complete my question with all code. – Javetor Jun 22 '15 at 12:46
  • iText 2.1.7 should no longer be used because of technical and legal issues. Read the legal question in this [free sample of The Best iText Questions on StackOverflow](http://samples.leanpub.com/itext_so-sample.pdf). – Bruno Lowagie Jun 22 '15 at 13:30
  • Hello @BrunoLowagie, i dont decide the version to use. Its in the proyect when i start to work in it. Is 5.5.6 the last version??? Thanks – Javetor Jun 22 '15 at 14:02
  • Yes, 5.5.6 is the most recent version. – Bruno Lowagie Jun 22 '15 at 14:21

1 Answers1

2

I just ran your SSCCE (Short, Self Contained, Correct (Compilable), Example), and printed the resulting PDF from Adobe Reader. I measured the red rectangle both on-screen using the Adobe Reader measuring tool and on-paper.

Screenshot of measurement on-screen

The result on screen:

Excerpt of screenshot of measurement on-screen

matches your parameters

createRectangle(writer, 30.75f, 11, 148.5f, 210, Color.RED);
------------------------------------^^^^^^--^^^

exactly, and the measurement on-paper matches as exactly as measurement with rulers can be.


Possible causes of the problem on your side:

  1. I use a current iText version. But while there have been quite a lot of improvements and fixes since 2.1.7, I doubt any fixes had been applied here.

  2. Different printers; some printers are known to scale while printing.

  3. Different viewer from which you print; some viewers may always scale.

  4. Different scaling settings in printer dialog.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thanks @mkl No matter how many times I tell people to stop using iText 2.1.7, they just don't listen... – Bruno Lowagie Jun 22 '15 at 13:32
  • 1
    @BrunoLowagie Yes, people should indeed upgrade for so many reasons. But I doubt it will help with this issue, I am pretty sure iText 2.x properly translated dimensions. – mkl Jun 22 '15 at 13:38
  • I try with 5.5.6 version and the result is the same :( I'm virtually certain that the problem is the printer or the printer dialog. Thanks – Javetor Jun 22 '15 at 14:14
  • I would advice also to measure on-screen first. If Adobe Reader measurement tool displays the correct data as above, the issue is definitively between viewer and printer, not in the PDF. – mkl Jun 22 '15 at 14:53