한글파일에 이미지 첨부하는 기능 추가

main
이범준 1 year ago
parent edfbdc1f05
commit 3fef97ee5b

@ -0,0 +1,23 @@
package cokr.xit.fims.cmmn.hwp;
import kr.dogfoot.hwplib.object.HWPFile;
import kr.dogfoot.hwplib.object.bodytext.ParagraphListInterface;
import kr.dogfoot.hwplib.object.bodytext.paragraph.Paragraph;
import kr.dogfoot.hwplib.tool.paragraphadder.ParagraphAdder;
public class AddingParagraphBetweenHWPFile {
public static void fileMerge(HWPFile sourceHWPFile, HWPFile targetHWPFile) throws Exception {
if (sourceHWPFile != null && targetHWPFile != null) {
ParagraphListInterface targetFirstSection = targetHWPFile.getBodyText().getSectionList().get(0);
Paragraph sourceParagraph = sourceHWPFile.getBodyText().getSectionList().get(0).getParagraph(0);
ParagraphAdder paraAdder = new ParagraphAdder(targetHWPFile, targetFirstSection);
paraAdder.add(sourceHWPFile, sourceParagraph);
}
}
}

@ -0,0 +1,253 @@
package cokr.xit.fims.cmmn.hwp;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import kr.dogfoot.hwplib.object.HWPFile;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.CtrlHeaderGso;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.GsoHeaderProperty;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.HeightCriterion;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.HorzRelTo;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.ObjectNumberSort;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.RelativeArrange;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.TextFlowMethod;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.TextHorzArrange;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.VertRelTo;
import kr.dogfoot.hwplib.object.bodytext.control.ctrlheader.gso.WidthCriterion;
import kr.dogfoot.hwplib.object.bodytext.control.gso.ControlRectangle;
import kr.dogfoot.hwplib.object.bodytext.control.gso.GsoControlType;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.ShapeComponentNormal;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.lineinfo.LineArrowShape;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.lineinfo.LineArrowSize;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.lineinfo.LineEndShape;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.lineinfo.LineInfo;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.lineinfo.LineType;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.lineinfo.OutlineStyle;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.shadowinfo.ShadowInfo;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponent.shadowinfo.ShadowType;
import kr.dogfoot.hwplib.object.bodytext.control.gso.shapecomponenteach.ShapeComponentRectangle;
import kr.dogfoot.hwplib.object.bodytext.control.table.Cell;
import kr.dogfoot.hwplib.object.bodytext.paragraph.Paragraph;
import kr.dogfoot.hwplib.object.bodytext.paragraph.text.ParaText;
import kr.dogfoot.hwplib.object.docinfo.BinData;
import kr.dogfoot.hwplib.object.docinfo.bindata.BinDataCompress;
import kr.dogfoot.hwplib.object.docinfo.bindata.BinDataState;
import kr.dogfoot.hwplib.object.docinfo.bindata.BinDataType;
import kr.dogfoot.hwplib.object.docinfo.borderfill.fillinfo.FillInfo;
import kr.dogfoot.hwplib.object.docinfo.borderfill.fillinfo.ImageFill;
import kr.dogfoot.hwplib.object.docinfo.borderfill.fillinfo.ImageFillType;
import kr.dogfoot.hwplib.object.docinfo.borderfill.fillinfo.PictureEffect;
import kr.dogfoot.hwplib.tool.objectfinder.CellFinder;
public class InsertingImageCell {
public static void IMPL_InsertPicture(HWPFile hwpFile, String fieldName, String imgFilePath) throws IOException {
InsertingImageCell tii = new InsertingImageCell();
tii.insertShapeWithImage(hwpFile, fieldName, imgFilePath);
}
private final String imageFileExt = "jpg";
private final BinDataCompress compressMethod = BinDataCompress.ByStorageDefault;
private int instanceID = 0x5bb840e1;
private HWPFile hwpFile;
private String imageFilePath;
private String fieldName;
private int streamIndex;
private int binDataID;
private ControlRectangle rectangle;
private Rectangle shapePosition = new Rectangle(1, 1, 20, 20);
private void insertShapeWithImage(HWPFile hwpFile, String fieldName, String imgFilePath) throws IOException {
this.hwpFile = hwpFile;
this.fieldName = fieldName;
this.imageFilePath = imgFilePath;
addBinData();
binDataID = addBinDataInDocInfo(streamIndex);
addGsoControl();
}
private void addBinData() throws IOException {
streamIndex = hwpFile.getBinData().getEmbeddedBinaryDataList().size() + 1;
String streamName = getStreamName();
byte[] fileBinary = loadFile();
hwpFile.getBinData().addNewEmbeddedBinaryData(streamName, fileBinary, compressMethod);
}
private String getStreamName() {
return "Bin" + String.format("%04X", streamIndex) + "." + imageFileExt;
}
private byte[] loadFile() throws IOException {
File file = new File(imageFilePath);
byte[] buffer = new byte[(int) file.length()];
InputStream ios = null;
try {
ios = new FileInputStream(file);
ios.read(buffer);
} finally {
try {
if (ios != null)
ios.close();
} catch (IOException e) {
}
}
return buffer;
}
private int addBinDataInDocInfo(int streamIndex) {
BinData bd = new BinData();
bd.getProperty().setType(BinDataType.Embedding);
bd.getProperty().setCompress(compressMethod);
bd.getProperty().setState(BinDataState.NotAccess);
bd.setBinDataID(streamIndex);
bd.setExtensionForEmbedding(imageFileExt);
hwpFile.getDocInfo().getBinDataList().add(bd);
return hwpFile.getDocInfo().getBinDataList().size();
}
private void addGsoControl() {
createRectangleControlAtCell();
setCtrlHeaderGso();
setShapeComponent();
setShapeComponentRectangle();
}
private void createRectangleControlAtCell() {
ArrayList<Cell> cellList = CellFinder.findAll(hwpFile, fieldName);
for (Cell c : cellList) {
int cellWidth = (int)c.getListHeader().getWidth();
int cellHeight = (int)c.getListHeader().getHeight();
shapePosition = new Rectangle(1, 1, cellWidth, cellHeight);
Paragraph firstPara = c.getParagraphList().getParagraph(0);
ParaText paraText = firstPara.getText();
if (paraText == null) {
firstPara.createText();
paraText = firstPara.getText();
}
// 문단에서 사각형 컨트롤의 위치를 표현하기 위한 확장 문자를 넣는다.
paraText.addExtendCharForGSO();
// 문단에 사각형 컨트롤 추가한다.
rectangle = (ControlRectangle) firstPara.addNewGsoControl(GsoControlType.Rectangle);
break;
}
}
private void setCtrlHeaderGso() {
CtrlHeaderGso hdr = rectangle.getHeader();
GsoHeaderProperty prop = hdr.getProperty();
prop.setLikeWord(false);
prop.setApplyLineSpace(false);
prop.setVertRelTo(VertRelTo.Para);
prop.setVertRelativeArrange(RelativeArrange.TopOrLeft);
prop.setHorzRelTo(HorzRelTo.Para);
prop.setHorzRelativeArrange(RelativeArrange.TopOrLeft);
prop.setVertRelToParaLimit(true);
prop.setAllowOverlap(true);
prop.setWidthCriterion(WidthCriterion.Absolute);
prop.setHeightCriterion(HeightCriterion.Absolute);
prop.setProtectSize(false);
prop.setTextFlowMethod(TextFlowMethod.FitWithText);
prop.setTextHorzArrange(TextHorzArrange.BothSides);
prop.setObjectNumberSort(ObjectNumberSort.Figure);
hdr.setyOffset(shapePosition.y);
hdr.setxOffset(shapePosition.x);
hdr.setWidth(shapePosition.width);
hdr.setHeight(shapePosition.height);
hdr.setzOrder(0);
hdr.setOutterMarginLeft(0);
hdr.setOutterMarginRight(0);
hdr.setOutterMarginTop(0);
hdr.setOutterMarginBottom(0);
hdr.setInstanceId(instanceID);
hdr.setPreventPageDivide(false);
hdr.getExplanation().setBytes(null);
}
private int fromMM(int mm) {
if (mm == 0) {
return 1;
}
return (int) ((double) mm * 72000.0f / 254.0f + 0.5f);
}
private void setShapeComponent() {
ShapeComponentNormal sc = (ShapeComponentNormal) rectangle.getShapeComponent();
sc.setOffsetX(0);
sc.setOffsetY(0);
sc.setGroupingCount(0);
sc.setLocalFileVersion(1);
sc.setWidthAtCreate(shapePosition.width);
sc.setHeightAtCreate(shapePosition.height);
sc.setWidthAtCurrent(shapePosition.width);
sc.setHeightAtCurrent(shapePosition.height);
sc.setRotateAngle(0);
sc.setRotateXCenter((shapePosition.width / 2));
sc.setRotateYCenter((shapePosition.height / 2));
sc.createLineInfo();
LineInfo li = sc.getLineInfo();
li.getProperty().setLineEndShape(LineEndShape.Flat);
li.getProperty().setStartArrowShape(LineArrowShape.None);
li.getProperty().setStartArrowSize(LineArrowSize.MiddleMiddle);
li.getProperty().setEndArrowShape(LineArrowShape.None);
li.getProperty().setEndArrowSize(LineArrowSize.MiddleMiddle);
li.getProperty().setFillStartArrow(true);
li.getProperty().setFillEndArrow(true);
li.getProperty().setLineType(LineType.None);
li.setOutlineStyle(OutlineStyle.Normal);
li.setThickness(0);
li.getColor().setValue(0);
sc.createFillInfo();
FillInfo fi = sc.getFillInfo();
fi.getType().setPatternFill(false);
fi.getType().setImageFill(true);
fi.getType().setGradientFill(false);
fi.createImageFill();
ImageFill imgF = fi.getImageFill();
imgF.setImageFillType(ImageFillType.FitSize);
imgF.getPictureInfo().setBrightness((byte) 0);
imgF.getPictureInfo().setContrast((byte) 0);
imgF.getPictureInfo().setEffect(PictureEffect.RealPicture);
imgF.getPictureInfo().setBinItemID(binDataID);
sc.createShadowInfo();
ShadowInfo si = sc.getShadowInfo();
si.setType(ShadowType.None);
si.getColor().setValue(0xc4c4c4);
si.setOffsetX(283);
si.setOffsetY(283);
si.setTransparent((short) 0);
sc.setMatrixsNormal();
}
private void setShapeComponentRectangle() {
ShapeComponentRectangle scr = rectangle.getShapeComponentRectangle();
scr.setRoundRate((byte) 0);
scr.setX1(0);
scr.setY1(0);
scr.setX2(shapePosition.width);
scr.setY2(0);
scr.setX3(shapePosition.width);
scr.setY3(shapePosition.height);
scr.setX4(0);
scr.setY4(shapePosition.height);
}
}

@ -11,8 +11,12 @@ import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import cokr.xit.base.file.FileInfo;
import cokr.xit.base.file.service.FileQuery;
import cokr.xit.base.file.service.FileService;
import cokr.xit.fims.cmmn.CmmnUtil;
import cokr.xit.fims.cmmn.hwp.AddingParagraphBetweenHWPFile;
import cokr.xit.fims.cmmn.hwp.InsertingImageCell;
import cokr.xit.fims.crdn.Crdn;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.dao.Crdn01Mapper;
import cokr.xit.fims.crdn.service.CrdnService;
@ -29,6 +33,9 @@ import kr.dogfoot.hwplib.writer.HWPWriter;
@Service("crdn01Service")
public class Crdn01ServiceBean extends AbstractServiceBean implements CrdnService {
@Resource(name="fileService")
protected FileService fileService;
@Resource(name="crdn01Mapper")
protected Crdn01Mapper crdn01Mapper;
@ -58,7 +65,7 @@ public class Crdn01ServiceBean extends AbstractServiceBean implements CrdnServic
return crdn01Bean.removeCivilComplaint(crdnIDs);
}
public ModelAndView makeFileFromHwpFormat(CrdnQuery crdnQuery, String formatFilePath) {
public ModelAndView makeFileFromHwpFormat(CrdnQuery crdnQuery, String baseFormatFilePath, String attachFormatFilePath) {
crdnQuery.setView("hwp");
DataObject dataObject = crdn01Bean.getCivilComplaintOriginalInfo(crdnQuery);
@ -66,51 +73,67 @@ public class Crdn01ServiceBean extends AbstractServiceBean implements CrdnServic
SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat ymdHypenFormat = new SimpleDateFormat("yyyy-MM-dd");
HWPFile hwpFile;
try {
hwpFile = HWPReader.fromFile(formatFilePath);
HWPFile baseFormatFile = HWPReader.fromFile(baseFormatFilePath);
ArrayList<String> strings = new ArrayList<String>();
strings.add(dataObject.string("CVLCPT_APLCNT_NM"));
FieldFinder.setClickHereText(hwpFile, "신청자", strings);
FieldFinder.setClickHereText(baseFormatFile, "신청자", strings);
strings = new ArrayList<String>();
String cvlcptRcptYmd = dataObject.string("CVLCPT_RCPT_YMD");
cvlcptRcptYmd = ymdHypenFormat.format(ymdFormat.parse(cvlcptRcptYmd));
strings.add(cvlcptRcptYmd);
FieldFinder.setClickHereText(hwpFile, "접수일자", strings);
FieldFinder.setClickHereText(baseFormatFile, "접수일자", strings);
strings = new ArrayList<String>();
String cvlcptRrcsPrnmntDt = dataObject.string("CVLCPT_PRCS_PRNMNT_DT");
cvlcptRrcsPrnmntDt = ymdHypenFormat.format(ymdFormat.parse(cvlcptRrcsPrnmntDt));
strings.add(cvlcptRrcsPrnmntDt);
FieldFinder.setClickHereText(hwpFile, "처리기한", strings);
FieldFinder.setClickHereText(baseFormatFile, "처리기한", strings);
strings = new ArrayList<String>();
strings.add(dataObject.string("CVLCPT_RCPT_NO"));
FieldFinder.setClickHereText(hwpFile, "접수번호", strings);
FieldFinder.setClickHereText(baseFormatFile, "접수번호", strings);
strings = new ArrayList<String>();
String cvlcptAplyCn = dataObject.string("CVLCPT_APLY_CN");
cvlcptAplyCn = CmmnUtil.escapeHTMLEntity(cvlcptAplyCn);
strings.add(cvlcptAplyCn);
FieldFinder.setClickHereText(hwpFile, "민원내용", strings);
//TODO : 사진삽입
FieldFinder.setClickHereText(baseFormatFile, "민원내용", strings);
String currentTime = ymdhmsFormat.format(new Date());
String resultPath = "files" + File.separator + "result" + File.separator + currentTime + ".hwp";
HWPWriter.toFile(hwpFile, resultPath);
HWPWriter.toFile(baseFormatFile, resultPath);
FileInfo fileInfo = new FileInfo();
fileInfo.setPath(resultPath);
fileInfo.setName(currentTime+".hwp");
fileInfo.setMimeType("application/vnd.hancom.hwpx");
fileInfo.setExtension("hwp");
fileInfo.setSize(new File(resultPath).length());
HWPFile resultFile = HWPReader.fromFile(resultPath);
String downlaodFileName = "민원내역원본_" + currentTime + ".hwp";
FileQuery fileQuery = new FileQuery();
fileQuery.setInfoType(Crdn.INF_TYPE);
fileQuery.setInfoKeys(crdnQuery.getCrdnId());
List<DataObject> fileInfoList = fileService.getFileList(fileQuery);
if(fileInfoList != null && fileInfoList.size() > 0) {
int photoCnt = fileInfoList.size();
int PHOTO_COUNT_PER_PAGE = 4;
for(int i=0; i < photoCnt; i += PHOTO_COUNT_PER_PAGE) {
HWPFile attachFormatFile = HWPReader.fromFile(attachFormatFilePath);
InsertingImageCell.IMPL_InsertPicture(attachFormatFile, "왼쪽위사진", fileInfoList.get(i).string("URL"));
if(i+1 < photoCnt)
InsertingImageCell.IMPL_InsertPicture(attachFormatFile, "오른쪽위사진", fileInfoList.get(i+1).string("URL"));
if(i+2 < photoCnt)
InsertingImageCell.IMPL_InsertPicture(attachFormatFile, "왼쪽아래사진", fileInfoList.get(i+2).string("URL"));
if(i+3 < photoCnt)
InsertingImageCell.IMPL_InsertPicture(attachFormatFile, "오른쪽아래사진", fileInfoList.get(i+3).string("URL"));
AddingParagraphBetweenHWPFile.fileMerge(attachFormatFile, resultFile);
}
HWPWriter.toFile(resultFile, resultPath);
}
String downlaodFileName = "민원내역원본_" + currentTime + ".hwp";
ModelAndView mav = new ModelAndView("jsonView");
mav.addObject("filePath", resultPath);
mav.addObject("fileName", downlaodFileName);
@ -123,4 +146,5 @@ public class Crdn01ServiceBean extends AbstractServiceBean implements CrdnServic
}
}

@ -181,8 +181,9 @@ public class Crdn01Controller extends ApplicationController {
@RequestMapping(name="민원내역 원본 한글 파일 생성", value="/050/makeFileFromHwpFormat.do")
public ModelAndView makeFileFromHwpFormat(CrdnQuery crdnQuery, HttpServletRequest request) {
String formatFilePath = request.getServletContext().getRealPath("resources/format/cvlcptOrgnl.hwp");
String baseFormatFilePath = request.getServletContext().getRealPath("resources/format/cvlcptOrgnl_text.hwp");
String attachFormatFilePath = request.getServletContext().getRealPath("resources/format/cvlcptOrgnl_photo.hwp");
return crdn01Service.makeFileFromHwpFormat(crdnQuery, formatFilePath);
return crdn01Service.makeFileFromHwpFormat(crdnQuery, baseFormatFilePath, attachFormatFilePath);
}
}

Loading…
Cancel
Save