gpt4 book ai didi

c# - 在另一个 pdf 的所有页面中添加一个 pdf 的一组注释并创建一个新的输出 pdf

转载 作者:行者123 更新时间:2023-11-30 17:32:51 25 4
gpt4 key购买 nike

我为输入文件创建了一个阅读器,为标记文件创建了一个阅读器。我不确定我是否应该遍历注释然后将它们一个一个地添加到输出中,或者是否有办法从标记文件中提取所有注释并将它们添加到输入文件中并保留它们的 x,z 坐标。

我有下面的代码,但我不确定在注释部分该做什么。 AddAnnotation 方法仅将 PdfAnnotation 作为输入,但我不确定如何将 PdfDictionary 转换为 PdfAnnotaiton。

class Program
{
public static string inputFile = @"E:\pdf-sample.pdf";
public static string markupFile = @"E:\StampPdf.pdf";
public static string outputFile = @"E:\pdf.pdf";

public static PdfReader inputReader = new PdfReader(inputFile);
public static PdfReader markupReader = new PdfReader(markupFile);

static void Main(string[] args)
{
PdfDocument inputDoc = new PdfDocument(inputReader, new PdfWriter(outputFile));

PdfDocument markupDoc = new PdfDocument(markupReader);

int n = inputDoc.GetNumberOfPages();
for (int i = 1; i <= n; i++)
{
PdfPage page = inputDoc.GetPage(i);

PdfDictionary markupPage = markupDoc.GetFirstPage().GetPdfObject();
PdfArray annots = markupPage.GetAsArray(PdfName.Annots);

if(annots != null)
{
for(int j=0; j < annots.Size(); j++)
{
PdfDictionary annotItem = annots.GetAsDictionary(i);
//******
//page.AddAnnotation(?);
//******
}
}
}
inputDoc.Close();
}
}

在 iText7 中找到新的 GetAnnotations 方法后,我尝试了另一种变体。此处代码运行良好,但我无法打开 O/P 文件并收到文件已损坏的错误消息。此外,当我运行 inputDoc.Close() 而不是下面给出的最后一行时,我收到错误消息“Pdf 间接对象属于其他 PDF 文档。将对象复制到当前 pdf 文档。”

        PdfReader ireader = new PdfReader(inputFile);
PdfDocument inputDoc = new PdfDocument(ireader, new PdfWriter(outputFile));
PdfReader mreader = new PdfReader(markupFile);
PdfDocument markupDoc = new PdfDocument(mreader);
var annots = markupDoc.GetFirstPage().GetAnnotations();
if (annots != null)
{
for (int j = 0; j < annots.Count(); j++)
{
inputDoc.GetFirstPage().AddAnnotation(annots[j]);
}
}
ireader.Close();
mreader.Close();
markupDoc.Close();
inputDoc.SetCloseWriter(true);

最佳答案

也许试试这个:

    if (annots != null)
{
for (int j = 0; j < annots.Size(); j++)
{
PdfDictionary annotItem = annots.GetAsDictionary(i);
PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(annotItem);
page.AddAnnotation(lineAnnotation);
}
}

如果它不起作用,这里有一些文档(不幸的是在 Java 中)

http://developers.itextpdf.com/examples/actions-and-annotations/clone-creating-and-adding-annotations

如果您可以发布带有您希望复制的注释的 Pdf - 也许我可以调试并尝试更多。

关于c# - 在另一个 pdf 的所有页面中添加一个 pdf 的一组注释并创建一个新的输出 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45501663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com