Skip to content

Commit

Permalink
Fixed #10: Add footer to every page.
Browse files Browse the repository at this point in the history
Adds a new method to override `protected PDFFooterView getFooterView(int pageIndex)`

As this will break some peoples existing code and to follow semantic versioning, iam updating version to 2.0
  • Loading branch information
tej.singh committed Feb 21, 2021
1 parent c37c74b commit 8cce338
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 76 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
compileSdkVersion 30
defaultConfig {
applicationId "com.tejpratapsingh.pdfcreatorandroid"
minSdkVersion 21
targetSdkVersion 29
versionCode 3
versionName "1.2"
targetSdkVersion 30
versionCode 4
versionName "2.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -23,7 +23,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'

testImplementation 'junit:junit:4.13'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.tejpratapsingh.pdfcreator.activity.PDFCreatorActivity;
import com.tejpratapsingh.pdfcreator.utils.PDFUtil;
import com.tejpratapsingh.pdfcreator.views.PDFBody;
import com.tejpratapsingh.pdfcreator.views.PDFFooterView;
import com.tejpratapsingh.pdfcreator.views.PDFHeaderView;
import com.tejpratapsingh.pdfcreator.views.PDFTableView;
import com.tejpratapsingh.pdfcreator.views.basic.PDFHorizontalView;
import com.tejpratapsingh.pdfcreator.views.basic.PDFImageView;
import com.tejpratapsingh.pdfcreator.views.basic.PDFLineSeparatorView;
import com.tejpratapsingh.pdfcreator.views.basic.PDFTextView;
import com.tejpratapsingh.pdfcreator.views.basic.PDFView;

import java.io.File;
import java.util.Locale;
Expand Down Expand Up @@ -55,15 +58,6 @@ public void pdfGenerationFailure(Exception exception) {
protected PDFHeaderView getHeaderView(int pageIndex) {
PDFHeaderView headerView = new PDFHeaderView(getApplicationContext());

PDFTextView pdfTextViewPage = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.SMALL);
pdfTextViewPage.setText(String.format(Locale.getDefault(), "Page: %d", pageIndex + 1));
pdfTextViewPage.setLayout(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 0));
pdfTextViewPage.getView().setGravity(Gravity.CENTER_HORIZONTAL);

headerView.addView(pdfTextViewPage);

PDFHorizontalView horizontalView = new PDFHorizontalView(getApplicationContext());

PDFTextView pdfTextView = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.HEADER);
Expand Down Expand Up @@ -110,10 +104,16 @@ protected PDFBody getBodyViews() {
pdfAddressView.setText("Address Line 1\nCity, State - 123456");
pdfBody.addView(pdfAddressView);

String[] textInTable = {"1", "2", "3", "4"};

PDFLineSeparatorView lineSeparatorView2 = new PDFLineSeparatorView(getApplicationContext()).setBackgroundColor(Color.WHITE);
lineSeparatorView2.setLayout(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
8, 0));
pdfBody.addView(lineSeparatorView2);

String[] textInTable = {"1", "2", "3", "4"};

PDFLineSeparatorView lineSeparatorView3 = new PDFLineSeparatorView(getApplicationContext()).setBackgroundColor(Color.WHITE);
pdfBody.addView(lineSeparatorView3);
PDFTextView pdfTableTitleView = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.P);
pdfTableTitleView.setText("Table Example");
pdfBody.addView(pdfTableTitleView);
Expand Down Expand Up @@ -144,8 +144,8 @@ protected PDFBody getBodyViews() {
}
pdfBody.addView(tableView);

PDFLineSeparatorView lineSeparatorView3 = new PDFLineSeparatorView(getApplicationContext()).setBackgroundColor(Color.BLACK);
pdfBody.addView(lineSeparatorView3);
PDFLineSeparatorView lineSeparatorView4 = new PDFLineSeparatorView(getApplicationContext()).setBackgroundColor(Color.BLACK);
pdfBody.addView(lineSeparatorView4);

PDFTextView pdfIconLicenseView = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.H3);
Spanned icon8Link = Html.fromHtml("Icon from <a href='https://icons8.com'>https://icons8.com</a>");
Expand All @@ -155,6 +155,22 @@ protected PDFBody getBodyViews() {
return pdfBody;
}

@Override
protected PDFFooterView getFooterView(int pageIndex) {
PDFFooterView footerView = new PDFFooterView(getApplicationContext());

PDFTextView pdfTextViewPage = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.SMALL);
pdfTextViewPage.setText(String.format(Locale.getDefault(), "Page: %d", pageIndex + 1));
pdfTextViewPage.setLayout(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 0));
pdfTextViewPage.getView().setGravity(Gravity.CENTER_HORIZONTAL);

footerView.addView(pdfTextViewPage);

return footerView;
}

@Override
protected void onNextClicked(final File savedPDFFile) {
Uri pdfUri = Uri.fromFile(savedPDFFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,40 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
finish();
break;
if (item.getItemId() == android.R.id.home) {
finish();
} else if (item.getItemId() == R.id.menuPrintPdf) {
File fileToPrint = getPdfFile();
if (fileToPrint == null || !fileToPrint.exists()) {
Toast.makeText(this, R.string.text_generated_file_error, Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
case R.id.menuPrintPdf: {
File fileToPrint = getPdfFile();
if (fileToPrint == null || !fileToPrint.exists()) {
Toast.makeText(this, R.string.text_generated_file_error, Toast.LENGTH_SHORT).show();
break;
}

PrintAttributes.Builder printAttributeBuilder = new PrintAttributes.Builder();
printAttributeBuilder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);
printAttributeBuilder.setMinMargins(PrintAttributes.Margins.NO_MARGINS);
PrintAttributes.Builder printAttributeBuilder = new PrintAttributes.Builder();
printAttributeBuilder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);
printAttributeBuilder.setMinMargins(PrintAttributes.Margins.NO_MARGINS);

PDFUtil.printPdf(PdfViewerActivity.this, fileToPrint, printAttributeBuilder.build());
break;
PDFUtil.printPdf(PdfViewerActivity.this, fileToPrint, printAttributeBuilder.build());
} else if (item.getItemId() == R.id.menuSharePdf) {
File fileToShare = getPdfFile();
if (fileToShare == null || !fileToShare.exists()) {
Toast.makeText(this, R.string.text_generated_file_error, Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
case R.id.menuSharePdf: {
File fileToShare = getPdfFile();
if (fileToShare == null || !fileToShare.exists()) {
Toast.makeText(this, R.string.text_generated_file_error, Toast.LENGTH_SHORT).show();
break;
}

Intent intentShareFile = new Intent(Intent.ACTION_SEND);
Intent intentShareFile = new Intent(Intent.ACTION_SEND);

Uri apkURI = FileProvider.getUriForFile(
getApplicationContext(),
getApplicationContext()
.getPackageName() + ".provider", fileToShare);
intentShareFile.setDataAndType(apkURI, URLConnection.guessContentTypeFromName(fileToShare.getName()));
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri apkURI = FileProvider.getUriForFile(
getApplicationContext(),
getApplicationContext()
.getPackageName() + ".provider", fileToShare);
intentShareFile.setDataAndType(apkURI, URLConnection.guessContentTypeFromName(fileToShare.getName()));
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

intentShareFile.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + fileToShare.getAbsolutePath()));
intentShareFile.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + fileToShare.getAbsolutePath()));

startActivity(Intent.createChooser(intentShareFile, "Share File"));
break;
}
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
return super.onOptionsItemSelected(item);
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:4.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
11 changes: 5 additions & 6 deletions pdfcreator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ apply plugin: 'com.github.dcendents.android-maven'
group='com.github.tejpratap46'

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
targetSdkVersion 30
versionCode 2
versionName "2.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand All @@ -31,9 +31,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.github.chrisbanes:PhotoView:2.3.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'

testImplementation 'junit:junit:4.13'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
Loading

0 comments on commit 8cce338

Please sign in to comment.