tags:

views:

108

answers:

2

I have a stream (PDF file with annotations) and another stream (the same PDF file without annotations). I use streams because I need to execute this operations in memory. I need to copy annotations from first document to another. Annotations can be different: comments, highlighting and other. So it is better to copy annotations without parsing it.

Can you advice me some helpful PDF library for .NET? And some sample for this problem.

+1  A: 

I'm using ITextSharp which is forked from IText (a java implemenation fpr pdf editing).

http://sourceforge.net/projects/itextsharp/

http://itextpdf.com/

Edit - this is what you need to do (untested but shoul be close):

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

// return processed stream (a new MemoryStream) 
public Stream copyAnnotations(Stream sourcePdfStream, Stream destinationPdfStream)
{
    // Create new document (IText)
    Document outdoc = new Document(PageSize.A4);

    // Seek to Stream start and create Reader for input PDF
    m.Seek(0, SeekOrigin.Begin);
    PdfReader inputPdfReader = new PdfReader(sourcePdfStream);

    // Seek to Stream start and create Reader for destination PDF
    m.Seek(0, SeekOrigin.Begin);
    PdfReader destinationPdfReader = new PdfReader(destinationPdfStream);

    // Create a PdfWriter from for new a pdf destination stream
    // You should write into a new stream here!
    Stream processedPdf = new MemoryStream();
    PdfWriter pdfw = PdfWriter.GetInstance(outdoc, processedPdf);

    // do not close stream if we've read everything
    pdfw.CloseStream = false;

    // Open document
    outdoc.Open();

    // get number of pages
    int numPagesIn = inputPdfReader.NumberOfPages;
    int numPagesOut = destinationPdfReader.NumberOfPages;

    int max = numPagesIn;

    // Process max number of pages
    if (max<numPagesOut)
    {
        throw new Exception("Impossible - different number of pages");
    }
    int i = 0;

    // Process Pdf pages
    while (i < max)
    {
        // Import pages from corresponding reader
        PdfImportedPage pageIn = writer.inputPdfReader(reader, i);
        PdfImportedPage pageOut = writer.destinationPdfReader(reader, i);

        // Get named destinations (annotations
        List<Annotations> toBeAdded = ParseInAndOutAndGetAnnotations(pageIn, pageOut);

        // add your annotations
        foreach (Annotation anno in toBeAdded) pageOut.Add(anno);

        // Add processed page to output PDFWriter
        outdoc.Add(pageOut);
    }

    // PDF creation finished
    outdoc.Close();

    // your new destination stream is processedPdf
    return processedPdf;
}

The implementation of ParseInAndOutAndGetAnnotations(pageIn, pageOut) needs to reflect your annotations.

Here is a good example with annotations: http://www.java2s.com/Open-Source/Java-Document/PDF/pdf-itext/com/lowagie/text/pdf/internal/PdfAnnotationsImp.java.htm

Andreas Rehm
I tried to use ITextSharp, but I didn't find any good sample for copying annotations.
Lanfear
I've added a small sample - you need to adapt it - it's just adding an annotation. It is easy to parse through each page with iTextSharp - that's what you need to do.
Andreas Rehm
I've change my example - you just need to implement your annotation comparer depending on the type of annotations - e.g. http://www.java2s.com/Open-Source/Java-Document/PDF/pdf-itext/com/lowagie/text/pdf/internal/PdfAnnotationsImp.java.htm
Andreas Rehm
Thank you very much for your help. iTextSharp is great library, but sometimes not enough examples.
Lanfear
A: 

i'm really interested on the implementation of ParseInAndOutAndGetAnnotations() to get annotations. Is it somewhere? or a nice example to read annotation with iTextSharp? Thanks a lot

jesus