The SDK depends on forked versions of PdfiumAndroid and AndroidPdfViewer. Therefore you need to clone 3 repositories in order to setup your build environment.
- This repository
- Specific fork of PdfiumAndroid
- Specific fork of AndroidPdfViewer
Run the following commands at a folder of your choice:
git clone ssh://git@github.com:itext/PdfiumAndroid.git
git clone ssh://git@github.com:itext/AndroidPdfViewer.git
git clone ssh://git@github.com:itext/itext7-android-ui.git
This will generate the following folder structure on your machine:
your-folder
└── itext7-android
└── pdfiumandroid
└── androidpdfviewer
Double-check that your folder structure looks this way, because it is needed by settings.gradle in order to resolve all modules correctly. If you want to use a different folder structure, make sure to update your settings.gradle accordingly.
Do not use spaces in your local paths/directories, as it can lead to all sorts of errors related to Android NDK, ndk-build or cmake.
If you are having problems related to Android NDK, make sure to follow the correct setup procedure: https://developer.android.com/studio/projects/install-ndk
The SDK provides different Fragments and classes to manipulate PDF files.
You can create a new instance of PdfFragment via code:
private fun showPdfFragment(pdfUri: Uri) {
// See PdfConfig for all available customization options
val config = PdfConfig(pdfUri = pdfUri, showScrollIndicator = true)
// Create PdfFragment
val pdfFragment = PdfFragment.newInstance(pdfConfig)
// show fragment, e.g. via supportFragmentManager
}
You can also inflate a PDF fragment via XML. In thise case, the fragment is customizable via different styleables, such as app:enable_split_view, etc.
<!-- The PDF fragment can be configured in XML via different styleables, such as app:enable_split_view, etc. -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/pdf_fragment_container"
android:name="com.itextpdf.android.library.fragments.PdfFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:background_color="#F4EFBB"
app:display_file_name="true"
app:file_name="sample_1.pdf"
app:file_uri="file:///storage/emulated/0/Android/data/com.itextpdf.android.app/cache/sample_1_copy.pdf"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:enable_split_view="false"
app:page_spacing="400"
app:primary_color="#217C12"
app:secondary_color="#C6E6C1" />
You can directly launch fragment to split PDF documents via...
private fun showSplitFragment(pdfUri: Uri) {
val config = PdfConfig(pdfUri = pdfUri, showScrollIndicator = true)
val splitFragment = SplitDocumentFragment.newInstance(config)
// show fragment, e.g. via supportFragmentManager
}
You can directly manipulate PDF files without showing a Fragment-UI by using the PDFManipulator:
val manipulator = PdfManipulator.create(requireContext(), pdfUri)
manipulator.addTextAnnotationToPdf(...)
manipulator.splitPdfWithSelection(...)
manipulator.addMarkupAnnotationToPdf(...)
// etc...
You can receive fragment results by registering a fragment result listener to your fragmentManager.
private fun listenForPdfFragmentResult(fragmentManager: FragmentManager) {
fragmentManager.setFragmentResultListener(PdfFragment.REQUEST_KEY, this) { requestKey: String, bundle: Bundle ->
// Retrieve fragment result from bundle
val result: PdfResult? = bundle.getParcelable(PdfFragment.RESULT_FILE)
when (result) {
is PdfResult.CancelledByUser -> // ...
is PdfResult.PdfEdited -> // ...
is PdfResult.PdfSplit -> // ...
is PdfResult.NoChanges -> // ...
null -> // ...
}
}
}