套打.二开案例.后台创建动态表单套打导出
【场景】后台创建动态表单套打导出
【前置知识】
(0)[动态表单套打](https://vip.kingdee.com/article/434749865378241536?productLineId=1)
(1)[创建单据视图套打导出](https://vip.kingdee.com/article/136780524734740224)
【二开案例】在采购订单套打导出动态表单PDF文件
![image.webp](/download/0100f874ee09395f4d1fbe5c6fe065599bf0.webp)
```csharp
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.BOS.Web.Core;
using Kingdee.BOS.Web.DynamicForm;
using System;
using System.Collections.Generic;
using System.Data;
namespace DynamicFormPlugIn.NotePrint
{
[Kingdee.BOS.Util.HotUpdate]
[System.ComponentModel.Description("后台创建动态表单进行套打导出")]
public class BillPlugIn_ExportDynamicPdf:AbstractBillPlugIn
{
public override void BarItemClick(BarItemClickEventArgs e)
{
if (!string.Equals(e.BarItemKey, "tb_ExportDynamic", StringComparison.OrdinalIgnoreCase))
return;
string formId = "ka0aa985cfc5d41ed838cde76b833a455";
string tempId = "da156944-0ee7-4d26-a0de-4d21c1b6bdf1";
var sessionManager = CommonSession.GetCurrent(this.View.Context.UserToken).SessionManager;
//如果生成的地方没有Http请求,可以直接实例化一个
//SessionManager sessionManager = new SessionManager();
//<0>创建动态表单逻辑
var view = CreateDynamicFormView(this.View.Context, formId, sessionManager);
var viewService = view as IDynamicFormViewService;
if (viewService == null)
return;
//<1> 模拟录入部分字典,进行数据查询
view.Model.SetValue("FDate",DateTime.Now);
viewService.ButtonClick("FRefresh", "");
//<2> 生成动态表单导出文件
string filePath = string.Empty;
try
{
//<3>mark 生成前要注册视图
sessionManager.GetOrAdd(((AbstractDynamicWebFormView)view).PageId, view.GetType().Name, view);
//说明 dynamicformpreview 是动态表单中针对套打的单据内码
filePath = ExportTargetBill(view, formId, "dynamicformpreview", tempId);
this.View.ShowMessage(filePath);
}
finally
{
view.Close();
//<4>mark 生成完要反注册视图
sessionManager.Remove(((AbstractDynamicWebFormView)view).PageId, view.GetType().Name);
}
}
/// <summary>
/// 套打导出逻辑
/// </summary>
/// <param name="dynamicFormView"></param>
/// <param name="formId"></param>
/// <param name="billId"></param>
/// <param name="templateId"></param>
/// <returns></returns>
private static string ExportTargetBill(IDynamicFormView dynamicFormView, string formId, string billId, string templateId)
{
IDynamicFormViewService viewService = dynamicFormView as IDynamicFormViewService;
if (viewService == null)
return null;
try
{
List<string> billIds = new List<string>() { billId };
List<string> templateIds = new List<string>() { templateId };
PrintExportInfo pExInfo = new PrintExportInfo();
pExInfo.PageId = dynamicFormView.PageId;
pExInfo.FormId = formId;
pExInfo.BillIds = billIds; //单据内码
pExInfo.TemplateIds = templateIds; //套打模板ID
pExInfo.FileType = ExportFileType.PDF; //文件格式
pExInfo.ExportType = ExportType.ByPage; //导出格式
string fileName = Guid.NewGuid().ToString() + ".PDF";
string temppath = PathUtils.GetPhysicalPath("TempfilePath", fileName);
pExInfo.FilePath = temppath; //文件输出路径
viewService.ExportNotePrint(pExInfo);
if(pExInfo.ExportFileInfos == null || pExInfo.ExportFileInfos.Count<=0)
{
return null;
}
return pExInfo.ExportFileInfos[0].FilePath;
}
finally
{
}
}
/// <summary>
/// 创建视图
/// </summary>
/// <param name="ctx"></param>
/// <param name="formId"></param>
/// <param name="pkValue"></param>
/// <returns></returns>
public static IDynamicFormView CreateDynamicFormView(Context ctx, string formId, SessionManager sessionManager)
{
FormMetadata metadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
if (metadata == null)
return null;
var openParameter = CreateOpenParameter(ctx, metadata);
//这个地方增加视图会话管理器
openParameter.SetCustomParameter("SessionManager", sessionManager);
var provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider();
IDynamicFormViewService billView = (IDynamicFormViewService)provider.GetService(typeof(IDynamicFormView));
billView.Initialize(openParameter, provider);
billView.LoadData();
return (IDynamicFormView)billView;
}
private static DynamicFormOpenParameter CreateOpenParameter(Context ctx, FormMetadata metadata)
{
var form = metadata.BusinessInfo.GetForm();
DynamicFormOpenParameter openPara = new DynamicFormOpenParameter(form.Id, metadata.GetLayoutInfo().Id);
openPara.Context = ctx;
openPara.PageId = Guid.NewGuid().ToString();
openPara.Status = OperationStatus.VIEW;
openPara.FormMetaData = metadata;
openPara.SetCustomParameter(Kingdee.BOS.Core.FormConst.PlugIns, form.CreateFormPlugIns());
openPara.ServiceName = form.FormServiceName;
return openPara;
}
}
}
```
【效果】
![20230927 2037.webp](/download/0100d5ae5800a7574e7aa42b7c9e2e211bea.webp)
套打.二开案例.后台创建动态表单套打导出
【场景】后台创建动态表单套打导出【前置知识】(0)[动态表单套打](https://vip.kingdee.com/article/434749865378241536?productLineId=1...
点击下载文档
本文2024-09-23 04:12:13发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-163788.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章