How can I copy annotations / selections from one PDF file into an updated version of this PDF document?

Recently, I switched to almost exclusively electronic books. I prefer highlighting documents with highlighting or annotations when I read them.

However, when I receive an updated version of the O'Reilly PDF file, for example, provide access to the corrected versions of the books you bought, then I was stuck with the old copy marked and the newer version to copy, without my notes.

Preferred C # language I understand that iTextSharp is probably what I will need to use if I want to do this programmatically (see for example Copy pdf annotations via C # ), but is there an easier way to handle this?

I can’t believe that I am the only one who has this problem, so maybe there is already a solution that will handle this for me?

+4
source share
1 answer

You can use this example for iTextSharp to solve your problem:

var output = new MemoryStream(); using (var document = new Document(PageSize.A4, 70f, 70f, 20f, 20f)) { var readers = new List<PdfReader>(); var writer = PdfWriter.GetInstance(document, output); writer.CloseStream = false; document.Open(); const Int32 requiredWidth = 500; const Int32 zeroBottom = 647; const Int32 left = 50; Action<String, Action> inlcudePdfInDocument = (filename, e) => { var reader = new PdfReader(filename); readers.Add(reader); var pageCount = reader.NumberOfPages; for (var i = 0; i < pageCount; i++) { e?.Invoke(); var imp = writer.GetImportedPage(reader, (i + 1)); var scale = requiredWidth / imp.Width; var height = imp.Height * scale; writer.DirectContent.AddTemplate(imp, scale, 0, 0, scale, left, zeroBottom - height); var annots = reader.GetPageN(i + 1).GetAsArray(PdfName.ANNOTS); if (annots != null && annots.Size != 0) { foreach (var a in annots) { var newannot = new PdfAnnotation(writer, new Rectangle(0, 0)); var annotObj = (PdfDictionary) PdfReader.GetPdfObject(a); newannot.PutAll(annotObj); var rect = newannot.GetAsArray(PdfName.RECT); rect[0] = new PdfNumber(((PdfNumber)rect[0]).DoubleValue * scale + left); // Left rect[1] = new PdfNumber(((PdfNumber)rect[1]).DoubleValue * scale); // top rect[2] = new PdfNumber(((PdfNumber)rect[2]).DoubleValue * scale + left); // right rect[3] = new PdfNumber(((PdfNumber)rect[3]).DoubleValue * scale); // bottom writer.AddAnnotation(newannot); } } document.NewPage(); } } foreach (var apprPdf in pdfs) { document.NewPage(); inlcudePdfInDocument(apprPdf.Pdf, null); } document.Close(); readers.ForEach(x => x.Close()); } output.Position = 0; return output; 

This example copies the list of PDF files with annotations to a new pdf file.

Receive data from two PdfReaders at the same time - one for copying a new pdf, and the other for copying annotations from an old pdf.

0
source

All Articles