2013-04-09 79 views
8

我使用iTextSharp从图像生成pdf文档。到目前为止,我还没有成功。
编辑:我使用iTextSharp的生成PDF使用iTextSharp制作PDF格式的符合PDF/A的图像

所有我尝试是使的PDF文档(1a或1b,无论套),具有一定的图像。这是我到目前为止的代码,但当我尝试使用pdf-toolsvalidatepdfa来验证它们时,我总是收到错误。

这是我从pdf-工具(使用PDF/A-1b验证)得到的错误: 编辑:MarkInfo和色彩空间还没有工作。剩下的就是好的

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. 

主流

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(); 

这是
它被用来压平到bmp图像的prepareImage(),所以我不需要操心alpha通道。

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; 
} 

任何人都可以帮助我解决错误的方向吗? 具体的device-specific color spaces

编辑:更多的解释:我试图实现是,将扫描的图像以PDF/A用于长期数据存储

编辑:增加了一些文件,我使用带有
PDF和Pictures.rar(3.9 MB)
https://mega.co.nz/#!n8pClYgL!NJOJqSO3EuVrqLVyh3c43yW-u_U35NqeB0svc6giaSQ

+0

这可能是值得提高与iText的人的错误。 – Rup 2013-04-09 08:49:29

+0

为什么要将一致性级别设置为PDF/A-1a,然后检查1b?保持一致是件好事。另外,为什么要打开文档两次?此外,我会尝试首先解决其他错误 - 你有文件结构损坏等错误,可以很容易地干扰(颜色空间)(小)问题... – 2013-04-09 08:57:34

+0

@大卫好吧,谢谢你的回复。尽管我已经几乎所有的东西都正确工作了。只有'色彩空间'是不正确的。我在代码中添加了一些编辑。 – Highmastdon 2013-04-09 09:51:29

回答

1

OK测试,我检查了卡拉斯pdfToolbox文件之一,它说:“使用的设备颜色空间,但没有PDF /输出意图“。我将其作为一个标志,表示在向文档写入输出意向时出现错误。然后我用相同的工具将该文件转换为PDF/A-1b,差别是显而易见的。

也许还有其他错误需要修复,但这里的第一个错误是您在名为“OutputIntent”的PDF文件的目录词典中放置了一个键。这是错误的:PDF规范的第75页指出,密钥应该命名为“OutputIntents”。

就像我说的,也许还有其他问题,您的文件超出了这一点,但是其中一个重要的错误的名称会导致PDF/A验证器找不到您尝试将文件中的输出意向...

+0

+1;如果@Highmastdon使用了“PdfWriter.SetOutputIntents”方法,则会使用正确的名称......如果他使用了“PdfAWriter”而不是“PdfWriter”,则会自动处理更多内容。 – mkl 2013-04-10 07:39:18

0
  1. 首先,pdfx不是pdfa。

    1. 其次,您使用的是错误的PdfWriter。它应该是PdfAWriter。

我没有为形象问题解决可惜的是,但我有1和2。

问候

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(); 
     } 

    } 


} 
相关问题