Java Code Examples for com.lowagie.text.pdf.PdfDestination#FIT

The following examples show how to use com.lowagie.text.pdf.PdfDestination#FIT . 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: PdfLogicalPageDrawable.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void drawAnchor( final RenderNode content ) {
  if ( content.isNodeVisible( getDrawArea() ) == false ) {
    return;
  }
  final String anchorName = (String) content.getStyleSheet().getStyleProperty( ElementStyleKeys.ANCHOR_NAME );
  if ( anchorName == null ) {
    return;
  }
  final AffineTransform affineTransform = getGraphics().getTransform();
  final float translateX = (float) affineTransform.getTranslateX();

  final float upperY = translateX + (float) ( globalHeight - StrictGeomUtility.toExternalValue( content.getY() ) );
  final float leftX = (float) ( StrictGeomUtility.toExternalValue( content.getX() ) );
  final PdfDestination dest = new PdfDestination( PdfDestination.FIT, leftX, upperY, 0 );
  writer.getDirectContent().localDestination( anchorName, dest );
}
 
Example 2
Source File: PdfLogicalPageDrawable.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void drawBookmark( final RenderNode box, final String bookmark ) {
  if ( box.isNodeVisible( getDrawArea() ) == false ) {
    return;
  }
  final PdfOutline root = writer.getDirectContent().getRootOutline();

  final AffineTransform affineTransform = getGraphics().getTransform();
  final float translateX = (float) affineTransform.getTranslateX();

  final float upperY = translateX + (float) ( globalHeight - StrictGeomUtility.toExternalValue( box.getY() ) );
  final float leftX = (float) ( StrictGeomUtility.toExternalValue( box.getX() ) );
  final PdfDestination dest = new PdfDestination( PdfDestination.FIT, leftX, upperY, 0 );
  new PdfOutline( root, dest, bookmark );
  // destination will always point to the 'current' page
  // todo: Make this a hierarchy ..
}
 
Example 3
Source File: OutlineActionsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Demonstrates some PageLabel functionality.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();
	Document remote = new Document();
	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("OutlineActions.pdf"));
	PdfWriter.getInstance(remote, PdfTestBase.getOutputStream("remote.pdf"));
	// step 3:
	writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
	document.open();
	remote.open();
	// step 4:
	// we add some content
	document.add(new Paragraph("Outline action example"));
	// we add the outline
	PdfContentByte cb = writer.getDirectContent();
	PdfOutline root = cb.getRootOutline();
	PdfOutline links = new PdfOutline(root, new PdfAction("http://www.lowagie.com/iText/links.html"),
			"Useful links");
	links.setColor(new Color(0x00, 0x80, 0x80));
	links.setStyle(Font.BOLD);
	new PdfOutline(links, new PdfAction("http://www.lowagie.com/iText"), "Bruno's iText site");
	new PdfOutline(links, new PdfAction("http://itextpdf.sourceforge.net/"), "Paulo's iText site");
	new PdfOutline(links, new PdfAction("http://sourceforge.net/projects/itext/"), "iText @ SourceForge");
	PdfOutline other = new PdfOutline(root, new PdfDestination(PdfDestination.FIT), "other actions", false);
	other.setStyle(Font.ITALIC);
	new PdfOutline(other, new PdfAction("remote.pdf", 1), "Go to yhe first page of a remote file");
	new PdfOutline(other, new PdfAction("remote.pdf", "test"), "Go to a local destination in a remote file");
	new PdfOutline(other, PdfAction.javaScript("app.alert('Hello');\r", writer), "Say Hello");

	remote.add(new Paragraph("Some remote document"));
	remote.newPage();
	Paragraph p = new Paragraph("This paragraph contains a ");
	p.add(new Chunk("local destination").setLocalDestination("test"));
	remote.add(p);

	// step 5: we close the document
	document.close();
	remote.close();
}
 
Example 4
Source File: DestinationsTest.java    From itext2 with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a document with some goto actions.
 * 
 */
@Test
public void main() throws Exception {

	// step 1: creation of a document-object
	Document document = new Document();

	// step 2:
	PdfWriter writer = PdfWriter.getInstance(document, PdfTestBase.getOutputStream("Destinations.pdf"));
	// step 3:
	writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
	document.open();
	// step 4: we grab the ContentByte and do some stuff with it
	PdfContentByte cb = writer.getDirectContent();

	// we create a PdfTemplate
	PdfTemplate template = cb.createTemplate(25, 25);

	// we add some crosses to visualize the destinations
	template.moveTo(13, 0);
	template.lineTo(13, 25);
	template.moveTo(0, 13);
	template.lineTo(50, 13);
	template.stroke();

	// we add the template on different positions
	cb.addTemplate(template, 287, 787);
	cb.addTemplate(template, 187, 487);
	cb.addTemplate(template, 487, 287);
	cb.addTemplate(template, 87, 87);

	// we define the destinations
	PdfDestination d1 = new PdfDestination(PdfDestination.XYZ, 300, 800, 0);
	PdfDestination d2 = new PdfDestination(PdfDestination.FITH, 500);
	PdfDestination d3 = new PdfDestination(PdfDestination.FITR, 200, 300, 400, 500);
	PdfDestination d4 = new PdfDestination(PdfDestination.FITBV, 100);
	PdfDestination d5 = new PdfDestination(PdfDestination.FIT);

	// we define the outlines
	PdfOutline out1 = new PdfOutline(cb.getRootOutline(), d1, "root");
	PdfOutline out2 = new PdfOutline(out1, d2, "sub 1");
	new PdfOutline(out1, d3, "sub 2");
	new PdfOutline(out2, d4, "sub 2.1");
	new PdfOutline(out2, d5, "sub 2.2");

	// step 5: we close the document
	document.close();
}
 
Example 5
Source File: EpubToPdfConverter.java    From website with GNU Affero General Public License v3.0 4 votes vote down vote up
public File convert(String epubPath) throws IOException,DocumentException {
	applyProperties(epub2pdfProps);
       File epubFile = new File(epubPath);
       if (!(epubFile.canRead())) {
           throw new IOException("Could not read " + epubPath);
       } else {
       	logger.debug("Converting " + epubFile.getAbsolutePath());
       }
       String epubFilename = epubFile.getName();
       String epubFilenameBase = epubFilename.substring(0, epubFilename.length()-5);
       String pdfFilename = epubFilenameBase + ".pdf";

       File outputFile = new File(outputDir.getAbsolutePath() + File.separator + pdfFilename);

       epubIn = Epub.fromFile(epubPath);
       XhtmlHandler.setSourceEpub(epubIn);

       Opf opf = epubIn.getOpf();
       List<String> contentPaths = opf.spineHrefs();
       List<File> contentFiles = new ArrayList<File>();
       for (String path : contentPaths) {
           contentFiles.add(new File(epubIn.getContentRoot(),path));
       }
       Ncx ncx = epubIn.getNcx();
       
       List<NavPoint> ncxToc = new ArrayList<NavPoint>();
       if(ncx != null) {
       	ncxToc.addAll(ncx.getNavPointsFlat());
       }
       
       Tree<TocTreeNode> tocTree = TocTreeNode.buildTocTree(ncx);
       XhtmlHandler.setTocTree(tocTree);
       
       Document doc = new Document();
       boolean pageSizeOK = doc.setPageSize(pageSize);
       boolean marginsOK = doc.setMargins(marginLeftPt, marginRightPt, marginTopPt, marginBottomPt);

       logger.debug("Writing PDF to " + outputFile.getAbsolutePath());
       PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outputFile));
       writer.setStrictImageSequence(true);
       PdfOutline bookmarkRoot = null;

       if (!(pageSizeOK && marginsOK)) {
           throw new RuntimeException("Failed to set PDF page size a/o margins");
       }
       
       int fileCount = contentFiles.size();
       logger.debug("Processing " + fileCount + " HTML file(s): ");
       int currentFile = 0;

       for (File file : contentFiles) {
       	currentFile++;
       	
       	String progressChar;
       	
       	int mod10 = currentFile % 10;
       	if(mod10 == 5)
       		progressChar = "5";
       	else if(mod10 == 0) 
       		progressChar = "0";
       	else
       		progressChar = ".";
       	
       	logger.debug(progressChar);
           if (!(doc.isOpen())) {
               doc.open();
               doc.newPage();
               bookmarkRoot = writer.getRootOutline();
               XhtmlHandler.setBookmarkRoot(bookmarkRoot);
           }
           NavPoint fileLevelNP = Ncx.findNavPoint(ncxToc, file.getName());
           TreeNode<TocTreeNode> npNode = TocTreeNode.findInTreeByNavPoint(tocTree, fileLevelNP);
           
           if(fileLevelNP != null) {
           	doc.newPage();
           	PdfOutline pdfOutlineParent = bookmarkRoot;
           	if(npNode != null) {
           		TreeNode<TocTreeNode> parent = npNode.getParent();
           		if(parent != null) {
           			TocTreeNode parentTTN = parent.getValue();
           			if(parentTTN != null && parentTTN.getPdfOutline() != null) {
           				pdfOutlineParent = parentTTN.getPdfOutline();
           			}
           		}
           	}
           	
           	PdfDestination here = new PdfDestination(PdfDestination.FIT);
           	PdfOutline pdfTocEntry = new PdfOutline(pdfOutlineParent, here, fileLevelNP.getNavLabelText());
           	if(npNode != null) {
           		npNode.getValue().setPdfDestination(here);
           		npNode.getValue().setPdfOutline(pdfTocEntry);
           	}
           }
           XhtmlHandler.process(file.getCanonicalPath(), doc);
       }
       doc.close();
       logger.debug("PDF written to " + outputFile.getAbsolutePath());
       epubIn.cleanup();
       return outputFile;
}