入力ファイル用のリーダーとマークアップファイル用のリーダーを作成しました。注釈をループして出力に1つずつ追加するか、マークアップファイルからすべての注釈を取得してx、z座標を保持する入力ファイルに追加する方法があるかどうかはわかりません。別のpdfのすべてのページで1つのPDFから注釈のグループを追加して新しい出力を作成する
私は以下のコードを持っていますが、私はコメントのセクションで何をすべきか分かりません。 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);
pdfを読むためにどのライブラリを使用していますか? – Piotr
@Piotr - 私はiText7を使用しています –