Convertir archivo PDF a PDF/A
Aspose.PDF permite convertir un archivo PDF a un archivo PDF/A compatible. Antes de hacerlo, el archivo debe ser validado. Este artículo explica cómo.
Tenga en cuenta que seguimos Adobe Preflight para validar la conformidad PDF/A. Todas las herramientas del mercado tienen su propia “representación” de la conformidad PDF/A. Por favor, consulte este artículo en Herramientas de validación PDF/A para referencia. Elegimos productos de Adobe para verificar cómo Aspose.PDF produce archivos PDF porque Adobe está en el centro de todo lo relacionado con PDF.
Antes de convertir el PDF a un archivo compatible con PDF/A, valide el PDF utilizando el método validate. El resultado de la validación se almacena en un archivo XML y luego este resultado también se pasa al método convert. También puede especificar la acción para los elementos que no se pueden convertir utilizando el AcciónDeErrorDeConversión enumeración.
Conversión de PDF a PDF/A_1b
El siguiente fragmento de código muestra cómo convertir archivos PDF a PDF compatible con PDF/A-1b.
public void convertPDFtoPDFa1b() {
// Open document
try {
document = new Document(inputStream);
} catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
// Convert to PDF/A compliant document
// During conversion process, the validation is also performed
File logFileName = new File(fileStorage,"PDF-to-PDFA-log.xml");
File pdfaFileName = new File(fileStorage,"PDF-to-PDFA.pdf");
document.convert(logFileName.toString(), PdfFormat.PDF_A_1B, ConvertErrorAction.Delete);
// Save output document
document.save(pdfaFileName.toString());
}
Para realizar solo la validación, use la siguiente línea de código:
public void ValidatePDF_A_1B() {
// Open document
try {
document = new Document(inputStream);
} catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
// Validate to PDF/A compliant document
File logFileName = new File(fileStorage,"PDF-to-PDFA-log.xml");
if (document.validate(logFileName.toString(), PdfFormat.PDF_A_1B)){
resultMessage.setText("Document is valid");
}
else {
resultMessage.setText("Document is not valid");
}
}
Conversión de PDF a PDF/A_3b
public void convertPDFtoPDFa3b() {
// Open document
try {
document = new Document(inputStream);
} catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
// Convert to PDF/A compliant document
// During conversion process, the validation is also performed
File logFileName = new File(fileStorage,"PDF-to-PDFA-log.xml");
File pdfaFileName = new File(fileStorage,"PDF-to-PDFA.pdf");
document.convert(logFileName.toString(), PdfFormat.PDF_A_3B, ConvertErrorAction.Delete);
// Save output document
try {
document.save(pdfaFileName.toString());
}
catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
resultMessage.setText(R.string.success_message);
}
Conversión de PDF a PDF/A_3a
public void convertPDFtoPDFa3a() {
// Open document
try {
document = new Document(inputStream);
} catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
// Convert to PDF/A compliant document
// During conversion process, the validation is also performed
File logFileName = new File(fileStorage,"PDF-to-PDFA-log.xml");
File pdfaFileName = new File(fileStorage,"PDF-to-PDFA.pdf");
document.convert(logFileName.toString(), PdfFormat.PDF_A_3A, ConvertErrorAction.Delete);
// Save output document
try {
document.save(pdfaFileName.toString());
}
catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
resultMessage.setText(R.string.success_message);
}
Conversión de PDF a PDF/A_2a
public void convertPDFtoPDFa2a() {
// Open document
try {
document = new Document(inputStream);
} catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
// Convert to PDF/A compliant document
// During conversion process, the validation is also performed
File logFileName = new File(fileStorage,"PDF-to-PDFA-log.xml");
File pdfaFileName = new File(fileStorage,"PDF-to-PDFA.pdf");
document.convert(logFileName.toString(), PdfFormat.PDF_A_2A, ConvertErrorAction.Delete);
// Save output document
try {
document.save(pdfaFileName.toString());
}
catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
resultMessage.setText(R.string.success_message);
}
Conversión de PDF a PDF/A_3U
public void convertPDFtoPDFa3u() {
// Open document
try {
document = new Document(inputStream);
} catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
// Convert to PDF/A compliant document
// During conversion process, the validation is also performed
File logFileName = new File(fileStorage,"PDF-to-PDFA-log.xml");
File pdfaFileName = new File(fileStorage,"PDF-to-PDFA.pdf");
document.convert(logFileName.toString(), PdfFormat.PDF_A_3U, ConvertErrorAction.Delete);
// Save output document
try {
document.save(pdfaFileName.toString());
}
catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
resultMessage.setText(R.string.success_message);
}
Crear PDF/A-3 y adjuntar archivo XML
Aspose.PDF for Android via Java ofrece la función de convertir archivos PDF al formato PDF/A y también admite la capacidad de agregar archivos como adjuntos a un documento PDF. En caso de que tenga el requisito de adjuntar archivos al formato de cumplimiento PDF/A, recomendamos utilizar el valor PDF_A_3A del enumerado com.aspose.pdf.PdfFormat; PDF/A_3a es el formato que brinda la función de adjuntar cualquier tipo de archivo como adjunto a un archivo compatible con PDF/A. Sin embargo, una vez que el archivo está adjunto, debe convertirlo nuevamente al formato Pdf-3a, para corregir los metadatos. Por favor, revise el siguiente fragmento de código.
public void convertPDFtoPDFa3u_attachXML() {
// Open document
try {
document = new Document(inputStream);
} catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
// Convert to PDF/A compliant document
// During conversion process, the validation is also performed
File logFileName = new File(fileStorage,"PDF-to-PDFA-log.xml");
File pdfaFileName = new File(fileStorage,"PDF-to-PDFA.pdf");
File attachment = new File(fileStorage,"sample.xml");
// Save output document
try {
// load XML file
FileSpecification fileSpecification = new FileSpecification(attachment.toString(), "Sample xml file");
// Add attachment to document's attachment collection
document.getEmbeddedFiles().add(fileSpecification);
document.convert(logFileName.toString(), PdfFormat.PDF_A_3B, ConvertErrorAction.Delete);
document.save(pdfaFileName.toString());
}
catch (Exception e) {
resultMessage.setText(e.getMessage());
return;
}
resultMessage.setText(R.string.success_message);
}