I have a weird bug when adding annotations with Spire.PDF for Android V3.1.0.
Basically, if I create a new PDF and add an annotation, it works.
But if I create a new PDF, save it, read it back and add the annotation, it doesn't work.
You can find my whole Android Studio project as an attachment, but this is my code:
- Code: Select all
package com.example.spiretest;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.spire.license.LicenseProvider;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfPopupAnnotation;
import com.spire.pdf.annotations.PdfPopupIcon;
import com.spire.pdf.graphics.PdfFont;
import com.spire.pdf.graphics.PdfFontFamily;
import com.spire.pdf.graphics.PdfPens;
import com.spire.pdf.graphics.PdfRGBColor;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private static final String licenseKey = "Insert the license key here";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testPdf();
}
private void testPdf(){
String createPdfPath = new File(getExternalFilesDir(null), "createPdf-Test.pdf").getAbsolutePath();
String createAppendPdfPath1 = new File(getExternalFilesDir(null), "createAppendPdf-Test1.pdf").getAbsolutePath();
String createAppendPdfPath2 = new File(getExternalFilesDir(null), "createAppendPdf-Test2.pdf").getAbsolutePath();
LicenseProvider.setLicenseKey(licenseKey);
createPdf(createPdfPath);
createAppendPdf(createAppendPdfPath1, createAppendPdfPath2);
}
/**
* This method works and creates a PDF with an annotation.
* @param path Where to save the PDF.
*/
private void createPdf(String path){
try {
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.appendPage();
addAnnotation(page);
doc.saveToFile(path);
} catch (Exception ex){
ex.printStackTrace();
}
}
/**
* This method creates an empty PDF.
* Then it reads the PDF and adds an annotation.
* However, the resulting PDF only has the text, the annotation is missing.
* @param path Where to save the empty PDF.
* @param output Where to save the PDF which should have an annotation.
*/
private void createAppendPdf(String path, String output){
try {
PdfDocument doc = new PdfDocument();
doc.appendPage();
doc.saveToFile(path);
doc = new PdfDocument(path);
addAnnotation(doc.getPages().get(0));
doc.saveToFile(output);
} catch (Exception ex){
ex.printStackTrace();
}
}
private void addAnnotation(PdfPageBase page){
page.getCanvas().drawString("Comment:", new PdfFont(PdfFontFamily.Times_Roman, 12), PdfPens.getBlack(), 10, 120);
PdfPopupAnnotation testAnnotation = new PdfPopupAnnotation(new RectF(100, 100, 150, 140), "Test");
testAnnotation.setIcon(PdfPopupIcon.Comment);
testAnnotation.setColor(new PdfRGBColor(Color.BLUE));
page.getAnnotationsWidget().add(testAnnotation);
}
}
The resulting PDFs are stored in "internal storage/Android/data/com.example.spiretest/files".
The PDF "createPdf-Test" has the annotation like I would expect.
The PDF "createAppendPdf-Test2" should have the same annotation but does not have one.
Thank you for looking into this.
BTW: With Spire.PDF for C#, this works as expected.