Hi Guillaume,

here comes a main class that works. You have only to set the paths for the 
source and target file (or rewrite the main method to use the args array 
instead).
Dependency is Apache commons-io.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;

public class ConverterMain {

    private final PdfBoxJpgConverter converter = new PdfBoxJpgConverter();

    public static void main(String[] args) throws Exception {
        File source = new File(<insert your jpg file here>);
        File target = new File(<insert your target pdf file here>);
        new ConverterMain().convert(source, target);
    }

    private void convert(final File source, final File target) throws Exception 
{
        if (!target.exists()) {
            target.getParentFile().mkdirs();
            target.createNewFile();
        }

        InputStream in = null;
        OutputStream out = null;

        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(target);

            this.converter.convert(in, out);
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

}

Happy holidays,
Florian

-----Ursprüngliche Nachricht-----
Von: Guillaume Bailleul [mailto:[email protected]] 
Gesendet: Donnerstag, 19. Dezember 2013 21:07
An: [email protected]
Betreff: Re: To PDF/A converted JPG has corrupt XMP-Metdata

Hi,

Could please create and send me a main class doing the stuff ? I will have a 
look to your problem.

KR,

Guillaume

On Thu, Nov 21, 2013 at 11:36 AM, Kaiser, Florian (Extern) 
<[email protected]> wrote:
> Hi,
>
> using the infos from
> http://svn.apache.org/repos/asf/pdfbox/trunk/examples/src/main/java/or
> g/ apache/pdfbox/examples/pdfa/CreatePDFA.java and 
> http://java2s.com/Open-Source/Java/PDF/PDFBox-1.4.0/org/apache/pdfbox/
> ex amples/pdmodel/ImageToPDF.java.htm I wrote my own program that 
> converts JPG to PDF/A-1b with PdfBox 2.0.0-SNAPSHOT.
>
> All works fine but according to 3-Heights PDF Validator Online Tool 
> and the PDF-Validation of PdfBox the PDF is not a valid PDF/A-1b.
> 3-Heights tells me
>
>         Validating file "PdfBox-pic23392.pdf" for conformance level 
> pdfa-1b
>         XML line 1:1: Start tag expected, '<' not found.
>         The document does not conform to the requested standard.
>         The document's meta data is either missing or inconsistent or 
> corrupt.
>
> and PdfBox-Validation
>
>         java.lang.AssertionError: File is not a valid PDF/A-1b:
>         Error on MetaData, Failed to parse [7.1]
>
> The source code of my program is
>
>         import java.io.IOException;
>         import java.io.InputStream;
>         import java.io.OutputStream;
>         import javax.xml.transform.TransformerException;
>         import org.apache.jempbox.xmp.XMPMetadata;
>         import org.apache.jempbox.xmp.pdfa.XMPSchemaPDFAId;
>         import org.apache.pdfbox.pdmodel.PDDocument;
>         import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
>         import org.apache.pdfbox.pdmodel.PDPage;
>         import org.apache.pdfbox.pdmodel.common.PDMetadata;
>         import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
>         import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent;
>         import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
>         import
> org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
>
>         public class PdfBoxJpgConverter {
>
>             public void convert(final InputStream in, final 
> OutputStream
> out) throws Exception {
>
>                 PDDocument doc = null;
>                 try {
>                     doc = new PDDocument();
>
>                     PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
>
>                     doc.addPage(page);
>
>                     PDXObjectImage ximage = new PDJpeg(doc, in);
>
>                     float scaleFactor = getFitToPageScaleFactor(page, 
> ximage);
>
>                     PDPageContentStream contentStream = new 
> PDPageContentStream(doc, page);
>
>                     contentStream.drawXObject(ximage, 0, 0, 
> scaleFactor
> * ximage.getWidth(), scaleFactor * ximage.getHeight());
>
>                     writeXmpMetadata(doc);
>                     writeOutputIntent(doc);
>
>                     contentStream.close();
>                     doc.save(out);
>                 } finally {
>                     if (doc != null) {
>                         doc.close();
>                     }
>                 }
>
>             }
>
>             private float getFitToPageScaleFactor(final PDPage page, 
> final PDXObjectImage image) {
>                 float pageWidth = page.getMediaBox().getWidth();
>                 float pageHeight = page.getMediaBox().getHeight();
>                 float imageWidth = image.getWidth();
>                 float imageHeight = image.getHeight();
>                 return Math.min(pageHeight / imageHeight, pageWidth / 
> imageWidth);
>             }
>
>             private void writeXmpMetadata(final PDDocument document) 
> throws IOException, TransformerException {
>                 PDDocumentCatalog catalog = 
> document.getDocumentCatalog();
>                 PDMetadata metadata = new PDMetadata(document);
>                 XMPMetadata xmp = new XMPMetadata();
>
>                 XMPSchemaPDFAId pdfaid = new XMPSchemaPDFAId(xmp);
>                 pdfaid.setConformance("B");
>                 pdfaid.setPart(1);
>                 pdfaid.setAbout("");
>
>                 xmp.addSchema(pdfaid);
>                 metadata.importXMPMetadata(xmp);
>                 catalog.setMetadata(metadata);
>             }
>
>             private void writeOutputIntent(final PDDocument doc) 
> throws Exception {
>                 PDDocumentCatalog cat = doc.getDocumentCatalog();
>
>                 InputStream colorProfile = 
> PdfBoxJpgConverter.class.getResourceAsStream("/sRGB_IEC61966-2-1_black
> _s
> caled.icc");
>
>                 PDOutputIntent oi = new PDOutputIntent(doc, 
> colorProfile);
>                 oi.setInfo("sRGB IEC61966-2.1");
>                 oi.setOutputCondition("sRGB IEC61966-2.1");
>                 oi.setOutputConditionIdentifier("sRGB IEC61966-2.1");
>                 oi.setRegistryName("http://www.color.org";);
>
>                 cat.addOutputIntent(oi);
>             }
>
>         }
>
> Am I doing something wrong or is this a bug?
>
> Thanks for the help.
>
> Florian

Reply via email to