forked from allgood/OpenNoteScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds the possibility to create a pdf from selected images to GalleryG…
…ridActivity
- Loading branch information
Your Name
committed
Jun 8, 2020
1 parent
7828ecc
commit 9cff336
Showing
6 changed files
with
146 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
app/src/main/java/com/todobom/opennotescanner/helpers/PdfHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.todobom.opennotescanner.helpers; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
import android.widget.Toast; | ||
|
||
import com.itextpdf.io.image.ImageData; | ||
import com.itextpdf.io.image.ImageDataFactory; | ||
import com.itextpdf.kernel.geom.PageSize; | ||
import com.itextpdf.kernel.pdf.PdfDocument; | ||
import com.itextpdf.kernel.pdf.PdfWriter; | ||
import com.itextpdf.layout.Document; | ||
import com.itextpdf.layout.element.Image; | ||
import com.todobom.opennotescanner.R; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.net.MalformedURLException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
|
||
|
||
public class PdfHelper { | ||
|
||
public static void mergeImagesToPdf(Context applicationContext, ArrayList<String> files) { | ||
//TODO move this to background thread | ||
if (files.isEmpty()) { | ||
Toast | ||
.makeText(applicationContext, applicationContext.getString(R.string.no_files_selected), Toast.LENGTH_SHORT) | ||
.show(); | ||
return; | ||
} | ||
String outputFile = "PDF-" | ||
+ new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date()) + ".pdf"; | ||
|
||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext); | ||
String pdfFilePath = new File( | ||
android.os.Environment.getExternalStorageDirectory() | ||
+ File.separator + sharedPreferences.getString("storage_folder", "OpenNoteScanner") | ||
, outputFile) | ||
.getAbsolutePath(); | ||
|
||
PdfWriter pdfWriter = null; | ||
try { | ||
pdfWriter = new PdfWriter(pdfFilePath); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} | ||
if(pdfWriter == null){ | ||
return; | ||
} | ||
|
||
PdfDocument pdfDocument = new PdfDocument(pdfWriter); | ||
Document document = new Document(pdfDocument); | ||
|
||
Collections.sort(files); | ||
|
||
for (String file : files) { | ||
ImageData imageData = null; | ||
try { | ||
imageData = ImageDataFactory.create(file); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
} | ||
if(imageData == null){ | ||
return; | ||
} | ||
Image image = new Image(imageData); | ||
pdfDocument.addNewPage(new PageSize(image.getImageWidth(), image.getImageHeight())); | ||
document.add(image); | ||
} | ||
|
||
document.close(); | ||
|
||
Toast | ||
.makeText(applicationContext, pdfFilePath, Toast.LENGTH_SHORT) | ||
.show(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- Taken from material.io | ||
Apache license version 2.0 applies --> | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FF000000" | ||
android:pathData="M20,2L8,2c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM11.5,9.5c0,0.83 -0.67,1.5 -1.5,1.5L9,11v2L7.5,13L7.5,7L10,7c0.83,0 1.5,0.67 1.5,1.5v1zM16.5,11.5c0,0.83 -0.67,1.5 -1.5,1.5h-2.5L12.5,7L15,7c0.83,0 1.5,0.67 1.5,1.5v3zM20.5,8.5L19,8.5v1h1.5L20.5,11L19,11v2h-1.5L17.5,7h3v1.5zM9,9.5h1v-1L9,8.5v1zM4,6L2,6v14c0,1.1 0.9,2 2,2h14v-2L4,20L4,6zM14,11.5h1v-3h-1v3z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters