Merging of different file format into single pdf using itext is giving corrupt pdf











up vote
0
down vote

favorite












I have a multipart form in which different type of files will be uploaded in single request like pdf,png,jpg etc. I need to read these files and create final merged single pdf file. Does iText will support this kind of merger of different file types.



I can able to merge all image type but when i try to merge pdf with imgae getting some issues.Any sample example on this.Below code creating final pdf without images.



public void generateMergedPDF(Map<String, String> dataMap, MultipartFile files) throws Exception {

ClassPathResource resource = new ClassPathResource("templatepdfForm.pdf");

FileOutputStream userInfo = new FileOutputStream(new File("C:\pdf\megepath\updateddocument.pdf"));

//Update filed values to template Starts
PdfReader reader = new PdfReader(resource.getInputStream());
PdfStamper stamper = new PdfStamper(reader, userInfo);

stamper.setFormFlattening(true);
AcroFields form = stamper.getAcroFields();

Map<String, Item> fieldMap = form.getFields();

for (String key : fieldMap.keySet()) {
String fieldValue = dataMap.get(key);
if (fieldValue != null) {
form.setField(key, fieldValue);
}
}

stamper.close();

//Update filed values to template Ends
Document mergePdfDoc = new Document();
PdfCopy pdfCopy;
boolean smartCopy = false;

FileOutputStream finalFile = new FileOutputStream("C:\pdf\finalmergedfile.pdf");

//Merge updated pdf with multipart content
if(smartCopy)
pdfCopy = new PdfSmartCopy(mergePdfDoc, finalFile);
else
pdfCopy = new PdfCopy(mergePdfDoc, finalFile);

PdfWriter writer = PdfWriter.getInstance(mergePdfDoc, finalFile);

mergePdfDoc.open();
PdfReader mergeReader = new PdfReader(new FileInputStream(new File("C:\pdf\megepath\updateddocument.pdf")));

pdfCopy.addDocument(mergeReader);
pdfCopy.freeReader(mergeReader);
mergeReader.close();

PdfReader pdfReader = new PdfReader[files.length];

for(int i=0; i<=files.length-1;i++) {
if(FileContentType.APPLICATION_TYPE.getContentTypes().contains(files[i].getContentType())) {
//To add multipart pdf content
pdfReader[i] = new PdfReader(files[i].getInputStream());
pdfCopy.addDocument(pdfReader[i]);
pdfCopy.freeReader(pdfReader[i]);
pdfReader[i].close();
}else if(FileContentType.IMAGE_TYPE.getContentTypes().contains(files[i].getContentType())) {
//To add multipart image content
System.out.println("Image Type Loop");
Image fileImage = Image.getInstance(files[i].getBytes());
mergePdfDoc.setPageSize(fileImage);
mergePdfDoc.newPage();
claimImage.setAbsolutePosition(0, 0);
mergePdfDoc.add(fileImage);
}
}
pdfCopy.setMergeFields();
//mergePdfDoc.close(); //If i enable this close stream closed error
//writer.close(); //If i enable this close stream closed error
memInfo.close();

finalFile.close();
}









share|improve this question




















  • 2




    Your actual problem appears to be "but when i try to merge pdf with imgae getting some issues". How about making that your question topic? Like showing what you've tried and explaining how that failed.
    – mkl
    Nov 14 at 15:47












  • @mkl i have updated sample programme
    – springbootlearner
    Nov 14 at 16:14






  • 1




    Your code uses one Document instance (mergePdfDoc) both for one PdfCopy (pdfCopy) and a PdfWriter (writer) which both use the same target file finalFile. This in most cases results in broken PDF results (if there are any at all) because you effectively have to separate PDF creators writing to the same file concurrently.
    – mkl
    Nov 19 at 16:20















up vote
0
down vote

favorite












I have a multipart form in which different type of files will be uploaded in single request like pdf,png,jpg etc. I need to read these files and create final merged single pdf file. Does iText will support this kind of merger of different file types.



I can able to merge all image type but when i try to merge pdf with imgae getting some issues.Any sample example on this.Below code creating final pdf without images.



public void generateMergedPDF(Map<String, String> dataMap, MultipartFile files) throws Exception {

ClassPathResource resource = new ClassPathResource("templatepdfForm.pdf");

FileOutputStream userInfo = new FileOutputStream(new File("C:\pdf\megepath\updateddocument.pdf"));

//Update filed values to template Starts
PdfReader reader = new PdfReader(resource.getInputStream());
PdfStamper stamper = new PdfStamper(reader, userInfo);

stamper.setFormFlattening(true);
AcroFields form = stamper.getAcroFields();

Map<String, Item> fieldMap = form.getFields();

for (String key : fieldMap.keySet()) {
String fieldValue = dataMap.get(key);
if (fieldValue != null) {
form.setField(key, fieldValue);
}
}

stamper.close();

//Update filed values to template Ends
Document mergePdfDoc = new Document();
PdfCopy pdfCopy;
boolean smartCopy = false;

FileOutputStream finalFile = new FileOutputStream("C:\pdf\finalmergedfile.pdf");

//Merge updated pdf with multipart content
if(smartCopy)
pdfCopy = new PdfSmartCopy(mergePdfDoc, finalFile);
else
pdfCopy = new PdfCopy(mergePdfDoc, finalFile);

PdfWriter writer = PdfWriter.getInstance(mergePdfDoc, finalFile);

mergePdfDoc.open();
PdfReader mergeReader = new PdfReader(new FileInputStream(new File("C:\pdf\megepath\updateddocument.pdf")));

pdfCopy.addDocument(mergeReader);
pdfCopy.freeReader(mergeReader);
mergeReader.close();

PdfReader pdfReader = new PdfReader[files.length];

for(int i=0; i<=files.length-1;i++) {
if(FileContentType.APPLICATION_TYPE.getContentTypes().contains(files[i].getContentType())) {
//To add multipart pdf content
pdfReader[i] = new PdfReader(files[i].getInputStream());
pdfCopy.addDocument(pdfReader[i]);
pdfCopy.freeReader(pdfReader[i]);
pdfReader[i].close();
}else if(FileContentType.IMAGE_TYPE.getContentTypes().contains(files[i].getContentType())) {
//To add multipart image content
System.out.println("Image Type Loop");
Image fileImage = Image.getInstance(files[i].getBytes());
mergePdfDoc.setPageSize(fileImage);
mergePdfDoc.newPage();
claimImage.setAbsolutePosition(0, 0);
mergePdfDoc.add(fileImage);
}
}
pdfCopy.setMergeFields();
//mergePdfDoc.close(); //If i enable this close stream closed error
//writer.close(); //If i enable this close stream closed error
memInfo.close();

finalFile.close();
}









share|improve this question




















  • 2




    Your actual problem appears to be "but when i try to merge pdf with imgae getting some issues". How about making that your question topic? Like showing what you've tried and explaining how that failed.
    – mkl
    Nov 14 at 15:47












  • @mkl i have updated sample programme
    – springbootlearner
    Nov 14 at 16:14






  • 1




    Your code uses one Document instance (mergePdfDoc) both for one PdfCopy (pdfCopy) and a PdfWriter (writer) which both use the same target file finalFile. This in most cases results in broken PDF results (if there are any at all) because you effectively have to separate PDF creators writing to the same file concurrently.
    – mkl
    Nov 19 at 16:20













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a multipart form in which different type of files will be uploaded in single request like pdf,png,jpg etc. I need to read these files and create final merged single pdf file. Does iText will support this kind of merger of different file types.



I can able to merge all image type but when i try to merge pdf with imgae getting some issues.Any sample example on this.Below code creating final pdf without images.



public void generateMergedPDF(Map<String, String> dataMap, MultipartFile files) throws Exception {

ClassPathResource resource = new ClassPathResource("templatepdfForm.pdf");

FileOutputStream userInfo = new FileOutputStream(new File("C:\pdf\megepath\updateddocument.pdf"));

//Update filed values to template Starts
PdfReader reader = new PdfReader(resource.getInputStream());
PdfStamper stamper = new PdfStamper(reader, userInfo);

stamper.setFormFlattening(true);
AcroFields form = stamper.getAcroFields();

Map<String, Item> fieldMap = form.getFields();

for (String key : fieldMap.keySet()) {
String fieldValue = dataMap.get(key);
if (fieldValue != null) {
form.setField(key, fieldValue);
}
}

stamper.close();

//Update filed values to template Ends
Document mergePdfDoc = new Document();
PdfCopy pdfCopy;
boolean smartCopy = false;

FileOutputStream finalFile = new FileOutputStream("C:\pdf\finalmergedfile.pdf");

//Merge updated pdf with multipart content
if(smartCopy)
pdfCopy = new PdfSmartCopy(mergePdfDoc, finalFile);
else
pdfCopy = new PdfCopy(mergePdfDoc, finalFile);

PdfWriter writer = PdfWriter.getInstance(mergePdfDoc, finalFile);

mergePdfDoc.open();
PdfReader mergeReader = new PdfReader(new FileInputStream(new File("C:\pdf\megepath\updateddocument.pdf")));

pdfCopy.addDocument(mergeReader);
pdfCopy.freeReader(mergeReader);
mergeReader.close();

PdfReader pdfReader = new PdfReader[files.length];

for(int i=0; i<=files.length-1;i++) {
if(FileContentType.APPLICATION_TYPE.getContentTypes().contains(files[i].getContentType())) {
//To add multipart pdf content
pdfReader[i] = new PdfReader(files[i].getInputStream());
pdfCopy.addDocument(pdfReader[i]);
pdfCopy.freeReader(pdfReader[i]);
pdfReader[i].close();
}else if(FileContentType.IMAGE_TYPE.getContentTypes().contains(files[i].getContentType())) {
//To add multipart image content
System.out.println("Image Type Loop");
Image fileImage = Image.getInstance(files[i].getBytes());
mergePdfDoc.setPageSize(fileImage);
mergePdfDoc.newPage();
claimImage.setAbsolutePosition(0, 0);
mergePdfDoc.add(fileImage);
}
}
pdfCopy.setMergeFields();
//mergePdfDoc.close(); //If i enable this close stream closed error
//writer.close(); //If i enable this close stream closed error
memInfo.close();

finalFile.close();
}









share|improve this question















I have a multipart form in which different type of files will be uploaded in single request like pdf,png,jpg etc. I need to read these files and create final merged single pdf file. Does iText will support this kind of merger of different file types.



I can able to merge all image type but when i try to merge pdf with imgae getting some issues.Any sample example on this.Below code creating final pdf without images.



public void generateMergedPDF(Map<String, String> dataMap, MultipartFile files) throws Exception {

ClassPathResource resource = new ClassPathResource("templatepdfForm.pdf");

FileOutputStream userInfo = new FileOutputStream(new File("C:\pdf\megepath\updateddocument.pdf"));

//Update filed values to template Starts
PdfReader reader = new PdfReader(resource.getInputStream());
PdfStamper stamper = new PdfStamper(reader, userInfo);

stamper.setFormFlattening(true);
AcroFields form = stamper.getAcroFields();

Map<String, Item> fieldMap = form.getFields();

for (String key : fieldMap.keySet()) {
String fieldValue = dataMap.get(key);
if (fieldValue != null) {
form.setField(key, fieldValue);
}
}

stamper.close();

//Update filed values to template Ends
Document mergePdfDoc = new Document();
PdfCopy pdfCopy;
boolean smartCopy = false;

FileOutputStream finalFile = new FileOutputStream("C:\pdf\finalmergedfile.pdf");

//Merge updated pdf with multipart content
if(smartCopy)
pdfCopy = new PdfSmartCopy(mergePdfDoc, finalFile);
else
pdfCopy = new PdfCopy(mergePdfDoc, finalFile);

PdfWriter writer = PdfWriter.getInstance(mergePdfDoc, finalFile);

mergePdfDoc.open();
PdfReader mergeReader = new PdfReader(new FileInputStream(new File("C:\pdf\megepath\updateddocument.pdf")));

pdfCopy.addDocument(mergeReader);
pdfCopy.freeReader(mergeReader);
mergeReader.close();

PdfReader pdfReader = new PdfReader[files.length];

for(int i=0; i<=files.length-1;i++) {
if(FileContentType.APPLICATION_TYPE.getContentTypes().contains(files[i].getContentType())) {
//To add multipart pdf content
pdfReader[i] = new PdfReader(files[i].getInputStream());
pdfCopy.addDocument(pdfReader[i]);
pdfCopy.freeReader(pdfReader[i]);
pdfReader[i].close();
}else if(FileContentType.IMAGE_TYPE.getContentTypes().contains(files[i].getContentType())) {
//To add multipart image content
System.out.println("Image Type Loop");
Image fileImage = Image.getInstance(files[i].getBytes());
mergePdfDoc.setPageSize(fileImage);
mergePdfDoc.newPage();
claimImage.setAbsolutePosition(0, 0);
mergePdfDoc.add(fileImage);
}
}
pdfCopy.setMergeFields();
//mergePdfDoc.close(); //If i enable this close stream closed error
//writer.close(); //If i enable this close stream closed error
memInfo.close();

finalFile.close();
}






java itext






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 16:14









mkl

52.3k1166143




52.3k1166143










asked Nov 14 at 15:32









springbootlearner

2002418




2002418








  • 2




    Your actual problem appears to be "but when i try to merge pdf with imgae getting some issues". How about making that your question topic? Like showing what you've tried and explaining how that failed.
    – mkl
    Nov 14 at 15:47












  • @mkl i have updated sample programme
    – springbootlearner
    Nov 14 at 16:14






  • 1




    Your code uses one Document instance (mergePdfDoc) both for one PdfCopy (pdfCopy) and a PdfWriter (writer) which both use the same target file finalFile. This in most cases results in broken PDF results (if there are any at all) because you effectively have to separate PDF creators writing to the same file concurrently.
    – mkl
    Nov 19 at 16:20














  • 2




    Your actual problem appears to be "but when i try to merge pdf with imgae getting some issues". How about making that your question topic? Like showing what you've tried and explaining how that failed.
    – mkl
    Nov 14 at 15:47












  • @mkl i have updated sample programme
    – springbootlearner
    Nov 14 at 16:14






  • 1




    Your code uses one Document instance (mergePdfDoc) both for one PdfCopy (pdfCopy) and a PdfWriter (writer) which both use the same target file finalFile. This in most cases results in broken PDF results (if there are any at all) because you effectively have to separate PDF creators writing to the same file concurrently.
    – mkl
    Nov 19 at 16:20








2




2




Your actual problem appears to be "but when i try to merge pdf with imgae getting some issues". How about making that your question topic? Like showing what you've tried and explaining how that failed.
– mkl
Nov 14 at 15:47






Your actual problem appears to be "but when i try to merge pdf with imgae getting some issues". How about making that your question topic? Like showing what you've tried and explaining how that failed.
– mkl
Nov 14 at 15:47














@mkl i have updated sample programme
– springbootlearner
Nov 14 at 16:14




@mkl i have updated sample programme
– springbootlearner
Nov 14 at 16:14




1




1




Your code uses one Document instance (mergePdfDoc) both for one PdfCopy (pdfCopy) and a PdfWriter (writer) which both use the same target file finalFile. This in most cases results in broken PDF results (if there are any at all) because you effectively have to separate PDF creators writing to the same file concurrently.
– mkl
Nov 19 at 16:20




Your code uses one Document instance (mergePdfDoc) both for one PdfCopy (pdfCopy) and a PdfWriter (writer) which both use the same target file finalFile. This in most cases results in broken PDF results (if there are any at all) because you effectively have to separate PDF creators writing to the same file concurrently.
– mkl
Nov 19 at 16:20

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303663%2fmerging-of-different-file-format-into-single-pdf-using-itext-is-giving-corrupt-p%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303663%2fmerging-of-different-file-format-into-single-pdf-using-itext-is-giving-corrupt-p%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin