Hig*_*don 8 .net c# pdf pdf-generation itextsharp
我正在使用iTextSharp从图像生成pdf-a文档.到目前为止,我还没有成功.
编辑:我正在使用iTextSharp生成PDF
我所做的就是用一些图像制作一个pdf文件(1a或1b,无论适合什么).这是我到目前为止提出的代码,但是当我尝试使用pdf-tools或validatepdfa验证它们时,我一直遇到错误.
这是我从pdf-tools获得的错误(使用PDF/A-1b验证): 编辑:MarkInfo和Color Space尚未运行.剩下的还可以
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.
Run Code Online (Sandbox Code Playgroud)
主流
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();
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我找到修复错误的方向吗?特别是device-specific color spaces
编辑:更多解释:我想要实现的是,将扫描图像转换为PDF/A以进行长期数据存储
编辑:添加了一些我用来测试
PDF和Pictures.rar(3.9 MB)的文件
https://mega.co.nz/#!n8pClYgL!NJOJqSO3EuVrqLVyh3c43yW-u_U35NqeB0svc6giaSQ
好的,我在 callas pdfToolbox 中检查了您的一个文件,它显示:“已使用设备色彩空间,但没有 PDF/A 输出意图”。我认为这是您在将输出意图写入文档时做错了什么的迹象。然后我使用相同的工具将该文档转换为 PDF/A-1b,差异很明显。
也许还有其他错误需要修复,但这里的第一个错误是您在名为“OutputIntent”的 PDF 文件的目录字典中放置了一个键。这是错误的:PDF 规范第 75 页指出该键应命名为“OutputIntents”。
就像我说的,也许您的文件还存在其他问题,但密钥名称错误会导致 PDF/A 验证器找不到您尝试放入文件中的输出意图...
| 归档时间: |
|
| 查看次数: |
5771 次 |
| 最近记录: |