Page to Imageimport com.teamdev.jxdocument.Document;
import com.teamdev.jxdocument.Page;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
/**
* The sample demonstrates how to load specified PDF document and get image of its first page.
*/
public class PageToImageSample {
public static void main(String[] args) throws IOException {
File file = new File("C:\\TeamDev.pdf");
// Load PDF document from specified file
Document document = new Document(file);
// Get total number of pages
int pageCount = document.getPageCount();
if (pageCount > 0) {
// Get first Page
Page page = document.getPageAt(0);
// Get Page size in pixels
Dimension pageSize = page.getSize();
// Get Image of the page with specified width and height
Image pageImage = page.convertToImage(pageSize.width, pageSize.height);
// Save Image into PNG file.
ImageIO.write((RenderedImage) pageImage, "PNG", new File("TeamDev-1.png"));
}
// Close document to release all allocated resources and memory
document.close();
}
} Load PDF Documentimport com.teamdev.jxdocument.Document;
import java.io.File;
/**
* The sample demonstrates how to load PDF document from specified file and
* print number of pages.
*/
public class LoadDocumentSample {
public static void main(String[] args) {
File file = new File("C:\\TeamDev.pdf");
// Load PDF document from specified file
Document document = new Document(file);
// Print total number of pages in the loaded document
System.out.println("Page Count: " + document.getPageCount());
// Close document to release all allocated resources and memory
document.close();
}
} Get Text from PDF Documentimport com.teamdev.jxdocument.Document;
import com.teamdev.jxdocument.Page;
import com.teamdev.jxdocument.Text;
import java.io.File;
/**
* The sample demonstrates how to get text on specified Page.
*/
public class GetTextOnPageSample {
public static void main(String[] args) {
File file = new File("C:\\TeamDev.pdf");
// Load PDF document from specified file
Document document = new Document(file);
// Get total number of pages
int pageCount = document.getPageCount();
if (pageCount > 0) {
// Get first Page
Page page = document.getPageAt(0);
// Access text presentation of the Page
Text pageText = page.getText();
// Print all text on the Page
System.out.println(pageText.getText());
}
// Close document to release all allocated resources and memory
document.close();
}
}
Rendering PDF into image
Right now JxDocument allows rendering into an image only the whole PDF document page. If you need to get only part of rendered page, then you can clip it yourself using the following code:
import com.teamdev.jxdocument.Document;
import com.teamdev.jxdocument.Page;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
/**
* The sample demonstrates how to load specified PDF document and get image of its first page.
*/
public class PageToImageSample {
public static void main(String[] args) throws IOException {
File file = new File("D:\\test.pdf");
// Load PDF document from specified file
Document document = new Document(file);
// Get total number of pages
int pagesCount = document.getPageCount();
if (pagesCount > 0) {
// Get first Page
Page page = document.getPageAt(0);
// Get Page size in pixels
Dimension pageSize = page.getSize();
// Get Image of the page with specified width and height
Image pageImage = page.convertToImage(pageSize.width, pageSize.height);
BufferedImage subImage = ((BufferedImage) pageImage).getSubimage(100, 100, 200, 200);
// Save Image into PNG file.
ImageIO.write(subImage, "PNG", new File("TeamDev-1.png"));
}
// Close document to release all allocated resources and memory
document.close();
}
} |
|