LEADTOOLS Support
Document
Document SDK Questions
How to use Document Writer to Save Document with Annotations
#1
Posted
:
Tuesday, July 21, 2020 3:22:42 AM(UTC)
Groups: Registered
Posts: 60
I have a PDF Document with Annotations and I want to Save the document with annotations using DocumentWriter. Please assist on how to achieve this. Here is my code.
Code:
var memoryStream = new MemoryStream();
var docWriter = new DocumentWriter();
var pdfOptions = docWriter.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions;
pdfOptions.DocumentType = PdfDocumentType.PdfA;
docWriter.SetOptions(DocumentFormat.Pdf, pdfOptions);
docWriter.BeginDocument(memoryStream, DocumentFormat.Pdf);
//copy annotation and resize
using RasterCodecs doccodecs = new RasterCodecs();
doccodecs.Options.Load.Name = $"anything.pdf";
var docoriginalImage = doccodecs.Load(files[0]);
var docinfo = doccodecs.GetInformation(files[0], true);
RasterImage docfullFileImage = null;
var docannCodecs = new AnnCodecs();
var docannContainer = new AnnContainer();
var docrenderingEngine = new AnnDrawRenderingEngine();
for (int j = 1; j <= docoriginalImage.PageCount; j++)
{
var page = new DocumentWriterSvgPage();
//page.SvgDocument = doccodecs.LoadSvg(files[0], j, null);
//docWriter.AddPage(page);
//page.SvgDocument.Dispose();
RasterImage image = doccodecs.Load(files[0], docinfo.BitsPerPixel, CodecsLoadByteOrder.BgrOrGrayOrRomm, j, j);
docannContainer = docannCodecs.Load(annotationFile, j);
var height = image.ImageHeight * (720 / (double)image.YResolution);
var width = image.ImageWidth * (720 / (double)image.XResolution);
docannContainer.Resize(new LeadSizeD(width, height), AnnResizeContainerFlags.ResizeObjects, AnnResizeMode.Stretch);
docannCodecs.Save(@$"{currentPath}\4-45-resized.xml", docannContainer, AnnFormat.Annotations, 1);
if (docfullFileImage is null)
{
var temp = docannContainer is null ? image : docrenderingEngine.RenderOnImage(docannContainer, image);
page.Image = temp;
docfullFileImage = temp;
}
else
{
var temp = docannContainer is null ? image : docrenderingEngine.RenderOnImage(docannContainer, image);
page.Image = temp;
docfullFileImage.AddPage(temp);
}
docWriter.AddPage(page);
page.Image.Dispose();
}
docWriter.EndDocument();
// save the document writer stream
using var fileStream = File.Create(@$"{currentPath}\docwriterresize.pdf");
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
// save using raster codecs
doccodecs.Save(docfullFileImage ?? docoriginalImage, @$"{currentPath}\resize.pdf", RasterImageFormat.RasPdfLzw, 0, 1, docoriginalImage.PageCount, 1, CodecsSavePageMode.Append);
docWriter.AddPage(page) throws File Not Found Exception
I have a file and annotation xml. I'm trying to save file with annotation using document writer as document writer produces files with less size compared to files saved using raster codecs.
Please correct me if I'm wrong. Attached the input pdf and annotation xml file for your reference.
#2
Posted
:
Wednesday, July 22, 2020 4:36:14 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 105
Was thanked: 3 time(s) in 3 post(s)
Hello Abdul,
Thank you for providing the sample files and code. After reviewing everything it looks like the exception is being thrown because the SvgDocument is null so the DocumentWriterSvgPage's SvgDocument, Width, and Height properties are all null. For your use-case since you are rendering annotations on a RasterImage and then saving out as a PDF with the DocumentWriter, you will need to use DocumentWriterRasterPage.
https://www.leadtools.co...entwriterrasterpage.htmlYour code should look more like the code below:
Code:
for (int i = 1; i <= totalPages; i++)
{
var page = new DocumentWriterRasterPage();
RasterImage image = codecs.Load(inputFileName, info.BitsPerPixel, CodecsLoadByteOrder.BgrOrGrayOrRomm, i, i);
docannContainer = docannCodecs.Load(annFile, i);
RasterImage tempImage = docrenderingEngine.RenderOnImage(docannContainer, image);
page.Image = tempImage;
docWriter.AddPage(page);
page.Image.Dispose();
}
}
docWriter.EndDocument();
// save the document writer stream
using var fileStream = File.Create(@"C:\Temp\docwriterresize.pdf");
memoryStream.Seek(0, SeekOrigin.Begin);
memoryStream.CopyTo(fileStream);
If you have any questions about the above process please feel free to reach back out to me.
Thanks
Matt Bresson
Developer Support Engineer
LEAD Technologies, Inc.
LEADTOOLS Support
Document
Document SDK Questions
How to use Document Writer to Save Document with Annotations
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.