本文整理匯總了Java中com.itextpdf.text.pdf.PdfReader類的典型用法代碼示例。如果您正苦於以下問題:Java PdfReader類的具體用法?Java PdfReader怎麽用?Java PdfReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PdfReader類屬於com.itextpdf.text.pdf包,在下文中一共展示了PdfReader類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPdfContent
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
private String getPdfContent(String pdfFile) {
try {
PdfReader reader = new PdfReader(pdfFile);
StringBuffer sb = new StringBuffer();
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
TextExtractionStrategy strategy;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
sb.append(strategy.getResultantText());
}
reader.close();
return sb.toString();
} catch (IOException e) {
throw new IllegalArgumentException("Not able to read file " + pdfFile, e);
}
}
示例2: testWritePlainLetter
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
@Test
public void testWritePlainLetter() {
try {
PdfReader reader = new PdfReader("letters/" + voter.getNif() + ".pdf");
String page = PdfTextExtractor.getTextFromPage(reader, 1);
String[] lines = page.split("\n");
String email = lines[1].split(":\t")[1];
String password = lines[2].split(":\t")[1];
assertEquals(voter.getEmail(), email);
assertEquals(voter.getPassword(), password);
} catch (IOException e) {
System.err.println("Archivo no encontrado");
}
}
示例3: createCropJob
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
public static CropJob createCropJob(ClusterJob curClusterJob) throws IOException {
File source = curClusterJob.getSource();
if (source != null && source.exists()) {
PdfReader reader = new PdfReader(source.getAbsolutePath());
CropJob result = new CropJob(source, reader.getNumberOfPages(), reader.getInfo(),
SimpleBookmark.getBookmark(reader));
reader.close();
result.setClusterCollection(curClusterJob.getClusterCollection());
return result;
}
return null;
}
示例4: testSwitchOrientation
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/34394199/i-cant-rotate-my-page-from-existing-pdf">
* I can't rotate my page from existing PDF
* </a>
* <p>
* Switching between portrait and landscape like this obviously will cut off some parts of the page.
* </p>
*/
@Test
public void testSwitchOrientation() throws DocumentException, IOException
{
try (InputStream resourceStream = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/n2013.00849449.pdf"))
{
PdfReader reader = new PdfReader(resourceStream);
int n = reader.getNumberOfPages();
PdfDictionary pageDict;
for (int i = 1; i <= n; i++) {
Rectangle rect = reader.getPageSize(i);
Rectangle crop = reader.getCropBox(i);
pageDict = reader.getPageN(i);
pageDict.put(PdfName.MEDIABOX, new PdfArray(new float[] {rect.getBottom(), rect.getLeft(), rect.getTop(), rect.getRight()}));
pageDict.put(PdfName.CROPBOX, new PdfArray(new float[] {crop.getBottom(), crop.getLeft(), crop.getTop(), crop.getRight()}));
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File(RESULT_FOLDER, "n2013.00849449-switch.pdf")));
stamper.close();
reader.close();
}
}
示例5: testMergeGrandizerFiles
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge">
* How To Remove Whitespace on Merge
* </a>
* <p>
* Testing {@link PdfDenseMergeTool} using the OP's files.
* </p>
*/
@Test
public void testMergeGrandizerFiles() throws DocumentException, IOException
{
try ( InputStream docA = getClass().getResourceAsStream("Header.pdf");
InputStream docB = getClass().getResourceAsStream("Body.pdf");
InputStream docC = getClass().getResourceAsStream("Footer.pdf"); )
{
PdfDenseMergeTool tool = new PdfDenseMergeTool(PageSize.A4, 18, 18, 5);
PdfReader readerA = new PdfReader(docA);
PdfReader readerB = new PdfReader(docB);
PdfReader readerC = new PdfReader(docC);
try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "GrandizerMerge.pdf")))
{
List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC);
tool.merge(fos, inputs);
}
finally
{
readerA.close();
readerB.close();
readerC.close();
}
}
}
示例6: build
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
/**
* Update NDA file with visitor name and visitor signature.
*
* @param destFile
* @param signatureImage signature file
* @param visitorName
* @return File
*/
public static File build(Path destFile, File signatureImage, String visitorName) {
try {
PdfReader pdfReader = new PdfReader(ndaUrl);
PdfStamper pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream(destFile.toString()));
Image image = createNDAImage(signatureImage, 0, 0);
PdfContentByte over = pdfStamper.getOverContent(5);
over.addImage(image);
PdfContentByte pdfContentByte = pdfStamper.getOverContent(5);
pdfContentByte.beginText();
pdfContentByte.setFontAndSize(BaseFont.createFont
(BaseFont.HELVETICA,
BaseFont.CP1257,
BaseFont.EMBEDDED
)
, 10);
pdfContentByte.setTextMatrix(112, 428);
pdfContentByte.showText(visitorName);
pdfContentByte.setTextMatrix(89, 406);
pdfContentByte.showText(new SimpleDateFormat("E, dd MMM yyyy").format(new Date()));
pdfContentByte.endText();
pdfStamper.close();
return destFile.toFile();
} catch (IOException | DocumentException | NumberFormatException e) {
logger.error("Exception while generating NDA file. ",e);
return null;
}
}
示例7: testRotateAndZoomUpperHalfPage
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
/**
* <a href="http://stackoverflow.com/questions/35374110/how-do-i-use-itext-to-have-a-landscaped-pdf-on-half-of-a-a4-back-to-portrait-and">
* How do i use iText to have a landscaped PDF on half of a A4 back to portrait and full size on A4
* </a>
* <p>
* This sample shows how to rotate and enlarge the upper half of an A4 page to fit into a new A4 page.
* </p>
*/
@Test
public void testRotateAndZoomUpperHalfPage() throws IOException, DocumentException
{
try ( InputStream resource = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/test.pdf");
OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "test-upperHalf.pdf")) )
{
PdfReader reader = new PdfReader(resource);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, result);
document.open();
double sqrt2 = Math.sqrt(2);
Rectangle pageSize = reader.getPageSize(1);
PdfImportedPage importedPage = writer.getImportedPage(reader, 1);
writer.getDirectContent().addTemplate(importedPage, 0, sqrt2, -sqrt2, 0, pageSize.getTop() * sqrt2, -pageSize.getLeft() * sqrt2);
document.close();
}
}
示例8: testVerifyTestSigned1
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
/**
* <a href="https://stackoverflow.com/questions/45027712/invalid-signature-when-signing-an-existing-sigrature-field-with-cosign-sapi">
* Invalid signature when signing an existing sigrature field with CoSign SAPI
* </a>
* <br/>
* <a href="https://www.dropbox.com/s/j6eme53lleaok13/test_signed.pdf?dl=0">
* test_signed-1.pdf
* </a>
* <p>
* Validation shows verification success while both Adobe and SD DSS fail.
* Embedded certificates have issues (emailAddress RDN is typed PrintableString
* which is wrong - specified is IA5String - and does not even make sense as
* there is no '@' in PrintableString), but does this explain it?
* </p>
*/
@Test
public void testVerifyTestSigned1() throws IOException, GeneralSecurityException
{
System.out.println("\n\ntest_signed-1.pdf\n===================");
try ( InputStream resource = getClass().getResourceAsStream("test_signed-1.pdf") )
{
PdfReader reader = new PdfReader(resource);
AcroFields acroFields = reader.getAcroFields();
List<String> names = acroFields.getSignatureNames();
for (String name : names) {
System.out.println("Signature name: " + name);
System.out.println("Signature covers whole document: " + acroFields.signatureCoversWholeDocument(name));
System.out.println("Document revision: " + acroFields.getRevision(name) + " of " + acroFields.getTotalRevisions());
PdfPKCS7 pk = acroFields.verifySignature(name);
System.out.println("Subject: " + CertificateInfo.getSubjectFields(pk.getSigningCertificate()));
System.out.println("Document verifies: " + pk.verify());
}
}
System.out.println();
}
示例9: testRemoveTransparentGraphicsTransparency
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
@Test
public void testRemoveTransparentGraphicsTransparency() throws IOException, DocumentException
{
try ( InputStream resource = getClass().getResourceAsStream("transparency.pdf");
OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "transparency-noTransparency.pdf")))
{
PdfReader pdfReader = new PdfReader(resource);
PdfStamper pdfStamper = new PdfStamper(pdfReader, result);
PdfContentStreamEditor editor = new TransparentGraphicsRemover();
for (int i = 1; i <= pdfReader.getNumberOfPages(); i++)
{
editor.editPage(pdfStamper, i);
}
pdfStamper.close();
}
}
示例10: getImageByFile
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
public ImageInstance getImageByFile( PdfContentByte cb , File file ) throws IOException, BadElementException{
Image image = null;
ImageInstance instance = null;
if( file.getName().toLowerCase().endsWith( ".pdf")){
PdfReader reader = new PdfReader( file.getAbsolutePath() );
PdfImportedPage p = cb.getPdfWriter().getImportedPage(reader, 1);
image = Image.getInstance(p);
instance = new ImageInstance(image, reader);
}else{
image = Image.getInstance( file.getAbsolutePath() );
instance = new ImageInstance(image, null);
}
instances.add(instance);
return instance;
}
示例11: closePassedStream
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
/**
* The <code>closePassedStream</code> method closes the stream passed.
*
* @param reader {@link PdfReader}
* @param document {@link Document}
* @param contentByte {@link PdfContentByte}
* @param writer {@link PdfWriter}
* @param fileInputStream {@link FileInputStream}
* @param fileOutputStream {@link FileOutputStream}
* @throws IOException {@link} if unable to close input or output stream
*/
private static void closePassedStream(final PdfReader reader, final Document document,
final PdfContentByte contentByte, final PdfWriter writer,
final FileInputStream fileInputStream, final FileOutputStream fileOutputStream) throws IOException {
if (null != reader) {
reader.close();
}
if (null != document) {
document.close();
}
if (null != contentByte) {
contentByte.closePath();
}
if (null != writer) {
writer.close();
}
if (null != fileInputStream) {
fileInputStream.close();
}
if (null != fileOutputStream) {
fileOutputStream.flush();
fileOutputStream.close();
}
}
示例12: split
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
void split(PdfReader reader, int page) throws IOException
{
PdfImportedPage importedPage = writer.getImportedPage(reader, page);
Rectangle pageSizeToImport = reader.getPageSize(page);
Iterable<Rectangle> rectangles = determineSplitRectangles(reader, page);
for (Rectangle rectangle : rectangles) {
newPage(rectangle);
PdfContentByte directContent = writer.getDirectContent();
directContent.saveState();
directContent.rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(), rectangle.getHeight());
directContent.clip();
directContent.newPath();
writer.getDirectContent().addTemplate(importedPage, -pageSizeToImport.getLeft(), -pageSizeToImport.getBottom());
directContent.restoreState();
}
}
示例13: merge
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
public void merge(OutputStream outputStream, Iterable<PdfReader> inputs) throws DocumentException, IOException
{
try
{
openDocument(outputStream);
for (PdfReader reader: inputs)
{
merge(reader);
}
}
finally
{
closeDocument();
}
}
示例14: extract
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
void extract(String resource, int startPage, int endPage, boolean isV2) throws IOException, DocumentException, NoSuchFieldException, SecurityException
{
String name = new File(resource).getName();
String target = String.format(isV2 ? "%s-lines-v2-%%s.txt" : "%s-lines-%%s.txt", name);
InputStream resourceStream = getClass().getResourceAsStream(resource);
try
{
PdfReader reader = new PdfReader(resourceStream);
System.out.printf("\nText by line in %s\n", name);
for (int page = startPage; page < endPage; page++)
{
System.out.printf("\n Page %s\n", page);
String pageText = isV2 ? extractV2(reader, page) : extract(reader, page);
Files.write(new File(RESULT_FOLDER, String.format(target, page)).toPath(), pageText.getBytes("UTF8"));
System.out.println(pageText);
}
}
finally
{
if (resourceStream != null)
resourceStream.close();
}
}
示例15: extractAndStore
import com.itextpdf.text.pdf.PdfReader; //導入依賴的package包/類
<E extends TextExtractionStrategy> String extractAndStore(PdfReader reader, String format, Class<E> strategyClass, RenderFilter... filters) throws Exception
{
StringBuilder builder = new StringBuilder();
for (int page = 1; page <= reader.getNumberOfPages(); page++)
{
TextExtractionStrategy strategy = strategyClass.getConstructor().newInstance();
if (filters != null && filters.length > 0)
{
strategy = new FilteredTextRenderListener(strategy, filters);
}
String pageText = extract(reader, page, strategy);
Files.write(Paths.get(String.format(format, page)), pageText.getBytes("UTF8"));
if (page > 1)
builder.append("\n\n");
builder.append(pageText);
}
return builder.toString();
}