Merge pdf files and loss of information - java

doing the merge of 2 or + pdf I lose some information that imposed in the upload phase of the files (ALT tags on the images). This is the method:
public static void mergeFiles(ArrayList<String> filesToBeMerged, String mergedFileLocation) {
String[] filesTBM = filesToBeMerged.toArray(new String[filesToBeMerged.size()]);
PDFMergerUtility ut = new PDFMergerUtility();
try {
for (int i = 0; i < filesTBM.length; i++) {
ut.addSource(filesTBM[i]);
}
ut.setDestinationFileName(mergedFileLocation);
ut.mergeDocuments();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
}
If the PDF with the ALT tags were in the list of files to be merged, the result is correct, otherwise not. So far I've tried max with 3 PDFs including 1 with ALT tags.
The questions:
How can I not lose the alt tag after the merge of the files?
Thanks to those who want to help me
Daniele
N.b. I have also tried iText pdf:
public static void mergeFiles(ArrayList<String> filesToBeMerged, String mergedFileLocation) {
String[] filesTBM = filesToBeMerged.toArray(new String[filesToBeMerged.size()]);
Document document = new Document();
PdfCopy copy = null;
try {
copy = new PdfCopy(document, new FileOutputStream(mergedFileLocation));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
document.open();
PdfReader[] reader = new PdfReader[filesTBM.length];
for (int i = 0; i < filesTBM.length; i++) {
try {
reader[i] = new PdfReader(filesTBM[i]);
} catch (IOException e) {
e.printStackTrace();
}
try {
copy.addDocument(reader[i]);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
copy.freeReader(reader[i]);
} catch (IOException e) {
e.printStackTrace();
}
reader[i].close();
}
document.close();
}

Related

Is there a way to get a generic mp3 tag using mp3agic?

I'm developing an MP3 player with java, using mp3agic to edit .mp3 files metadata. The problem is: I don't know the specific tags of the files to edit the desired data.
Here's my code to get the mp3 track for example:
public static int get_rep(Music msc)
{
try
{
Mp3File file = new Mp3File(msc.get_path());
if (file.hasId3v1Tag())
{
ID3v1 tag = file.getId3v1Tag();
return Integer.parseInt(tag.getTrack());
}
else if (file.hasId3v2Tag())
{
ID3v2 tag = file.getId3v2Tag();
return Integer.parseInt(tag.getTrack());
}
}
catch (UnsupportedTagException e)
{
e.printStackTrace();
}
catch (InvalidDataException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return -1;
}
Is there a way to get the tag value skipping file.hasId3v1Tag() and file.hasId3v2Tag() verifications?
I tried:
private static Object get_tag(Music msc)
{
try
{
Mp3File file = new Mp3File(msc.get_path());
if (file.hasId3v1Tag())
{
return file.getId3v1Tag();
}
else if (file.hasId3v2Tag())
{
return file.getId3v2Tag();
}
/*
else if(file.hasCustomTag())
{
file.removeCustomTag();
return file.getCustomTag();
}
*/
}
catch (UnsupportedTagException e)
{
e.printStackTrace();
}
catch (InvalidDataException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return Boolean.FALSE;
}
But I still have to check the tags, then cast the Object value to a tag value, which means I'd have to know it anyway. I'm accepting any suggestions, even exchanging mp3agic.
ID3v2 extends ID3v1, so you should be able to use ID3v1 tag = file.getId3v2Tag(); and be able to extract ID3v1 data from it.
You could try this:
private static ID3v1 get_tag(Music msc) {
try {
Mp3File file = new Mp3File(msc.get_path());
if (file.hasId3v1Tag()) {
return file.getId3v1Tag();
} else if (file.hasId3v2Tag()) {
return file.getId3v2Tag();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

Get data from getter method instead of passing in a Bundle

I need to pass some Bitmaps from one activity to another, and, since the size limit of the Bundle won't let me pass these images (even using a byte array*), I thought that I could use a getter method between these Activities.
-But, since I'm still not a master in Android (Java), I don't know if that would make any difference, and, if it does, what should I watch out for when using it.
the byte array did reduce the total size(at about 60%), but it still wasn't enough
scaling down is a way out, but just in case any other solution works
save your object in a file
private void saveDataToFile() {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = getContext().openFileOutput("fileName", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
try {
if (objectOutputStream != null) {
objectOutputStream.writeObject(yourObject); //which data u want to save
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (objectOutputStream != null) {
objectOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Retrieve the object from another activity
private void getDataFromFile() {
FileInputStream fileInputStream = null;
try {
fileInputStream = getContext().openFileInput("fileName");
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(fileInputStream);
} catch (IOException |NullPointerException e) {
e.printStackTrace();
}
try {
yourObject = (ObjectClass) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Pass through Uri by writing getter method in POJO class
If you want to use getter setter, just create URI of your bitmap and pass to setter method in POJO class and then retrieve using getter method of POJO class.

Opening file when hyperlink is clicked - Java FX

I have hyperlink created in a textflow container in Java FX. The code provided below only opens the last hyperlinked file even when the preceding links are clicked. I think the problem is in the iteration. Kindly bear with me as I am still fresh on Java. `
String[] splits = lessonResources.split("\\s+");
for(String s: splits){
link = new Hyperlink(s);
lessonResourcesTextFlow.getChildren().add(link);
linked = new File(s);
link.setOnAction((ActionEvent e) -> {
try {
if(Desktop.isDesktopSupported()){
try {
Desktop.getDesktop().open(linked);
} catch (IOException ex) {
Logger.getLogger(LessonPlanController.class.getName ()).log(Level.SEVERE, null, ex);
}
}
} catch (Exception e) {
System.out.println(e);
}
});
}
Including the file constructor within the link event handler solved my problem.
String[] splits = lessonResources.split("\\s+");
for(String s: splits){
link = new Hyperlink(s);
lessonResourcesTextFlow.getChildren().add(link);
link.setOnAction((ActionEvent e) -> {
linked = new File(s);
try {
if(Desktop.isDesktopSupported()){
try {
Desktop.getDesktop().open(linked);
} catch (IOException ex) {
Logger.getLogger(LessonPlanController.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (Exception e) {
System.out.println(e);
}
});
}

JAVA: Convert IEnvelope to Shapefile

I am currently attempting to convert an IEnvelope to a Shapefile and am at a stand still. I am at a point where I have retrieved the IEnvelope from the Shapefile but am not sure how to go about converting to a Shapefile object. The relevant code I have is listed below:
private final ResultsMenuAction createLayerAction = new ResultsMenuAction("uiCreateLayer") {
protected void doActionPerformed(ActionEvent e) {
System.out.println("Button hit!");
try {
ImmutableSet<FeatureWrapper> features = getFeatures();
Iterator itr = features.iterator();
while(itr.hasNext()){
FeatureWrapper wrapper = (FeatureWrapper) itr.next();
IGeometry geom = wrapper.getOriginalShape();
IEnvelope envelope = geom.getEnvelope();
}
System.out.println("Done");
} catch (ArcEngineException e1) {
e1.printStackTrace();
} catch (AutomationException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
};

Printing to multiple output files in java

I have two arrays that I want to print to separate files. Here's my code:
try {
PrintStream out = new PrintStream(new FileOutputStream(
"Edges.txt"));
for (i = 0; i < bcount; i++) {
out.println(b[i][0] + " " + b[i][1]);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
PrintStream out = new PrintStream(new FileOutputStream(
"Nodes.txt"));
for (i = 0; i < bigbIter; i++) {
out.println(bigb[i]);
}
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
If I only use the first set of try / catch / catch, it works perfectly. But when I use both it doesn't work, giving me the errors "illegal start of type ... } catch" and "error: class, interface, or enum expected". What am I doing wrong?
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
You have an extra }, which throws off the parser and gives you lots of errors.
You should write a method to write to the file. Just pass the file name and data. You should see that you have too many closing brackets, get your IDE to highlight brackets.
Lesson is just don't copy/paste and then edit the catch block when you want it again!
Edit: Also in java 7 you can have multiple catches in one block, it is better to do this:
catch (FileNotFoundException | IOException e)
{
}

Categories