Extract Vector Data from a PDF file using C#
Contents
[
Hide
]
Access to Vector Data from PDF document
Since the the 24.2 release, Aspose.PDF for .NET library allows vector data extraction from a PDF file. The next code snippet creates a new Document object using some input data, initializes a ‘GraphicsAbsorber’(the GraphicsAbsorber returns the vector data) to handle graphic elements, and then visits the second page of the document to extract and analyze these elements. It retrieves various properties of the second graphic element, such as its associated operators, rectangle, and position.
// Creates a new Document object using the provided input data.
var doc = new Document(input);
// Instantiates a new GraphicsAbsorber object to process graphic elements.
var grAbsorber = new GraphicsAbsorber();
// Visits the second page of the document to extract graphic elements.
grAbsorber.Visit(doc.Pages[1]);
// Retrieves the list of graphic elements from the GraphicsAbsorber.
var elements = grAbsorber.Elements;
// Accesses the operators associated with the second graphic element.
var operations = elements[1].Operators;
// Retrieves the rectangle associated with the second graphic element.
var rectangle = elements[1].Rectangle;
// Gets the position of the second graphic element.
var position = elements[1].Position;
Extract Vector Data from PDF document
For extraction of Vector Data from PDF, we can use SVG extractor:
var doc = new Document(input);
doc.Pages[1].TrySaveVectorGraphics(outputSvg);
Extract all subpaths to images separately
SvgExtractionOptions options = new SvgExtractionOptions
{
ExtractEverySubPathToSvg = true
};
SvgExtractor extractor = new SvgExtractor(options);
extractor.Extract(page, svgDirPath);
Extract list of elements to single image
List<GraphicElement> elements = new List<GraphicElement>();
// Fill elements list needed graphic elements.
SvgExtractor svgExtractor = new SvgExtractor();
svgExtractor.Extract(elements, page, Path.Combine(svgDirPath, "1.svg"));
Extract single element
GraphicsAbsorber graphicsAbsorber = new GraphicsAbsorber();
graphicsAbsorber.Visit(page);
XFormPlacement xFormPlacement = graphicsAbsorber.Elements[1] as XFormPlacement;
xFormPlacement.Elements[2].SaveToSvg(page, Path.Combine(svgDirPath,"1.svg"));