Mail MergePDF단어 변환 방법
Contents
[
Hide
]
이 문서에서는PDF에서 변환 한 다음PDF저장 한 단어 문서에서Mail Merge하는 방법을 보여주는 간단한 예를 보여줍니다. Aspose.Words을 사용하여PDF에서 단어 변환 파일에 대한 간단한Mail Merge프로세스를 실행하는 것은 경우에 따라 작동하지 않습니다. PDF을DOCX(워드 문서)로 변환하는 동안Aspose.PDF
이 실제MERGEFIELDs를 쓰지 않기 때문에 문제가 발생합니다. 그러나 이러한 정적 텍스트를 실제MERGEFIELDs로 변환 한 다음Mail Merge작업을 실행하여 달성 할 수 있습니다. 다음 해결 방법을 참조하십시오.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String dataDir = Utils.getDataDir(BubbleChart.class); | |
String fileName = "Converted.pdf"; | |
com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(dataDir + fileName); | |
ByteArrayOutputStream baOs = new ByteArrayOutputStream(); | |
//Converting PDF document to word document | |
pdfDoc.save(baOs, com.aspose.pdf.SaveFormat.DocX); | |
String[] referenceFields = {"FIRSTNAME", "MEMFIRST"}; | |
Object[] referenceValues = {"AAA", "BBB"}; | |
Document document = mailMergeTemplate(baOs.toByteArray(), referenceFields, referenceValues); | |
document.save(dataDir + "Saved.pdf"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static Document mailMergeTemplate(byte[] templateFile, String[] referenceFields, Object[] referenceValues) throws Exception { | |
Document doc = null; | |
try { | |
doc = new Document(new ByteArrayInputStream(templateFile)); | |
FindReplaceOptions opts = new FindReplaceOptions(); | |
opts.setFindWholeWordsOnly(false); | |
opts.setReplacingCallback(new ReplaceEvaluatorFindAndInsertMergefield()); | |
doc.getRange().replace(Pattern.compile("�(.*?)�"), "", opts); | |
doc.getMailMerge().setFieldMergingCallback(new HandleMergeFields()); | |
doc.getMailMerge().execute(referenceFields, referenceValues); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return doc; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
/** | |
* This is called when mail merge engine encounters plain text (non image) merge | |
*/ | |
static class HandleMergeFields implements IFieldMergingCallback { | |
public void fieldMerging(FieldMergingArgs args) throws Exception { | |
System.out.println("Mail merge for field : " + args.getFieldName() + " & Value : " + args.getFieldValue()); | |
} | |
/** | |
* This is called when mail merge engine encounters Image:XXX merge | |
* field in the document. You have a chance to return an Image object, | |
* file name or a stream that contains the image. | |
*/ | |
public void imageFieldMerging(ImageFieldMergingArgs e) throws Exception { | |
System.out.println("Mail merge for field : " + e.getFieldName() + " & Value : " + e.getFieldValue()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
static class ReplaceEvaluatorFindAndInsertMergefield implements IReplacingCallback { | |
public int replacing(ReplacingArgs e) throws Exception { | |
// This is a Run node that contains either the beginning or the complete match. | |
Node currentNode = e.getMatchNode(); | |
// The first (and may be the only) run can contain text before the match, | |
// in this case it is necessary to split the run. | |
if (e.getMatchOffset() > 0) | |
currentNode = splitRun((Run) currentNode, e.getMatchOffset()); | |
ArrayList runs = new ArrayList(); | |
// Find all runs that contain parts of the match string. | |
int remainingLength = e.getMatch().group().length(); | |
while ((remainingLength > 0) && (currentNode != null) && (currentNode.getText().length() <= remainingLength)) { | |
runs.add(currentNode); | |
remainingLength = remainingLength - currentNode.getText().length(); | |
// Select the next Run node. | |
// Have to loop because there could be other nodes such as BookmarkStart etc. | |
do { | |
currentNode = currentNode.getNextSibling(); | |
} while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN)); | |
} | |
// Split the last run that contains the match if there is any text left. | |
if ((currentNode != null) && (remainingLength > 0)) { | |
splitRun((Run) currentNode, remainingLength); | |
runs.add(currentNode); | |
} | |
//Change static text to real merge fields. | |
DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument()); | |
builder.moveTo((Run) runs.get(runs.size() - 1)); | |
builder.insertField("MERGEFIELD \"" + e.getMatch().group(1) + "\""); | |
for (Run run : (Iterable<Run>) runs) | |
run.remove(); | |
// Signal to the replace engine to do nothing because we have already done all what we wanted. | |
return ReplaceAction.SKIP; | |
} | |
/** | |
* Splits text of the specified run into two runs. Inserts the new run just | |
* after the specified run. | |
*/ | |
private Run splitRun(Run run, int position) throws Exception { | |
Run afterRun = (Run) run.deepClone(true); | |
afterRun.setText(run.getText().substring(position)); | |
run.setText(run.getText().substring((0), (0) + (position))); | |
run.getParentNode().insertAfter(afterRun, run); | |
return afterRun; | |
} | |
} |