c#으로 만든 오토메이션 함수들
// 바탕쪽 만들기
private void Make_MasterPage()
{
hwp.HAction.GetDefault("MasterPage", hwp.HParameterSet.HMasterPage.HSet);
hwp.HParameterSet.HMasterPage.Duplicate = 0;
hwp.HParameterSet.HMasterPage.Front = 0;
hwp.HParameterSet.HMasterPage.Type = 0;
hwp.HParameterSet.HMasterPage.HSet.SetItem("ApplyTo", 2);
hwp.HAction.Execute("MasterPage", hwp.HParameterSet.HMasterPage.HSet);
}
// 표 만들기 (줄, 칸, 가로크기, 세로크기)
private void CreateTable(int row, int column, double width, double height)
{
hwp.HAction.GetDefault("TableCreate", hwp.HParameterSet.HTableCreation.HSet);
hwp.HParameterSet.HTableCreation.Rows = row;
hwp.HParameterSet.HTableCreation.Cols = column;
hwp.HParameterSet.HTableCreation.WidthType = 2; ;
hwp.HParameterSet.HTableCreation.HeightType = 1; ;
hwp.HParameterSet.HTableCreation.WidthValue = hwp.MiliToHwpUnit(width);
hwp.HParameterSet.HTableCreation.HeightValue = hwp.MiliToHwpUnit(height);
hwp.HParameterSet.HTableCreation.CreateItemArray("ColWidth", column); // 칸 수만큼 배열 생성
for (int i = 0; i < column; i++)
{
hwp.HParameterSet.HTableCreation.ColWidth.item[i] = hwp.MiliToHwpUnit((width / column) - 3.6); //칸마다 폭 크기 설정
// 칸마다 왼쪽 여백 1.8, 오른쪽 여백 1.8을 빼줘야 함
}
//hwp.HParameterSet.HTableCreation.ColWidth.item[0] = hwp.MiliToHwpUnit(16.0);
//hwp.HParameterSet.HTableCreation.ColWidth.item[1] = hwp.MiliToHwpUnit(36.0);
//hwp.HParameterSet.HTableCreation.ColWidth.item[2] = hwp.MiliToHwpUnit(46.0);
//hwp.HParameterSet.HTableCreation.ColWidth.item[3] = hwp.MiliToHwpUnit(16.0);
//hwp.HParameterSet.HTableCreation.ColWidth.item[4] = hwp.MiliToHwpUnit(16.0);;
hwp.HParameterSet.HTableCreation.CreateItemArray("RowHeight", row); ; // 행 수 만큼 배열 생성
for (int i = 0; i < row; i++)
{
hwp.HParameterSet.HTableCreation.RowHeight.item[i] = hwp.MiliToHwpUnit((height / row) - 1.0); // 행마다 높이 설정
}
//hwp.HParameterSet.HTableCreation.RowHeight.item[0] = hwp.MiliToHwpUnit(40.0);
//hwp.HParameterSet.HTableCreation.RowHeight.item[1] = hwp.MiliToHwpUnit(20.0);
//hwp.HParameterSet.HTableCreation.RowHeight.item[2] = hwp.MiliToHwpUnit(50.0);
//hwp.HParameterSet.HTableCreation.RowHeight.item[3] = hwp.MiliToHwpUnit(20.0);
//hwp.HParameterSet.HTableCreation.RowHeight.item[4] = hwp.MiliToHwpUnit(20.0);
//hwp.HParameterSet.HTableCreation.TableProperties.HorzOffset = hwp.MiliToHwpUnit(0.0);
//hwp.HParameterSet.HTableCreation.TableProperties.VertOffset = hwp.MiliToHwpUnit(0.0);
hwp.HParameterSet.HTableCreation.TableProperties.Width = mm2Hu(width);
hwp.HParameterSet.HTableCreation.TableProperties.OutsideMarginLeft = hwp.MiliToHwpUnit(0);
hwp.HParameterSet.HTableCreation.TableProperties.OutsideMarginRight = hwp.MiliToHwpUnit(0);
hwp.HParameterSet.HTableCreation.TableProperties.OutsideMarginTop = hwp.MiliToHwpUnit(0);
hwp.HParameterSet.HTableCreation.TableProperties.OutsideMarginBottom = hwp.MiliToHwpUnit(0);
hwp.HParameterSet.HTableCreation.TableProperties.TreatAsChar = 1; // # ;글자처럼 취급
hwp.HAction.Execute("TableCreate", hwp.HParameterSet.HTableCreation.HSet);
}
// 표의 현재 셀 너비 변경
private void ChangeCellWidth(double width)
{
hwp.HAction.GetDefault("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet);
hwp.HParameterSet.HShapeObject.HorzRelTo = hwp.HorzRel("Para");
hwp.HParameterSet.HShapeObject.HSet.SetItem("ShapeType", 3);
hwp.HParameterSet.HShapeObject.HSet.SetItem("ShapeCellSize", 1);
hwp.HParameterSet.HShapeObject.ShapeTableCell.Width = hwp.MiliToHwpUnit(width);
hwp.HAction.Execute("TablePropertyDialog", hwp.HParameterSet.HShapeObject.HSet);
}
// 선택 셀 글자크기 설정
private void CellTextPoint(float point_size)
{
hwp.HAction.GetDefault("CharShape", hwp.HParameterSet.HCharShape.HSet);
hwp.HParameterSet.HCharShape.FontTypeUser = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FontTypeSymbol = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FontTypeOther = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FontTypeJapanese = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FontTypeHanja = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FontTypeLatin = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FontTypeHangul = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.Height = hwp.PointToHwpUnit(point_size); // 글자크기 1
hwp.HAction.Execute("CharShape", hwp.HParameterSet.HCharShape.HSet);
}
// 표의 셀 배경색 넣기
private void CellBackColor(byte[] rgb)
{
hwp.HAction.GetDefault("CellFill", hwp.HParameterSet.HCellBorderFill.HSet);
hwp.HParameterSet.HCellBorderFill.FillAttr.Type = hwp.BrushType("NullBrush|WinBrush");
hwp.HParameterSet.HCellBorderFill.FillAttr.WinBrushFaceColor = hwp.RGBColor(rgb[0], rgb[1], rgb[2]);
hwp.HParameterSet.HCellBorderFill.FillAttr.WinBrushHatchColor = hwp.RGBColor(rgb[0], rgb[1], rgb[2]);
hwp.HParameterSet.HCellBorderFill.FillAttr.WinBrushFaceStyle = hwp.HatchStyle("None");
hwp.HParameterSet.HCellBorderFill.FillAttr.WindowsBrush = 1;
hwp.HAction.Execute("CellFill", hwp.HParameterSet.HCellBorderFill.HSet);
}
// 테두리 선 없애기
private void None_BoderLine()
{
hwp.HAction.GetDefault("CellBorder", hwp.HParameterSet.HCellBorderFill.HSet);
hwp.HParameterSet.HCellBorderFill.BorderTypeBottom = hwp.HwpLineType("None");
hwp.HParameterSet.HCellBorderFill.BorderTypeTop = hwp.HwpLineType("None");
hwp.HParameterSet.HCellBorderFill.BorderTypeRight = hwp.HwpLineType("None");
hwp.HParameterSet.HCellBorderFill.BorderTypeLeft = hwp.HwpLineType("None");
hwp.HParameterSet.HCellBorderFill.FillAttr.GradationAlpha = 0;
hwp.HParameterSet.HCellBorderFill.FillAttr.ImageAlpha = 0;
hwp.HAction.Execute("CellBorder", hwp.HParameterSet.HCellBorderFill.HSet);
//hwp.HAction.Run("Cancel"); // 선택 해제
}
// 글자 모양 변경(글꼴이름, 크기, 색상RGB)
private void ChangeTextStyle(string textstyle, float point, byte[] rgb)
{
hwp.HAction.GetDefault("CharShape", hwp.HParameterSet.HCharShape.HSet);
hwp.HParameterSet.HCharShape.FaceNameUser = textstyle;
hwp.HParameterSet.HCharShape.FontTypeUser = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FaceNameSymbol = textstyle;
hwp.HParameterSet.HCharShape.FontTypeSymbol = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FaceNameOther = textstyle;
hwp.HParameterSet.HCharShape.FontTypeOther = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FaceNameJapanese = textstyle;
hwp.HParameterSet.HCharShape.FontTypeJapanese = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FaceNameHanja = textstyle;
hwp.HParameterSet.HCharShape.FontTypeHanja = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FaceNameLatin = textstyle;
hwp.HParameterSet.HCharShape.FontTypeLatin = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.FaceNameHangul = textstyle;
hwp.HParameterSet.HCharShape.FontTypeHangul = hwp.FontType("TTF");
hwp.HParameterSet.HCharShape.TextColor = hwp.RGBColor(rgb[0], rgb[1], rgb[2]);
hwp.HParameterSet.HCharShape.Height = hwp.PointToHwpUnit(point);
hwp.HAction.Execute("CharShape", hwp.HParameterSet.HCharShape.HSet);
}
// 글자처럼 취급
private void ChangeLikeText()
{
var act = hwp.CreateAction("TablePropertyDialog");
var set = hwp.CreateSet("ShapeObject");
act.GetDefault(set);
set.SetItem("TreatAsChar", 1);
set.SetItem("Shapetype", 1);
act.Execute(set);
}
// 고치기 - 위치:종이 (글자처럼 취급x)
private void None_ChangeLiketext()
{
Debug.WriteLine("함수 : None_ChangeLiketext - 시작");
// 위치 종이
var act = hwp.CreateAction("TablePropertyDialog");
var set = act.CreateSet();
act.GetDefault(set);
set.SetItem("HorzRelTo", hwp.HorzRel("Paper")); // 가로 위치 : 종이
set.SetItem("VertRelTo", hwp.HorzRel("Paper")); // 세로 위치 : 종이
set.SetItem("TreatAsChar", 0); // 글자취급 x
set.SetItem("ShapeType", 6);
set.SetItem("OutsideMarginTop", 0); // 바깥여백 : 0
set.SetItem("OutsideMarginBottom", 0); // 바깥여백 : 0
set.SetItem("OutsideMarginRight", 0); // 바깥여백 : 0
set.SetItem("OutsideMarginLeft", 0); // 바깥여백 : 0
act.Execute(set);
Debug.WriteLine("함수 : None_ChangeLiketext - 완료");
}
// 글자처럼 취급 함수 연결
private void button8_Click(object sender, EventArgs e)
{
Debug.WriteLine("글자처럼 취급");
ChangeLikeText();
}
// 글자처럼 취급 X
private void button15_Click(object sender, EventArgs e)
{
Debug.WriteLine("글자처럼 취급 X");
hwp.Run("CloseEx"); // 표 빠져나오기
var code = hwp.FindCtrl(); // 컨트롤(표) 선택
if (code == "tbl" || code == "사각형")
{
None_ChangeLiketext();
hwp.Run("Cancel"); // 선택된 표 선택 해제
}
else { Debug.WriteLine("표 아님, 코드 : " + code); }
}
// 현재 커서 위치 정보 배열로 리턴 ([0]총구역, [1]현재구역, [2]쪽, [3]단, [4]줄, [5]칸, [6]컨트롤 종류)
private int[] Cursor_Info()
{
hwp.KeyIndicator(out int seccnt, out int secno, out int prnpageno, out int colno, out int line, out int pos, out short over, out string ctrlname);
int[] list = new int[6];
list[0] = seccnt; // 총 구역
list[1] = secno; // 구역 번호
list[2] = prnpageno; // 페이지 번호
list[3] = colno; // 다단 번호
list[4] = line; // 줄 번호
list[5] = pos; // 칸(컬럼) 번호
//short ins_over = over; // 삽입, 수정
return list;
}
private string get_Ctrl_ID()
{
try
{
hwp.KeyIndicator(out int seccnt, out int secno, out int prnpageno, out int colno, out int line, out int pos, out short over, out string ctrlname);
return ctrlname;
}
catch
{
return null;
}
}
// 표의 현재 커서 셀 위치 값 얻기 (A1 / E3 등등..)
private string Cell_addr()
{
try
{
string cell = get_Ctrl_ID();
cell = cell.Split('(', ')')[1];
return cell;
}
catch { return null; }
}
// 글자 입력
private void button1_Click(object sender, EventArgs e)
{
Gethwp();
Typing(textBox1.Text);
Cursor_Info();
}
// 표 안으로 들어가기
private void in_table()
{
hwp.Run("ShapeObjTableSelCell");
}
// 표 그리기
private void button2_Click(object sender, EventArgs e)
{
int row = decimal.ToInt32(numericUpDown1.Value);
int col = decimal.ToInt32(numericUpDown2.Value);
double[] pagesize = GetPageSize();
double width = Hu2mm(pagesize[0] - pagesize[4] - pagesize[5]); // 가로크기 - 좌,우 여백 (hwp unit to mm)
CreateTable(row, col, width, row * 10);
}
// 표 첫 줄 음영 주기
private void button3_Click(object sender, EventArgs e)
{
// 색상 설정 (표 배경색 버튼 색상값)
byte r = button76.BackColor.R;
byte g = button76.BackColor.G;
byte b = button76.BackColor.B;
byte[] backcolor = { r, g, b };
hwp.HAction.Run("Cancel"); // 표 셀 블럭 되어 있으면 해제
hwp.HAction.Run("TableCellBlock");
hwp.HAction.Run("TableColBegin");
hwp.HAction.Run("TableCellBlockExtend");
hwp.HAction.Run("TableColEnd");
hwp.HAction.GetDefault("CellFill", hwp.HParameterSet.HCellBorderFill.HSet);
hwp.HParameterSet.HCellBorderFill.FillAttr.Type = hwp.BrushType("NullBrush|WinBrush");
hwp.HParameterSet.HCellBorderFill.FillAttr.WinBrushFaceColor = hwp.RGBColor(r, g, b);
hwp.HParameterSet.HCellBorderFill.FillAttr.WinBrushHatchColor = hwp.RGBColor(153, 153, 153);
hwp.HParameterSet.HCellBorderFill.FillAttr.WinBrushFaceStyle = hwp.HatchStyle("None");
hwp.HParameterSet.HCellBorderFill.FillAttr.WindowsBrush = 1;
hwp.HAction.Execute("CellFill", hwp.HParameterSet.HCellBorderFill.HSet);
hwp.HAction.Run("Cancel");
}