Using our PdfAnnotationDataExporter allows you to export your annotations over your pdf without altering the underlying pdf. This allows you to add annotations over an already searchable pdf and to leave a non-image pdf at its already small file size. Here is a snippit of code that uses the exporter on a pdf:
C#
//Best practices is to encode our PDF first and then export the annos over the top.
//If you haven't created your PDF yet, do so now. If you're annotating over an existing PDF, acquire a Stream to it.
Stream saveDoc = Stream.Null; //This will be our stream that contains the PDF. Can be any kind of seekable stream.
//Just make sure you don't leave it as Stream.Null.
saveDoc.Position = 0;
//Get our generated pagecount. Note: we're resetting our filestream position after each access.
int pageCount = RegisteredDecoders.GetImageInfo(saveDoc).FrameCount;
saveDoc.Position = 0;
SizeF[] sizes = new SizeF[pageCount];
Dpi[] dpis = new Dpi[pageCount];
//For each page, get its size and resolution and put aside for later.
//It is absolutely possible to have a different dpi or size for each page.
ImageInfo currPageInfo;
for (int i = 0; i < pageCount; i++)
{
currPageInfo = RegisteredDecoders.GetImageInfo(saveDoc, i);
sizes[i] = currPageInfo.Size;
dpis[i] = currPageInfo.Resolution;
saveDoc.Position = 0;
}
Atalasoft.Annotate.AnnotationDataCollection data = new Atalasoft.Annotate.AnnotationDataCollection();
//Stuff all our layer data in a collection. Note, you will need to change this to reflect whatever annotation viewer you are using.
foreach (Atalasoft.Annotate.UI.LayerAnnotation layer in annotateViewer1.Annotations.Layers)
{
data.Add(layer.Data);
}
Atalasoft.Annotate.Exporters.PdfAnnotationDataExporter pae = new Atalasoft.Annotate.Exporters.PdfAnnotationDataExporter();
//Make our exporter and export these annotations.
//Please read this! You have the choice to export over the same stream that your Pdf is in, simply use ExportOver
pae.ExportOver(saveDoc, sizes, Atalasoft.Annotate.AnnotationUnit.Pixel, dpis, data);
//Or you can use Export and save the annotated Pdf to a separate stream!
MemoryStream pdfOut = new MemoryStream();
pae.Export(saveDoc, pdfOut, sizes, Atalasoft.Annotate.AnnotationUnit.Pixel, dpis, data);
VB.NET
'Best practices is to encode our PDF first and then export the annos over the top.
'If you haven't created your PDF yet, do so now.
Dim saveDoc As Stream = Stream.Null
'This will be our stream that contains the PDF. Can be any kind of seekable stream.
'Just make sure you don't leave it as Stream.Null.
saveDoc.Position = 0
'Get our generated pagecount. Note: we're resetting our filestream position after each access.
Dim pageCount As Integer = Atalasoft.Imaging.Codec.RegisteredDecoders.GetImageInfo(saveDoc).FrameCount
saveDoc.Position = 0
Dim sizes As SizeF() = New SizeF(pageCount - 1) {}
Dim dpis As Dpi() = New Dpi(pageCount - 1) {}
'For each page, get its size and resolution and put aside for later.
'It is absolutely possible to have a different dpi or size for each page.
Dim currPageInfo As Atalasoft.Imaging.Codec.ImageInfo
For i As Integer = 0 To pageCount - 1
currPageInfo = Atalasoft.Imaging.Codec.RegisteredDecoders.GetImageInfo(saveDoc, i)
sizes(i) = currPageInfo.Size
dpis(i) = currPageInfo.Resolution
saveDoc.Position = 0
Next
Dim data As New Atalasoft.Annotate.AnnotationDataCollection()
'Stuff all our layer data in a collection. Note, you will need to change this to reflect whatever annotation viewer you are using.
For Each layer As Atalasoft.Annotate.UI.LayerAnnotation In annotateViewer1.Annotations.Layers
data.Add(layer.Data)
Next
Dim pae As New Atalasoft.Annotate.Exporters.PdfAnnotationDataExporter()
'Make our exporter and export these annotations.
'Please read this! You have the choice to export over the same stream that your Pdf is in, simply use ExportOver
pae.ExportOver(saveDoc, sizes, Atalasoft.Annotate.AnnotationUnit.Pixel, dpis, data)
'Or you can use Export and save the annotated Pdf to a separate stream!
Dim pdfOut As New MemoryStream()
pae.Export(saveDoc, pdfOut, sizes, Atalasoft.Annotate.AnnotationUnit.Pixel, dpis, data)
Original Article:
Q10232 - HOWTO: Add annotations to a Pdf without rasterizing