首页 > 代码库 > ABCpdf.NET中Rect,Bottom,Height的关系

ABCpdf.NET中Rect,Bottom,Height的关系

因为项目需要新功能,要在一个图片的上下加上固定高度的白边如下图。项目中一直使用ABCpdf.NET处理图片,但我一直没有做这方面的功能,所以找来API参考做。

7-31-2014 4-32-20 PM

这个简单需求做的过程中出现了一些曲折,主要是ABCpdf.NET的坐标体系和想象中的不一样。

它坐标系不同于Windows所用的左上为原点的坐标系, 它是采用左下为坐标原点的(可以通过设置Doc的TopDown为true来改变原点的位置)。Doc.Rect属性很重要, 如果要输出什么对象的话, 都是输出到Rect所指定的矩形范围内,Rect的值在操作过程中不断被覆盖更新,但是之前加入的对象的位置不受影响。

对于我需要加入对象的位置主要受以下属性影响Rect,Bottom,Height

            Doc theDoc = new Doc(); //theDoc.Rect= "0 0 612 792" theDoc.Rect.Height = 792.0            XImage theImg = new XImage();            theImg.SetFile("MBC140448XX-HF02-0101bl.tif");            theDoc.Rect.Left = 0;            theDoc.Rect.Bottom = 27; // theDoc.Rect = "0 27 612 792" theDoc.Rect.Height=765            theDoc.Rect.Width = theImg.Width; //theImg.Width = 1200 theDoc.Rect = "0 27 1200 792" theDoc.Rect.Height=765            theDoc.Rect.Height = theImg.Height;//theImg.Height = 1200 theDoc.Rect = "0 27 1200 777" theDoc.Rect.Height=750            theDoc.AddImageObject(theImg, false);            theDoc.Rect.Height = theImg.Height + 27; //theDoc.Rect= "0 27 1200 804" theDoc.Rect.Height = 777.0            theDoc.Rect.Left = 0;            theDoc.Rect.Bottom = 0; //theDoc.Rect= "0 0 1200 804" theDoc.Rect.Height = 804.0            theDoc.Rendering.Save("out.tif");            theDoc.Clear();

以上代码后面注释的值都是本行语句执行后的结果。通过以上结果分析,总结得到下面规律。本段代码主要是对Y方向的操作,所以主要考虑Rect = {X, Y, W, H}的Rect.H,Bottom,Height.

Rect.H = Bottom+Height

  • theDoc.Rect.Bottom = 27  Bottom变化,Rect.Y随之变化,Rect.H没有变化,但是Height变了(Height=792-27=765,原始默认值792).所以,Bottom变,Rect.H不变,Height变
  • theDoc.Rect.Height = theImg.Height;              Height变化,Rect.H随之变化,Rect.H = Bottom + Height=27+750=777. 所以,Bottom不变,Height变,Rect.H随之变化

没有查看源码,根据结果总结以上结论,对我当前的需求是使用的。