0
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.codec.Base64.OutputStream;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.net.URL;

public class myclass {
    public static void main(String[] args) {
        String result = "<html><body><div>(i) the recognised association shall have the approval of the Forward  Markets  Commission established under the Forward  Contracts (Regulation) Act, 1952 (74 of 1952) in respect of trading in derivatives and shall function in accordance with the guidelines or conditions laid down by the Forward  Markets  Commission; </div>  <body> </html>";
        Document document = new Document();
        OutputStream file = null;
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "E://Image.pdf"));
            document.open();
            PdfWriter.getInstance(document, file);
            document.open();
            @SuppressWarnings("deprecation")
            HTMLWorker htmlWorker = new HTMLWorker(document);
            htmlWorker.parse(new StringReader(result));
            String imageUrl = "http://www.taxmann.com/emailer/demo/mobileAapp/newAppDesign.jpg";
            Image image2 = Image.getInstance(new URL(imageUrl));
            document.add(image2);
            document.close();
            file.flush();
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我正在尝试将图像和文本保存在 pdf 文件中。当我们设置文本或图像时,它工作正常,同时无法将图像和文本都保存在 pdf 中。如何在 Pdf 中保存图像和文本?我正在使用iText

4

1 回答 1

0

可能问题是 Wrong import forOutputstream并且file始终为 null 。您从未分配任何 O/P 流。

尝试这个

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.URL;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;

public class ItextExample {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {

        String result = "<html><body><div>(i) the recognised association shall have the approval of the Forward  Markets  Commission established under the Forward  Contracts (Regulation) Act, 1952 (74 of 1952) in respect of trading in derivatives and shall function in accordance with the guidelines or conditions laid down by the Forward  Markets  Commission; </div>  <body> </html>";
        Document document = new Document();
        OutputStream file = null;
        try {
            file = new FileOutputStream("E://Image1.pdf");
            PdfWriter.getInstance(document,file);
            document.open();
            HTMLWorker htmlWorker = new HTMLWorker(document);
            htmlWorker.parse(new StringReader(result));
            String imageUrl = "http://www.taxmann.com/emailer/demo/mobileAapp/newAppDesign.jpg";
            Image image2 = Image.getInstance(new URL(imageUrl));
            document.add(image2);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
            document.close();
            file.flush();
            }catch(Exception e) {
              e.printStackTrace();  
            }
        }
    }
}
于 2013-08-14T15:28:24.340 回答