使用iTextSharp将PDFA文档连接到有效的PDFA文档中

时间:2012-01-03 12:04:54

标签: .net itextsharp pdfa

我正在尝试使用以下代码使用iTextSharp 5.1.3将多个PDFA文档连接到一个文件中:

    Try
        Dim f As Integer = 0
        Dim outFile As String = destinationFile
        Dim document As iTextSharp.text.Document = Nothing
        Dim writer As PdfSmartCopy = Nothing
        While f < sourceFiles.Length
            ' Create a reader for a certain document
            Dim reader As New PdfReader(sourceFiles(f))

            ' Retrieve the total number of pages
            Dim n As Integer = reader.NumberOfPages
            If f = 0 Then
                document = New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
                writer = New PdfSmartCopy(document, New FileStream(outFile, FileMode.Create))
                document.Open()
            End If

            Dim page As PdfImportedPage
            Dim i As Integer = 0
            While i < n
                i += 1
                page = writer.GetImportedPage(reader, i)
                writer.AddPage(page)
            End While
            Dim form As PRAcroForm = reader.AcroForm
            If form IsNot Nothing Then
                writer.CopyAcroForm(reader)
            End If
            f += 1
        End While
        document.Close()
    Catch generatedExceptionName As Exception
    End Try

如果我在Acrobat Reader X中打开任何输入文件,我会收到一条消息,说明它们确实是PDFA,但如果我打开上面代码创建的输出文件则不会。因此,我新创建的连接PDF文档似乎不符合PDFA。

我已经尝试将writer.PDFXConformance属性设置为PdfWriter.PDFA1A,但这没有帮助。

有谁知道是否有可能实现我想要做的事情?

1 个答案:

答案 0 :(得分:1)

使用PDFXConformance时,您还需要在CreateXmpMetadata对象上调用writer。我通常在关闭文档之前就这样做了,我并不是100%肯定它需要去那里但是其他人都会把它放在那里。

End While
writer.CreateXmpMetadata()
document.Close()

然后,当你实例化你的编写器时,就像你一样设置一致性属性:

writer = New PdfSmartCopy(document, New FileStream(outFile, FileMode.Create))
writer.PDFXConformance = PdfWriter.PDFA1A