我使用iTextSharp从图像生成pdf文档。到目前为止,我还没有成功。
编辑:我使用iTextSharp的生成PDF使用iTextSharp制作PDF格式的符合PDF/A的图像
所有我尝试是使的PDF文档(1a或1b,无论套),具有一定的图像。这是我到目前为止的代码,但当我尝试使用pdf-tools或validatepdfa来验证它们时,我总是收到错误。
这是我从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
这可能是值得提高与iText的人的错误。 – Rup 2013-04-09 08:49:29
为什么要将一致性级别设置为PDF/A-1a,然后检查1b?保持一致是件好事。另外,为什么要打开文档两次?此外,我会尝试首先解决其他错误 - 你有文件结构损坏等错误,可以很容易地干扰(颜色空间)(小)问题... – 2013-04-09 08:57:34
@大卫好吧,谢谢你的回复。尽管我已经几乎所有的东西都正确工作了。只有'色彩空间'是不正确的。我在代码中添加了一些编辑。 – Highmastdon 2013-04-09 09:51:29