【发布时间】:2013-03-31 13:36:11
【问题描述】:
我正在使用 iTextSharp 从图像生成 pdf-a 文档。到目前为止,我还没有成功。
编辑:我正在使用 iTextSharp 生成 PDF
我所做的只是制作一个带有一些图像的 pdf-a 文档(1a 或 1b,随便什么)。这是我到目前为止提出的代码,但是当我尝试使用pdf-tools 或validatepdfa 验证它们时,我不断收到错误消息。
这是我从 pdf-tools 得到的错误(使用 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();
这是 prepareImage()
它用于将图像展平为 bmp,因此我无需为 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 以进行长期数据存储
编辑:添加了一些我用来测试的文件
PDFs and Pictures.rar (3.9 MB)
https://mega.co.nz/#!n8pClYgL!NJOJqSO3EuVrqLVyh3c43yW-u_U35NqeB0svc6giaSQ
【问题讨论】:
-
向 iText 人提出一个错误可能值得。
-
为什么将一致性级别设置为 PDF/A-1a 然后对照 1b 进行检查?保持一致会很好。另外,为什么要两次打开文档?另外,我会先尝试解决其他错误——文件结构损坏等错误很容易干扰色彩空间的(较小)问题......
-
@David 好的,感谢您的回复。虽然我现在几乎所有东西都可以正常工作了。只有
color space不正确。我已经对代码添加了一些修改。 -
您插入的图像的色彩空间是多少?你能分享一个示例PDF吗?这样我就可以通过 pdfToolbox PDF/A 验证运行它,并且可能有一个更具描述性的错误消息。
-
我们正在尝试将扫描的图像转换为 PDF/A 以进行长期数据存储。我已经上传了一个包含我用于测试的文件的 zip:PDF 和 Pictures.rar (3.9 MB) mega.co.nz/…
标签: c# .net pdf pdf-generation itextsharp