1

I´m trying to merge into one file with one or more PDF files stored files and a JasperReports file using iText.

I did this method.

Map<String, Object> parameters = new HashMap<String, Object>();

List<String> arquivos = new LinkedList<String>();
arquivos.add("/Formulario/teste.jrxml");
arquivos.add("/Formulario/teste.pdf");

try {
    Document document = new Document(PageSize.A4);
    document.setMargins(0F, 0F, 0F, 0F);
    PdfCopy pdfCopy = new PdfCopy(document, new FileOutputStream("c:\\labels\\teste.pdf"));
    pdfCopy.setMargins(0, 0, 0, 0);
    document.open();

    PdfReader pdfReader = null;
    InputStream inputStream = null;

    for (String arquivo : arquivos) {
        inputStream = ReportService.class.getResourceAsStream(arquivo);

        if (FilenameUtils.getExtension(arquivo).equals("jrxml")) {

            JasperReport report = JasperCompileManager.compileReport(inputStream);
            JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
            inputStream = new ByteArrayInputStream(JasperExportManager.exportReportToPdf(jasperPrint));
        }

        pdfReader = new PdfReader(inputStream);

        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            pdfCopy.addPage(pdfCopy.getImportedPage(pdfReader, i));
        }
        pdfCopy.freeReader(pdfReader);
        pdfReader.close();
    }

    document.close();
} catch (DocumentException | IOException | JRException e) {
    e.printStackTrace();
}

enter image description here

I looked at google, but didn't found any answer. So whats wrong? It's possible to generate these pages in the same margins?

Alex K
  • 22,315
  • 19
  • 108
  • 236
user3503888
  • 435
  • 5
  • 17
  • 2
    *So whats wrong* - you merge documents with differing page sizes and margins... what did you expect? If you want a common visual style, you have to create all the source documents according to that style. The PDF format is not a good format for after the fact style changes. – mkl Oct 02 '14 at 07:06
  • @mkl thanks for your answer, do you know if it´s possible to change it using `Itext`? Because my idea is to retrieve the content of ireport bytes or pdf bytes and join then. Because in one project someone need to desing all pages, and sometimes, some pages of that document is only text, and they will not have data from a database. – user3503888 Oct 02 '14 at 11:54
  • 1
    *do you know if it´s possible to change it using Itext?* - It is possible to make all pages the same size but in general it is difficult to recognize the margins and change them: There is no "margin" value in the PDF, you can merely inspect every drawn letter, line, or whatever and try and infer margins from their positions; there are some pitfalls, though. You had better create them equal. – mkl Oct 02 '14 at 12:05
  • @mkl I saw that I can get margins in Itext. But I imagined one thing, but don´t know if it´s easy. Get the text of pdf and generate a new one. So if was you, what you do? – user3503888 Oct 02 '14 at 12:27
  • 1
    *I saw that I can get margins in Itext* - yes, but there are margin notes and other pitfalls, cf. [this answer](http://stackoverflow.com/q/25132274/1729265); *Get the text of pdf and generate a new one.* - that's extremely difficult. You have to not only extract text but all styling information; furthermore you have to interpret text pieces in respect to their e.g. forming a single line or multiple columns etc... well, if you search stack overflow for text extraction from tables in PDFs, you'll find many questions but not many answers. – mkl Oct 02 '14 at 12:49
  • If you was me, what you do? – user3503888 Oct 02 '14 at 12:51
  • 1
    *If you was me, what you do?* - I'd try and make the designers of the reports and static PDFs adhere to a single set of page and margin sizes. If that proved to not be possible and my job still was to make the merged report look good, I'd do a week or so of analysis of the incoming PDFs and another months of developing a merging solution working for the analyzed documents and similar ones. – mkl Oct 02 '14 at 14:09

0 Answers0