Inherit margins after appending document in Java


#1

I am trying to combine 2 documents using appending functionality in Aspose for Java.
The first document has a footer that has margins. The second document doesn’t have a footer.
After appending, the second document will also have a footer, but there will be no margins. I want the footer to look the same in the whole document.

Here is my code:

//Merges binary content od two documents.
//First document should be in doc or docx format
//Second document can be in doc, docx, pdf, xls, xlsx, ppt or pptx format
private byte[] mergeBinaryContent(byte[] doc1, byte[] doc2) {
byte[] result;

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
         ByteArrayInputStream stream1 = new ByteArrayInputStream(doc1);
         ByteArrayInputStream stream2 = new ByteArrayInputStream(doc2);
    ){
        String fileExtension1 = FileExtensionService.getFileExtensionByContent(doc1);
        if(!".doc".equals(fileExtension1) && !".docx".equals(fileExtension1)) {
            throw new ClmaRuntimeException("formatNotRecognized");
        }
        String fileExtension2 = FileExtensionService.getFileExtensionByContent(doc2);

        com.aspose.words.Document asposeDoc1 = new com.aspose.words.Document(stream1);
        com.aspose.words.Document asposeDoc2;
        if(".doc".equals(fileExtension2) || ".docx".equals(fileExtension2)) {
            asposeDoc2 = new com.aspose.words.Document(stream2);
        }else if(".pdf".equals(fileExtension2)) {
            com.aspose.pdf.Document asposePdf2 = new com.aspose.pdf.Document(stream2);
            byte[] binary2 = AsposeFormatConverter.convertPDFToDOCX(asposePdf2);
            asposeDoc2 = new com.aspose.words.Document(new ByteArrayInputStream(binary2));
        } else if(".xls".equals(fileExtension2) || ".xlsx".equals(fileExtension2)) {
            Workbook workbook = new Workbook(stream2);
            byte[] binary2 = AsposeFormatConverter.convertExcelToWord(workbook);
            asposeDoc2 = new com.aspose.words.Document(new ByteArrayInputStream(binary2));
        }else if(".ppt".equals(fileExtension2) || ".pptx".equals(fileExtension2)) {
            Presentation presentation = new Presentation(stream2);
            byte[] binary2 = AsposeFormatConverter.convertPresentationToWord(presentation);
            asposeDoc2 = new com.aspose.words.Document(new ByteArrayInputStream(binary2));
        } else {
            throw new ClmaRuntimeException("formatNotRecognized");
        }

        asposeDoc1.appendDocument(asposeDoc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
        asposeDoc1.save(baos, asposeDoc1.getOriginalLoadFormat());
        result = baos.toByteArray();
    } catch (PdfException pe) {
        log.error("",pe);
        throw new PdfException(pe.getMessage());
    } catch(Exception e) {
        log.error("", e);
        throw new ClmaRuntimeException(e.getMessage());
    }
    return result;
}

#2

@callidus,

Sorry for the delayed response. Please go through the article Joining Documents with Headers and Footers for details on how to concatenate two documents. This article also contains information about different settings that can be made while processing the documents. You may try method HeaderFooterCollection.linkToPrevious to link all headers and footers to the corresponding headers and footers of first document.

Hope that above information helps. Feel free to contact us in case of any query or comments.