【问题标题】:iText set conformance leveliText 设置一致性级别
【发布时间】:2018-05-27 19:44:28
【问题描述】:

目前,我正在尝试使用 iText for C# 创建符合 A-1a 级别的 PDF 文件。这是我目前所拥有的:

var exportFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var exportFile = System.IO.Path.Combine(exportFolder, "Test.pdf");

var writer = new PdfWriter(exportFile);
var pdf = new PdfDocument(writer);
var document = new Document(pdf);
document.Add(new Paragraph("Hello World! Tom"));
document.Close();

我怎样才能为此设置一致性级别?

编辑

我在他们的文档中发现了这一点: https://developers.itextpdf.com/content/itext-7-jump-start-tutorial-net/chapter-7-creating-pdfua-and-pdfa-documents

但我没有得到我必须在第三行代码中替换 INTENT 的内容。 有人能给我一个完整的例子吗,只有一行 Hello World。它不一定是 iText。我愿意接受其他工具。

【问题讨论】:

  • 请注意,文档链接到此处的完整代码示例:developers.itextpdf.com/content/itext-7-jump-start-tutorial/…。 INTENT 链接到 PDF/A 文档所需的颜色配置文件。你可以在这里找到它:git.itextsupport.com/projects/I5JS/repos/sandbox/browse/…
  • 感谢您的帮助。这是否意味着我必须在我的项目中创建一个资源文件夹和一个颜色文件夹并将下载的配置文件放在那里? @JonReilly
  • PdfOutputIntent 的第 5 个参数接受一个包含 .icm 文件的 InputStream 对象。无论是从磁盘、内存还是外部加载都取决于您。
  • @Anokrize 您要求的所有内容都列在 Jon 提供的链接中。第二个链接甚至提供了一个.icm 文件供您下载并与第一个链接中的代码示例一起使用。 INTENT 保存流的 .icm 文件的路径。您可以轻松地将其加载到内存流中,而不是从磁盘中。
  • 确实:所有的例子都可以在网上找到。在 PDF/A 中添加颜色配置文件是强制性的。 Jon 共享的文件就是您可以使用的此类文件的一个示例。请不要滥用 Stack Overflow 来避免必须学习最起码的技术知识才能做好工作。如果您不了解 ISO 19005,则不应接受要求符合 ISO 19005 的工作。如果您了解,则应研究规范,而不是指望其他人为您这样做。看到所有信息都交给了您,但您不“接受”它,这非常令人沮丧。

标签: c# itext itext7


【解决方案1】:

第一步:

下载颜色配置文件。
您可以使用 iText 示例中提供的那个。 http://gitlab.itextsupport.com/itext7/samples/blob/develop/publications/jumpstart/src/main/resources/color/sRGB_CS_profile.icm

第 2 步:

//Initialize PDFA document with output intent
PdfADocument pdf = new PdfADocument(new PdfWriter(dest), PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent
            ("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream("sRGB_CS_profile.icm", FileMode.Open, FileAccess.Read
            )));

Document document = new Document(pdf);

//Fonts need to be embedded
PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.WINANSI, true);
Paragraph p = new Paragraph();
p.SetFont(font);
p.Add(new Text("The quick brown "));

iText.Layout.Element.Image foxImage = new Image(ImageDataFactory.Create(FOX));    
p.Add(foxImage);

p.Add(" jumps over the lazy ");

iText.Layout.Element.Image dogImage = new iText.Layout.Element.Image(ImageDataFactory.Create(DOG));
p.Add(dogImage);

document.Add(p);
document.Close();

【讨论】:

    【解决方案2】:

    尝试替换此行

    var pdf = new PdfDocument(writer);
    

    有了这个

    PdfADocument pdf = new PdfADocument(new PdfWriter(exportFile), PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent
    ("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(INTENT, FileMode.Open, FileAccess.Read
    )));
    Document document = new Document(pdf);
    // Etc...
    

    除此之外,我的建议始终是声明实例化的类,而不是保留泛型(使用 var),正如您从我的示例代码中看到的那样。

    希望这会奏效! 干杯

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 2017-06-03
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多