Creating measure dictionary for distance annotatio

2019-07-25 11:16发布

问题:

Creating a measure distanced annotation does not display the correct measurement unit, nor gives the correct calculation. Any idea which part I am doing wrong? or if there is some lacking data.

I am currently creating a measure distance annotation. Using pixel, it works just fine. but since now i am taking into account actual measurement and unit, i am not sure which part of code i am having problem with or if i am lacking something.

Please see image. That is some application i use to create a distance annotation and i calibrate it that that distance is 14.5cm so by dividing it by pixel, the calibration value per pixel would be 0.0519548.

Now, when I apply it to iText code, i am confused why the display is always still in inches? Even if i set my code to be inches and not cm, the calculation is incorrect.

I am not entirely sure what the problem is.

public class Test {

        public static void main(String[] args) throws Exception {
            PdfReader reader = new PdfReader("src.pdf");
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("result.pdf"));

            Rectangle location = new Rectangle(55.78125f, 493.875f, 253.59375f, 562.3125f);

            PdfDictionary dict = new PdfDictionary();

            PdfArray lineEndings = new PdfArray();
            lineEndings.add(new PdfName("OpenArrow"));
            lineEndings.add(new PdfName("OpenArrow"));

            PdfAnnotation stamp = PdfAnnotation.createLine(stamper.getWriter(), location, "test measurement", 55.78125f, 562.3125f, 253.59375f, 493.875f);
            stamp.put(new PdfName("LE"), lineEndings);
            stamp.put(PdfName.ROTATE, new PdfNumber(0));
            stamp.put(PdfName.MEASURE, createMeasureDictionary());
            stamp.put(new PdfName("IT"), new PdfName("LineDimension"));
            stamp.put(new PdfName("Cap"), new PdfBoolean(true));
            stamp.put(PdfName.F, new PdfNumber(516));

            stamp.setColor(PdfGraphics2D.prepareColor(Color.RED));
            stamper.addAnnotation(stamp, 1);
            stamper.close();
            reader.close();
        }

        private static PdfDictionary createMeasureDictionary() {
            PdfDictionary measureDictionary = new PdfDictionary(PdfName.MEASURE);
            measureDictionary.put(PdfName.R, new PdfString("1 cm = 1 cm"));

            PdfDictionary xDictionary = new PdfDictionary(PdfName.NUMBERFORMAT);

            xDictionary.put(PdfName.U, new PdfString("cm"));
            xDictionary.put(PdfName.C, new PdfNumber(0.0519548f));
            measureDictionary.put(PdfName.X, new PdfArray(xDictionary));

            PdfDictionary dDictionary = new PdfDictionary(PdfName.NUMBERFORMAT);
            dDictionary.put(PdfName.U, new PdfString("cm"));
            dDictionary.put(PdfName.C, new PdfNumber(1.0f));
            measureDictionary.put(PdfName.D, new PdfArray(dDictionary));

            PdfDictionary aDictionary = new PdfDictionary(PdfName.NUMBERFORMAT);
            aDictionary.put(PdfName.U, new PdfString("cm"));
            aDictionary.put(PdfName.C, new PdfNumber(1.0f));
            measureDictionary.put(PdfName.A, new PdfArray(aDictionary));

            return measureDictionary;
        }

}

@mkl im tagging you in case you have free time to check and guide. thank you.

回答1:

I do not fully follow your math.

Your line has a length of ca. 209.3 user space units. If you want that line to represent 14.5 cm, the X Conversion factor should be about 0.069273 and not your 0.0519548.

Setting

xDictionary.put(PdfName.C, new PdfNumber(0.069273f));

I get a PDF which upon opening shows

and after a slightest move (after which Adobe Reader rebuilds the appearance)

so no inches...