com.lowagie.text.pdf.PdfImportedPage Java Examples

The following examples show how to use com.lowagie.text.pdf.PdfImportedPage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PdfCreator.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
private float[] getTransformationMatrix(PdfImportedPage page, int xPos, int yPos, int tableWidth, int tableHeight) {
	float pageWidth = page.getWidth();
	float pageHeight = page.getHeight();
	float scaleX = tableWidth / pageWidth;
	float scaleY = tableHeight / pageHeight;
	float scale = Math.min(scaleX, scaleY);
	// float[] toReturn = {scale, 0f, 0f, scale, xPos, docHeight -
	// tableHeight};
	float dX = xPos;
	float dY = yPos - tableHeight;
	float[] toReturn = { scale, 0f, 0f, scale, dX, dY };
	return toReturn;
}
 
Example #2
Source File: CashReceiptCoverSheetServiceImpl.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Responsible for creating a new PDF page and workspace through <code>{@link PdfContentByte}</code> for direct writing to the
 * PDF.
 *
 * @param writer The PDF writer used to write to the new page with.
 * @param reader The PDF reader used to read information from the PDF file.
 * @param pageNumber The current number of pages in the PDF file, which will be incremented by one inside this method.
 *
 * @return The PDFContentByte used to access the new PDF page.
 * @exception DocumentException
 * @exception IOException
 */
protected PdfContentByte startNewPage(PdfWriter writer, PdfReader reader, ModifiableInteger pageNumber) throws DocumentException, IOException {
    PdfContentByte retval;
    PdfContentByte under;
    Rectangle pageSize;
    Document pdfDoc;
    PdfImportedPage newPage;

    pageNumber.increment();
    pageSize = reader.getPageSize(FRONT_PAGE);
    retval = writer.getDirectContent();
    // under = writer.getDirectContentUnder();

    if (pageNumber.getInt() > FRONT_PAGE) {
        newPage = writer.getImportedPage(reader, CHECK_PAGE_NORMAL);
        setCurrentRenderingYPosition(pageSize.top(TOP_MARGIN + CHECK_DETAIL_HEADING_HEIGHT));
    }
    else {
        newPage = writer.getImportedPage(reader, FRONT_PAGE);
        setCurrentRenderingYPosition(pageSize.top(TOP_FIRST_PAGE));
    }

    pdfDoc = retval.getPdfDocument();
    pdfDoc.newPage();
    retval.addTemplate(newPage, 0, 0);
    retval.setFontAndSize(getTextFont(), 8);

    return retval;
}
 
Example #3
Source File: TrialBalanceReportAction.java    From kfs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Generate pdf for sending response using itext
 *
 * @param reportFileFullName
 * @return
 * @throws IOException
 * @throws DocumentException
 * @throws BadPdfFormatException
 */
protected ByteArrayOutputStream generatePdfOutStream(String reportFileFullName) throws IOException, DocumentException, BadPdfFormatException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // we create a reader for a certain document
    PdfReader reader = new PdfReader(reportFileFullName);
    reader.consolidateNamedDestinations();

    // step 1: creation of a document-object
    Document document = new Document(reader.getPageSizeWithRotation(1));
    // step 2: we create a writer that listens to the document
    PdfCopy writer = new PdfCopy(document, baos);
    // step 3: we open the document
    document.open();

    // we retrieve the total number of pages
    int n = reader.getNumberOfPages();

    // step 4: we add content
    PdfImportedPage page;
    for (int i = 0; i < n;) {
        ++i;
        page = writer.getImportedPage(reader, i);
        writer.addPage(page);
    }
    writer.freeReader(reader);

    // step 5: we close the document
    document.close();
    return baos;
}
 
Example #4
Source File: SplitPdf.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * This class can be used to split an existing PDF file.
 * @param args the command line arguments
 */
public static void main(String args[]) {
    if (args.length != 4) {
        System.err.println("arguments: srcfile destfile1 destfile2 pagenumber");
    }
    else {
        try {
int pagenumber = Integer.parseInt(args[3]);
            
// we create a reader for a certain document
PdfReader reader = new PdfReader(args[0]);
// we retrieve the total number of pages
int n = reader.getNumberOfPages();
System.out.println("There are " + n + " pages in the original file.");
            
if (pagenumber < 2 || pagenumber > n) {
	throw new DocumentException("You can't split this document at page " + pagenumber + "; there is no such page.");
}
            
// step 1: creation of a document-object
Document document1 = new Document(reader.getPageSizeWithRotation(1));
Document document2 = new Document(reader.getPageSizeWithRotation(pagenumber));
// step 2: we create a writer that listens to the document
PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream(args[1]));
PdfWriter writer2 = PdfWriter.getInstance(document2, new FileOutputStream(args[2]));
// step 3: we open the document
document1.open();
PdfContentByte cb1 = writer1.getDirectContent();
document2.open();
PdfContentByte cb2 = writer2.getDirectContent();
PdfImportedPage page;
int rotation;
int i = 0;
// step 4: we add content
while (i < pagenumber - 1) {
	i++;
	document1.setPageSize(reader.getPageSizeWithRotation(i));
	document1.newPage();
	page = writer1.getImportedPage(reader, i);
	rotation = reader.getPageRotation(i);
	if (rotation == 90 || rotation == 270) {
		cb1.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
	}
	else {
		cb1.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
	}
}
while (i < n) {
	i++;
	document2.setPageSize(reader.getPageSizeWithRotation(i));
	document2.newPage();
	page = writer2.getImportedPage(reader, i);
	rotation = reader.getPageRotation(i);
	if (rotation == 90 || rotation == 270) {
		cb2.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
	}
	else {
		cb2.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
	}
	System.out.println("Processed page " + i);
}
// step 5: we close the document
document1.close();
document2.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example #5
Source File: TwoOnOneTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Reads the pages of an existing PDF file and puts 2 pages from the
 * existing doc into one of the new doc.
 */
@Test
public void main() throws Exception {
	// we create a reader for a certain document
	PdfReader reader = new PdfReader(PdfTestBase.RESOURCES_DIR + "ChapterSection.pdf");
	// we retrieve the total number of pages
	int n = reader.getNumberOfPages();
	// we retrieve the size of the first page
	Rectangle psize = reader.getPageSize(1);
	float width = psize.getHeight();
	float height = psize.getWidth();

	// step 1: creation of a document-object
	Document document = new Document(new Rectangle(width, height));
	// step 2: we create a writer that listens to the document
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("2on1.pdf"));
	// step 3: we open the document
	document.open();
	// step 4: we add content
	PdfContentByte cb = writer.getDirectContent();
	int i = 0;
	int p = 0;
	while (i < n) {
		document.newPage();
		p++;
		i++;
		PdfImportedPage page1 = writer.getImportedPage(reader, i);
		cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);
		if (i < n) {
			i++;
			PdfImportedPage page2 = writer.getImportedPage(reader, i);
			cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2 + 60, 120);
		}
		BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
		cb.beginText();
		cb.setFontAndSize(bf, 14);
		cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 2) + (n % 2 > 0 ? 1 : 0)),
				width / 2, 40, 0);
		cb.endText();
	}
	// step 5: we close the document
	document.close();

}
 
Example #6
Source File: PaperightPdfConverter.java    From website with GNU Affero General Public License v3.0 4 votes vote down vote up
private String createOneUp(String originalPdfFile) throws IOException, DocumentException {
	logger.debug("Started create 1-up file from " + originalPdfFile);
	String filename = FilenameUtils.getBaseName(originalPdfFile) + "_1up." + FilenameUtils.getExtension(originalPdfFile);
	String tempFilename =  FilenameUtils.concat(System.getProperty("java.io.tmpdir"), filename + ".tmp");
	filename = FilenameUtils.concat(getPdfFileFolder(), filename);
	File tempFile = new File(tempFilename);
	//float scale = 0.80f;
	PdfReader reader = new PdfReader(originalPdfFile);
	Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
	//Document doc = new Document(new RectangleReadOnly(606.96f, 850.32f), 0, 0, 0, 0);
	PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(tempFile));
	doc.open();
	for (int i = 1; i <= reader.getNumberOfPages(); i++) {
		doc.newPage();
		PdfContentByte cb = writer.getDirectContent();
		PdfImportedPage page = writer.getImportedPage(reader, i);
		
		
		float documentWidth = doc.getPageSize().getWidth();
		float documentHeight = doc.getPageSize().getHeight();
		if (i > 1) {
			documentHeight = documentHeight - 65f;
		}
		
		
		/*float documentHeight = doc.getPageSize().getHeight();
		if (i > 1) {
			documentHeight = documentHeight - 65f;
		}
		float documentWidth = doc.getPageSize().getWidth();*/

		float pageWidth = page.getWidth();
		float pageHeight = page.getHeight();
		
		float widthScale = documentWidth / pageWidth;
		float heightScale = documentHeight / pageHeight;
		float scale = Math.min(widthScale, heightScale);
		
		float offsetX = (documentWidth - (pageWidth * scale)) / 2;
		float offsetY = 0f;
		if (i > 1) {
			offsetY = 65f;
		}
		/*
		
		float offsetX = 0f;
		if (i > 1) {
			offsetX = 65f; //100f
		}
		//float offsetX = 65f;
		float offsetY = ((documentHeight) - (pageHeight * scale)) / 2;*/
		
		cb.addTemplate(page, scale, 0, 0, scale, offsetX, offsetY);

	}
	doc.close();
	overlayLicenceArea(tempFilename, filename, PageLayout.ONE_UP);
	logger.debug("Deleting temporary file " + tempFile.getAbsolutePath());
	FileUtils.deleteQuietly(tempFile);
	logger.debug("Completed create 1-up file from " + originalPdfFile);
	return filename;
}
 
Example #7
Source File: PaperightPdfConverter.java    From website with GNU Affero General Public License v3.0 4 votes vote down vote up
private String createTwoUp(String originalPdfFile) throws IOException, DocumentException {
	logger.debug("Started create 2-up file from " + originalPdfFile);
	String filename = FilenameUtils.getBaseName(originalPdfFile) + "_2up." + FilenameUtils.getExtension(originalPdfFile);
	String tempFilename =  FilenameUtils.concat(System.getProperty("java.io.tmpdir"), filename + ".tmp");
	filename = FilenameUtils.concat(getPdfFileFolder(), filename);
	File tempFile = new File(tempFilename);
	//float scale = 0.60f;
	PdfReader reader = new PdfReader(originalPdfFile);
	Document doc = new Document(new RectangleReadOnly(842f, 595f), 0, 0, 0, 0);
	//Document doc = new Document(new RectangleReadOnly(850.32f, 606.96f), 0, 0, 0, 0);
	//doc.setMargins(50f, 50f, 50f, 50f);
	PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(tempFile));
	doc.open();
	int totalPages = reader.getNumberOfPages();
	for (int i = 1; i <= totalPages; i = i + 2) {
		doc.newPage();
		PdfContentByte cb = writer.getDirectContent();
		PdfImportedPage page = writer.getImportedPage(reader, i); // page #1
		
		float documentWidth = doc.getPageSize().getWidth() / 2;
		float documentHeight = doc.getPageSize().getHeight();
		if (i > 1) {
			documentHeight = documentHeight - 65f;
		}

		float pageWidth = page.getWidth();
		float pageHeight = page.getHeight();
		
		float widthScale = documentWidth / pageWidth;
		float heightScale = documentHeight / pageHeight;
		float scale = Math.min(widthScale, heightScale);
		
		//float offsetX = 50f;
		float offsetX = (documentWidth - (pageWidth * scale)) / 2;
		float offsetY = 0f;
		if (i > 1) {
			offsetY = 65f; //100f
		}
		
		cb.addTemplate(page, scale, 0, 0, scale, offsetX, offsetY);
		
		if (i+1 <= totalPages) {
		
			PdfImportedPage page2 = writer.getImportedPage(reader, i+1); // page #2
			
			pageWidth = page.getWidth();
			pageHeight = page.getHeight();
			
			widthScale = documentWidth / pageWidth;
			heightScale = documentHeight / pageHeight;
			scale = Math.min(widthScale, heightScale);
			
			offsetX = ((documentWidth - (pageWidth * scale)) / 2) + documentWidth;
			//offsetY = 65f; //100f
			
			cb.addTemplate(page2, scale, 0, 0, scale, offsetX, offsetY);//430f
		}
	}
	
	doc.close();
	overlayLicenceArea(tempFilename, filename, PageLayout.TWO_UP);
	logger.debug("Deleting temporary file " + tempFile.getAbsolutePath());
	FileUtils.deleteQuietly(tempFile);
	logger.debug("Completed create 2-up file from " + originalPdfFile);
	return filename;
}
 
Example #8
Source File: PaperightPdfConverter.java    From website with GNU Affero General Public License v3.0 4 votes vote down vote up
private String createA5(String originalPdfFile) throws IOException, DocumentException {
	logger.debug("Started create A5 file from " + originalPdfFile);
	String filename = FilenameUtils.getBaseName(originalPdfFile) + "_a5." + FilenameUtils.getExtension(originalPdfFile);
	String tempFilename =  FilenameUtils.concat(System.getProperty("java.io.tmpdir"), filename + ".tmp");
	filename = FilenameUtils.concat(getPdfFileFolder(), filename);
	File tempFile = new File(tempFilename);
	//float scale = 0.72906403940886699507389162561576f;
	PdfReader reader = new PdfReader(originalPdfFile);
	
	//new RectangleReadOnly(425.16f, 606.96f)
	Document doc = new Document(PageSize.A5, 0, 0, 0, 0);
	PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(tempFile));
	doc.open();
	for (int i = 1; i <= reader.getNumberOfPages(); i++) {
		doc.newPage();
		PdfContentByte cb = writer.getDirectContent();
		PdfImportedPage page = writer.getImportedPage(reader, i); // page #1
		
		float documentWidth = doc.getPageSize().getWidth();
		float documentHeight = doc.getPageSize().getHeight();
		if (i > 1) {
			documentHeight = documentHeight - 65f;
		}
		//float documentHeight = doc.getPageSize().getHeight() - 65f;

		float pageWidth = page.getWidth();
		float pageHeight = page.getHeight();
		
		float widthScale = documentWidth / pageWidth;
		float heightScale = documentHeight / pageHeight;
		float scale = Math.min(widthScale, heightScale);
		
		//float offsetX = 50f;
		float offsetX = (documentWidth - (pageWidth * scale)) / 2;
		float offsetY = 0f;
		if (i > 1) {
			offsetY = 65f;
		}
		//float offsetY = ((documentHeight) - (pageHeight * scale)) / 2;
		
		cb.addTemplate(page, scale, 0, 0, scale, offsetX, offsetY);
		//cb.addTemplate(page, scale, 0, 0, scale, 50f, 100f);
	}
	doc.close();
	//FileUtils.moveFile(tempFile, new File(filename));
	overlayLicenceArea(tempFilename, filename, PageLayout.A5);
	logger.debug("Deleting temporary file " + tempFile.getAbsolutePath());
	FileUtils.deleteQuietly(tempFile);
	logger.debug("Completed create A5 file from " + originalPdfFile);
	return filename;
}