如何创建一个复合对象FormXObjetct?

如果需要将部分几何对象合并为一个小组方便一起操作,可以使用复合对象实现FormXObject该功能。当然,我们在遍历文档中的几何对象时,也应当递归PDF中的ormXObject。

通常我们在页面中插入一个图片和path对象,用福昕PDF高级编辑器打开,右键左侧边栏,打开内容面板,可以看见“路径”和“图像”对象是相互独立的。如下:

而新建一个FormX 并将新增的path和图片对象放到FormX中,当选中FormX对象时,图像和路径对象可以同时选中。效果如下:

通过SDK创建FormX对象的示例代码如下:

static public void TestAddFormXObject()
 {
     PDFDoc doc = new PDFDoc();
     doc.Load(null);
     PDFPage page= doc.InsertPage(0, PDFPage.Size.e_SizeA4);
     page.StartParse(0, null, true);
     using (var formXObj=FormXObject.Create(doc))
     {
         GraphicsObjects objects= formXObj.GetGraphicsObjects();
         long position = objects.GetLastGraphicsObjectPosition(GraphicsObject.Type.e_TypeAll);
         //添加路径path对象 
         using (var path_object = PathObject.Create())
         {
             using (var path = new foxit.common.Path())
             {
                 path.MoveTo(new PointF(400, 500));
                 path.LineTo(new PointF(500, 500));
                 path.LineTo(new PointF(500, 400));
                 path.LineTo(new PointF(400, 400));
                 path.LineTo(new PointF(400, 500));
                 path_object.SetPathData(path);

                 path_object.SetFillColor(0xFF000000);
                 path_object.SetFillMode(FillMode.e_FillModeAlternate);
                 path_object.SetStrokeState(true);
                 path_object.SetStrokeColor(0xFFFF00FF);
                position= objects.InsertGraphicsObject(position, path_object);
             }
         }
         //添加图片
         using (var image_object = ImageObject.Create(doc))
         {
             foxit.common.Image image = new foxit.common.Image(input_path + "sdk.png");
             image_object.SetImage(image, 0);

             float width = image.GetWidth()/2;
             float height = image.GetHeight()/2;
             float page_width = page.GetWidth();
             float page_height = page.GetHeight();

             image_object.SetMatrix(new Matrix2D(width, 0, 0, height, page_width/2 - width , page_height/2 - height));
             objects.InsertGraphicsObject(position, image_object);

         }
         objects.GenerateContent();
         formXObj.SetMatrix(new Matrix2D(1, 0, 0, 1, 0, 0));
         //设置FormX的内容显示区域 BBox 
         formXObj.SetClipRect(new RectF(0, 0, page.GetWidth(),page.GetHeight()));
         formXObj.GetStream().GetDictionary().SetAtRect("BBox",formXObj.GetRect());

         page.InsertGraphicsObject(page.GetLastGraphicsObjectPosition(GraphicsObject.Type.e_TypeAll), formXObj);
     }
     page.GenerateContent();
     doc.SaveAs(@"D:\test\test_formx.pdf",0);
 }