Skip to content

m1ga/ti.pdftools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Ti.PDFTools

Titanium module for using PdfBox on Android using https://github.com/TomRoush/PdfBox-Android

Installation

<modules>
    <module>ti.pdftools</module>
</modules>

Usage

var pdftools = require('ti.pdftools');

// files
var f1 = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "1.pdf");
var f2 = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "2.pdf");

var file = pdftools.merge([
	f1, f2
]);

/*
var f1 = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "image1.jpg");
var f2 = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "image2.png");

var file = pdftools.imagesToPdf([
	f1, f2
]);
*/

// open merged PDF file
var intent = Ti.Android.createIntent({
    action: Ti.Android.ACTION_VIEW,
    type: "application/pdf",
    data: file.nativePath
});
var open = Ti.Android.createIntentChooser(intent, "open pdf");
Ti.Android.currentActivity.startActivity(open);

create a new PDF

const win = Ti.UI.createWindow({});
const btn = Ti.UI.createButton({
	title: "add"
});
btn.addEventListener("click", function() {
	var pdftools = require('ti.pdftools');

	var page1 = {
		format: "a3",
		content: [{
				text: "Hello",
				x: 10,
				y: 10,
				fontSize: 20
			},
			{
				text: "Titanium",
				x: 10,
				y: 30,
				fontSize: 30
			}
		]
	};

	var page2 = {
		format: "a4",
		content: [{
				text: "Hello 2",
				x: 10,
				y: 10,
				fontSize: 20
			},
			{
				text: "Titanium 2",
				x: 10,
				y: 30,
				fontSize: 30
			}
		]
	};

	var file = pdftools.createPDF({
		pages: [page1, page2]
	});

	if (file != null) {
		var intent = Ti.Android.createIntent({
			action: Ti.Android.ACTION_VIEW,
			type: "application/pdf",
			data: file.nativePath
		});
		var open = Ti.Android.createIntentChooser(intent, "open pdf");
		Ti.Android.currentActivity.startActivity(open);
	}
})
win.add(btn);
win.open();

Convert PDF to image

var pdftools = require('ti.pdftools');
const win = Ti.UI.createWindow({});
const img = Ti.UI.createImageView({});

var f1 = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, "1.pdf");

pdftools.addEventListener("image", function(e) {
	img.image = e.image;
})

var file = pdftools.pdfToImage(f1);
win.add(img);
win.open();

Methods

method description parameter return
merge will merge multiple PDF into one Array of PDF files null or TiBlob
imagesToPdf will merge multiple images into a PDF Array of image files null or TiBlob
createPDF will create a new PDF Array of page objects: pages: [page, page]
page object: {format: "a4", content: [{ text: "Hello", x: 10, y: 10, fontSize: 20}]}
null or TiBlob
pdfToImage will convert PDF pages to images. Fires the `image` event with `e.image` File -

Events

  • image: returns e.image for every PDF page.

Author