为距离注释创建度量字典 [英] Creating measure dictionary for distance annotation

查看:29
本文介绍了为距离注释创建度量字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建测量距离注释不会显示正确的测量单位,也不会给出正确的计算.知道我做错了哪一部分吗?或者如果有一些数据缺失.

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.

请看图片.这是我用来创建距离注释的一些应用程序,我将其校准为 14.5 厘米,因此通过将其除以像素,每个像素的校准值为 0.0519548.

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.

现在,当我将它应用到 iText 代码时,我很困惑为什么显示总是以英寸为单位?即使我将代码设置为英寸而不是厘米,计算也是不正确的.

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 我会标记你,以防你有空闲时间检查和指导.谢谢.

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

推荐答案

我没有完全理解你的数学.

I do not fully follow your math.

您的行的长度为 ca.209.3 用户空间单位.如果你想让这条线代表 14.5 厘米,X C转换因子应该是 0.069273 而不是你的 0.0519548>.

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.

设置

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

我得到一个打开时显示的 PDF

I get a PDF which upon opening shows

稍微移动一下(之后 Adob​​e Reader 重建外观)

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

所以没有英寸...

这篇关于为距离注释创建度量字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆