2013-04-09 16 views
8

Sto usando iTextSharp per generare pdf: documenti da immagini. Finora non ho avuto successo.
Edit: sto usando iTextSharp per generare il PDFRealizza PDF/A conforme a PDF con le sole immagini usando iTextSharp

Tutto quello che cerco di fare è in pdf un documento (1A o 1B, comunque sia), con alcune immagini. Questo è il codice che ho imparato finora, ma continuo a ricevere errori quando provo a convalidarli con pdf-tools o validatepdfa.

Questi sono gli errori che ottengo dagli strumenti pdf (utilizzando la convalida PDF/A-1b): Modifica: MarkInfo e Spazio colore non funzionano ancora. Il resto è a posto

Validating file "0.pdf" for conformance level pdfa-1a 
The key MarkInfo is required but missing. 
A device-specific color space (DeviceRGB) without an appropriate output intent is used. 
The document does not conform to the requested standard. 
The document contains device-specific color spaces. 
The document doesn't provide appropriate logical structure information. 
Done. 

flusso principale

var output = new MemoryStream(); 
using (var iccProfileStream = new FileStream("ToPdfConverter/ColorProfiles/sRGB_v4_ICC_preference_displayclass.icc", FileMode.Open)) 
{ 
    var document = new Document(new Rectangle(PageSize.A4.Width, PageSize.A4.Height), 0f, 0f, 0f, 0f); 
    var pdfWriter = PdfWriter.GetInstance(document, output); 
    pdfWriter.PDFXConformance = PdfWriter.PDFA1A; 
    document.Open(); 

    var pdfDictionary = new PdfDictionary(PdfName.OUTPUTINTENT); 
    pdfDictionary.Put(PdfName.OUTPUTCONDITION, new PdfString("sRGB IEC61966-2.1")); 
    pdfDictionary.Put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1")); 
    pdfDictionary.Put(PdfName.S, PdfName.GTS_PDFA1); 

    var iccProfile = ICC_Profile.GetInstance(iccProfileStream); 
    var pdfIccBased = new PdfICCBased(iccProfile); 
    pdfIccBased.Remove(PdfName.ALTERNATE); 
    pdfDictionary.Put(PdfName.DESTOUTPUTPROFILE, pdfWriter.AddToBody(pdfIccBased).IndirectReference); 

    pdfWriter.ExtraCatalog.Put(PdfName.OUTPUTINTENT, new PdfArray(pdfDictionary)); 

    var image = PrepareImage(imageBytes); 

    document.Open(); 
    document.Add(image); 

    pdfWriter.CreateXmpMetadata(); 

    pdfWriter.CloseStream = false; 
    document.Close(); 
} 
return output.GetBuffer(); 

Questo è prepareImage()
E 'utilizzato per appiattire l'immagine in formato BMP, quindi non c'è bisogno di preoccuparsi canali alfa.

private Image PrepareImage(Stream stream) 
{ 
    Bitmap bmp = new Bitmap(System.Drawing.Image.FromStream(stream)); 
    var file = new MemoryStream(); 
    bmp.Save(file, ImageFormat.Bmp); 
    var image = Image.GetInstance(file.GetBuffer()); 

    if (image.Height > PageSize.A4.Height || image.Width > PageSize.A4.Width) 
    { 
     image.ScaleToFit(PageSize.A4.Width, PageSize.A4.Height); 
    } 
    return image; 
} 

Qualcuno può aiutarmi in una direzione per correggere gli errori? In particolare i device-specific color spaces

Edit: Più spiegazione: Quello che sto cercando di realizzare è, la conversione di immagini digitalizzate in formato PDF/A per l'archiviazione dei dati a lungo termine

Edit: aggiunto alcuni file Sono usando per testare con
PDF e Pictures.rar (3,9 MB)
https://mega.co.nz/#!n8pClYgL!NJOJqSO3EuVrqLVyh3c43yW-u_U35NqeB0svc6giaSQ

+0

forse vale la pena alzare un bug con la gente iText. – Rup

+0

Perché impostare il livello di conformità su PDF/A-1a e quindi verificare contro 1b? Sarebbe bello essere coerenti. Inoltre, perché apri il documento due volte? Inoltre, proverei a risolvere prima gli altri errori - gli errori che hai con la struttura dei file corrotta e così via, potrebbero facilmente interferire con il problema (minore) che hai con gli spazi colore ... –

+0

@David Ok, grazie per il tuo rispondere. Anche se ho già quasi tutto funzionante correttamente ora. Solo lo 'spazio colore 'non è corretto. Ho aggiunto alcune modifiche al codice. – Highmastdon

risposta

1

OK, ho controllato uno dei vostri file in callas pdfToolbox e dice: "spazio colore utilizzato, ma non PDF/Un intento di output ". Che ho preso come segno che fai qualcosa di sbagliato mentre scrivi un intento di output sul documento. Poi ho convertito quel documento in PDF/A-1b con lo stesso strumento e la differenza è ovvia.

Forse ci sono altri errori che è necessario correggere, ma il primo errore è che si inserisce una chiave nel dettato catalogo per il file PDF denominato "OutputIntent". È sbagliato: la pagina 75 della specifica PDF indica che la chiave deve essere denominata "OutputIntents".

Come ho detto, forse ci sono altri problemi con il file al di là di questo, ma il nome sbagliato per la chiave provoca PDF/A non validatori per trovare l'intento di output si tenta di mettere nel file ...

+0

+1; se @Highmastdon avesse usato il metodo 'PdfWriter.SetOutputIntents', sarebbe stato usato il nome corretto ... Se avesse usato' PdfAWriter' invece di 'PdfWriter', alcune cose sarebbero state automaticamente prese in considerazione. – mkl

0
  1. Prima di tutto, pdfx NON è pdfa.

    1. In secondo luogo, si sta utilizzando PdfWriter errato. Dovrebbe essere PdfAWriter.

io non avere soluzione per problema di immagine sfortunatamente, ma ho per 1 e 2.

saluti

using System; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
using System.Text; 
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 
using iTextSharp.text.html.simpleparser; 
using iTextSharp.tool.xml; 
using System.Drawing; 
using System.Drawing.Imaging; 

namespace Tests 
{ 
    /* 
    * References: 
    * UTF-8 encoding http://stackoverflow.com/questions/4902033/itextsharp-5-polish-character 
    * PDFA http://www.codeproject.com/Questions/661704/Create-pdf-A-using-itextsharp 
    * Images http://stackoverflow.com/questions/15896581/make-a-pdf-conforming-pdf-a-with-only-images-using-itextsharp 
    */ 

    [TestClass] 
    public class UnitTest1 
    { 
     /* 
     * IMPORTANT: Restrictions with html usage of tags and attributes 
     * 1. Dont use * <head> <title>Sklep</title> </head>, because title is rendered to the page 
     */ 

     // Test cases 
     static string contents = "<html><body style=\"font-family:arial unicode ms;font-size: 8px;\"><p style=\"text-align: center;\"> Davčna številka dolžnika: 74605968<br /> </p><table> <tr> <td><b>\u0160t. sklepa: 88711501</b></td> <td style=\"text-align: right;\">Davčna številka dolžnika: 74605968</td> </tr> </table> <br/><img src=\"http://img.rtvslo.si/_static/images/rtvslo_mmc_logo.png\" /></body></html>"; 
     //static string contents = "<html><body style=\"font-family:arial unicode ms;font-size: 8px;\"><p style=\"text-align: center;\"> Davčna številka dolžnika: 74605968<br /> </p><table> <tr> <td><b>\u0160t. sklepa: 88711501</b></td> <td style=\"text-align: right;\">Davčna številka dolžnika: 74605968</td> </tr> </table> <br/></body></html>"; 

     //[TestMethod] 
     public void CreatePdfHtml() 
     { 
      createPDF(contents, true);   
     } 

     private void createPDF(string html, bool isPdfa) 
     { 
      TextReader reader = new StringReader(html); 
      Document document = new Document(PageSize.A4, 30, 30, 30, 30); 
      HTMLWorker worker = new HTMLWorker(document); 

      PdfWriter writer; 
      if (isPdfa) 
      { 
       //set conformity level 
       writer = PdfAWriter.GetInstance(document, new FileStream(@"c:\temp\testA.pdf", FileMode.Create), PdfAConformanceLevel.PDF_A_1B); 

       //set pdf version 
       writer.SetPdfVersion(PdfAWriter.PDF_VERSION_1_4); 

       // Create XMP metadata. It's a PDF/A requirement. 
       writer.CreateXmpMetadata(); 
      } 
      else 
      { 
       writer = PdfWriter.GetInstance(document, new FileStream(@"c:\temp\test.pdf", FileMode.Create)); 
      } 

      document.Open(); 

      if (isPdfa) // document should be opend, or it will fail 
      { 
       // Set output intent for uncalibrated color space. PDF/A requirement. 
       ICC_Profile icc = ICC_Profile.GetInstance(Environment.GetEnvironmentVariable("SystemRoot") + @"\System32\spool\drivers\color\sRGB Color Space Profile.icm"); 
       writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc); 
      } 

      //register font used in html 
      FontFactory.Register(Environment.GetEnvironmentVariable("SystemRoot") + "\\Fonts\\ARIALUNI.TTF", "arial unicode ms"); 

      //adding custom style attributes to html specific tasks. Can be used instead of css 
      //this one is a must fopr display of utf8 language specific characters (čćžđpš) 
      iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet(); 
      ST.LoadTagStyle("body", "encoding", "Identity-H"); 
      worker.SetStyleSheet(ST); 

      worker.StartDocument(); 
      worker.Parse(reader); 
      worker.EndDocument(); 
      worker.Close(); 
      document.Close(); 
     } 

    } 


} 
Problemi correlati