How to display iText image objects in Java
•
Java
I'm creating a PDF reader in Java In order to read PDF files, I am using iText library I have a sample code to read PDF files, but I don't know how to display iText image objects in JFrame Below is the code to read PDF files
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
public class ReadAndUsePdf {
private static String INPUTFILE = "c:/temp/FirstPdf.pdf";
private static String OUTPUTFILE = "c:/temp/ReadPdf.pdf";
public static void main(String[] args) throws DocumentException,IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(OUTPUTFILE));
document.open();
PdfReader reader = new PdfReader(INPUTFILE);
int n = reader.getNumberOfPages();
PdfImportedPage page;
// Go through all pages
for (int i = 1; i <= n; i++) {
// Only page number 2 will be included
if (i == 2) {
page = writer.getImportedPage(reader,i);
Image instance = Image.getInstance(page); //how to show this object in a JFrame
document.add(instance);
}
}
document.close();
}
}
Solution
I have exactly the same problem, I use @ L_ 404_ 2 @ render PDF in swing and operate it through iText to solve it
Can I use iText to render PDF in my swing application?
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
