PDF: Edit content of PDF page programmatically
In This Topic
The SDK has algorithm that divides all content (graphics, images, forms, text, clip region) on PDF page into graphical figures.
The
PdfContentGraphicsFigureEditor class allows to edit graphical figures of PDF page programmatically.
The editor allows to do the following operations:
Here is C#/VB.NET code that demonstrates how to change the location of specified resource on PDF page:
/// <summary>
/// Sets the resource location on PDF page.
/// </summary>
/// <param name="inPdfFilename">The filename of input PDF document.</param>
/// <param name="outPdfFilename">The filename of output PDF document.</param>
/// <param name="pageIndex">Index of the PDF page.</param>
/// <param name="resourceIndex">Index of the resource on PDF page.</param>
/// <param name="newLocation">The new location in PDF page space.</param>
public static void SetResourceLocation(string inPdfFilename, string outPdfFilename, int pageIndex, int resourceIndex, System.Drawing.PointF newLocation)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(inPdfFilename))
{
// create PdfContentGraphicsFigureEditor on specified PDF page
using (Vintasoft.Imaging.Pdf.Content.PdfContentGraphicsFigureEditor contentEditor =
new Vintasoft.Imaging.Pdf.Content.PdfContentGraphicsFigureEditor(document.Pages[pageIndex], false))
{
int currentResourceIndex = 0;
// for each graphics figure on PDF page
foreach (Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.ContentStreamGraphicsFigure figure in contentEditor.GraphicsFigures)
{
// if graphics figure has resource
if (figure.Resource != null)
{
// if this is graphics figure that should be changed
if (currentResourceIndex == resourceIndex)
{
// get the bounding box of graphics figure
System.Drawing.RectangleF resourceBBox = figure.GetRegion().Bounds;
// create transformation that changes coordinates of object
Vintasoft.Imaging.AffineMatrix transform = Vintasoft.Imaging.AffineMatrix.CreateTranslation(newLocation.X - resourceBBox.X, newLocation.Y - resourceBBox.Y);
// apply transformation to the graphics figure
contentEditor.Transform(figure, transform);
break;
}
currentResourceIndex++;
}
}
if (currentResourceIndex != resourceIndex)
throw new System.IndexOutOfRangeException(string.Format("Resource with index {0} was not found", resourceIndex));
}
// save PDF document to a new file
document.Save(outPdfFilename);
}
}
''' <summary>
''' Sets the resource location on PDF page.
''' </summary>
''' <param name="inPdfFilename">The filename of input PDF document.</param>
''' <param name="outPdfFilename">The filename of output PDF document.</param>
''' <param name="pageIndex">Index of the PDF page.</param>
''' <param name="resourceIndex">Index of the resource on PDF page.</param>
''' <param name="newLocation">The new location in PDF page space.</param>
Public Shared Sub SetResourceLocation(inPdfFilename As String, outPdfFilename As String, pageIndex As Integer, resourceIndex As Integer, newLocation As System.Drawing.PointF)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(inPdfFilename)
' create PdfContentGraphicsFigureEditor on specified PDF page
Using contentEditor As New Vintasoft.Imaging.Pdf.Content.PdfContentGraphicsFigureEditor(document.Pages(pageIndex), False)
Dim currentResourceIndex As Integer = 0
' for each graphics figure on PDF page
For Each figure As Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.ContentStreamGraphicsFigure In contentEditor.GraphicsFigures
' if graphics figure has resource
If figure.Resource IsNot Nothing Then
' if this is graphics figure that should be changed
If currentResourceIndex = resourceIndex Then
' get the bounding box of graphics figure
Dim resourceBBox As System.Drawing.RectangleF = figure.GetRegion().Bounds
' create transformation that changes coordinates of object
Dim transform As Vintasoft.Imaging.AffineMatrix = Vintasoft.Imaging.AffineMatrix.CreateTranslation(newLocation.X - resourceBBox.X, newLocation.Y - resourceBBox.Y)
' apply transformation to the graphics figure
contentEditor.Transform(figure, transform)
Exit For
End If
currentResourceIndex += 1
End If
Next
If currentResourceIndex <> resourceIndex Then
Throw New System.IndexOutOfRangeException(String.Format("Resource with index {0} was not found", resourceIndex))
End If
End Using
' save PDF document to a new file
document.Save(outPdfFilename)
End Using
End Sub