简体   繁体   English

如何使用 iText 保存 .pdf 文件?

[英]How to save .pdf file using iText?

I'm using iText to generate .pdf files and then save them to a specific location.我正在使用iText生成 .pdf 文件,然后将它们保存到特定位置。

The problem is when running the service and I hit the service with a request, it saves the first file correctly but when I'm trying to generate another one, it generates a file with the data of the first one plus it appends the previous data with the new data.问题是当运行服务并且我通过请求点击服务时,它正确保存了第一个文件,但是当我尝试生成另一个文件时,它会生成一个文件,其中包含第一个文件的数据,并附加了以前的数据与新数据。 This happens for every request.每个请求都会发生这种情况。

The code is given below :代码如下:

    String reportName = Util.getReportname(headersArrayList);
    System.out.println("Report Name " + reportName);
    String file = Util.getProperty("save.report.file") + reportName;
    PdfWriter instance = null;
    try {
        Document document= new Document(PageSize.A4.rotate());
        instance = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        addTitlePage(document, headersArrayList);
        System.out.println("request.getTables().size() : " + request.getTables().size());
        TemplateUtility.addMetaData(document, headersArrayList);
        createTable(document, request.getTables());
        document.close();
    } catch (DocumentException e) {
        System.err.println(e.getMessage());
    } catch (FileNotFoundException e) {
        System.err.println(e.getMessage());
    }

How can I solve this issue ?我该如何解决这个问题?

Replace this for "instance = PdfWriter.getInstance(document, new FileOutputStream(file));"将此替换为“instance = PdfWriter.getInstance(document, new FileOutputStream(file));” PdfWriter.getInstance(document, new FileOutputStream("yourfilename.pdf"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM