PDFSamples.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
  3. * <p>
  4. * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  5. * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  6. * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  7. * This notice may not be removed from this file.
  8. */
  9. package com.compdfkit.samples;
  10. import android.content.Context;
  11. import android.os.Environment;
  12. import androidx.annotation.StringRes;
  13. import com.compdfkit.core.common.CPDFDocumentException;
  14. import com.compdfkit.core.document.CPDFDocument;
  15. import com.compdfkit.samples.util.OutputListener;
  16. import java.io.File;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. public abstract class PDFSamples {
  20. protected static final String INPUT_PATH = "TestFiles/";
  21. protected Context context;
  22. protected OutputListener outputListener;
  23. private ArrayList<String> outputFileList;
  24. private String title;
  25. private String description;
  26. public PDFSamples() {
  27. context = SampleApplication.getInstance();
  28. title = "{title}";
  29. description = "{description}";
  30. outputFileList = new ArrayList<>();
  31. }
  32. public String getTitle() {
  33. return title;
  34. }
  35. public void setTitle(String title) {
  36. this.title = title;
  37. }
  38. public void setTitle(@StringRes int titleResId) {
  39. this.title = SampleApplication.getInstance().getString(titleResId);
  40. }
  41. public String getDescription() {
  42. return description;
  43. }
  44. public void setDescription(String description) {
  45. this.description = description;
  46. }
  47. public void setDescription(@StringRes int descriptionResId) {
  48. this.description = SampleApplication.getInstance().getString(descriptionResId);
  49. }
  50. public List<String> getOutputFileList() {
  51. return outputFileList;
  52. }
  53. public String[] getOutputFileNames() {
  54. String[] names = new String[outputFileList.size()];
  55. outputFileList.toArray(names);
  56. for (int i = 0; i < names.length; i++) {
  57. File file = new File(names[i]);
  58. names[i] = file.getName();
  59. }
  60. return names;
  61. }
  62. public void addFileList(String file) {
  63. this.outputFileList.add(file);
  64. }
  65. protected void printHead() {
  66. String head = SampleApplication.getInstance().getString(R.string.sample_header, title);
  67. if (outputListener != null) {
  68. outputListener.println(head);
  69. }
  70. }
  71. protected void printFooter() {
  72. String footer = SampleApplication.getInstance().getString(R.string.sample_footer);
  73. if (outputListener != null) {
  74. outputListener.println("\n" + footer);
  75. printDividingLine();
  76. }
  77. }
  78. protected void printDividingLine(){
  79. if (outputListener != null) {
  80. outputListener.println("--------------------------------------------");
  81. }
  82. }
  83. protected void run(OutputListener outputListener) {
  84. this.outputListener = outputListener;
  85. this.outputFileList.clear();
  86. }
  87. protected void saveSamplePDF(CPDFDocument document, File file, boolean close){
  88. try {
  89. file.getParentFile().mkdirs();
  90. document.saveAs(file.getAbsolutePath(), false);
  91. if (file.exists()) {
  92. getOutputFileList().add(file.getAbsolutePath());
  93. }
  94. if (close){
  95. document.close();
  96. }
  97. } catch (CPDFDocumentException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. protected File outputDir(){
  102. return context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
  103. }
  104. }