Класс PdfOfficeDocumentAnnotation
В этом разделе
Представляет аннотацию VintaSoft PDF, в которой хранится офисный документ.
Объектная модель
Синтаксис
Пример
Вот пример, показывающий, как добавить аннотацию с диаграммой в PDF документ (в примере используется документ-шаблонChartTemplate.docx):
''' <summary>
''' Adds Office annotation with chart to a PDF document.
''' </summary>
''' <param name="filePath">The PDF file path.</param>
''' <param name="annotationRectInPoint">The annotation area on PDF page in points (1/72 inch).</param>
Public Shared Sub AnnotatePdfFile(filePath As String, annotationRectInPoint As System.Drawing.RectangleF)
' register appearance generator for Office annotation
Vintasoft.Imaging.Pdf.Tree.Annotations.PdfOfficeDocumentAnnotationAppearanceGenerator.Register()
' path to a template DOCX document with chart
Dim templateDocDocumentPath As String = "ChartTemplate.docx"
' open PDF document
Using pdfDocument As New Vintasoft.Imaging.Pdf.PdfDocument(filePath, False)
' get PDF document page
Dim pdfPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = pdfDocument.Pages(0)
' create PDF Office document annotation data
Dim annotationData As New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfOfficeDocumentAnnotation(pdfPage)
' set DOCX document for PDF Office document annotation data
annotationData.SetDocumentFilename(templateDocDocumentPath)
' set the annotation position
annotationData.Rectangle = annotationRectInPoint
annotationData.UseGraphicObjectReleativeSize = True
' add annotation to the PDF page
pdfPage.Annotations.Add(annotationData)
' fill template document with real data
FillDocumentTemplate(annotationData)
' create appearance for changed annotation
annotationData.UpdateAppearance()
' save changes in PDF document
pdfDocument.SaveChanges()
End Using
End Sub
''' <summary>
''' Fills the template DOCX document with real data.
''' </summary>
''' <param name="data">The annotation data.</param>
Private Shared Sub FillDocumentTemplate(data As Vintasoft.Imaging.Pdf.Tree.Annotations.PdfOfficeDocumentAnnotation)
' get DOCX document stream
Using documentStream As System.IO.MemoryStream = data.OfficeDocument.GetAsMemoryStream()
' create DOCX document editor
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(documentStream)
' get DOCX document body
Dim documentBody As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement = editor.Body
' change TITLE
' find title
Dim titleText As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent = documentBody.FindText("[TITLE]")
' change the title text
titleText.Text = "Sales"
' highlight the title in bold
titleText.SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.BoldText)
' change DATE
' find date
Dim dateText As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent = documentBody.FindText("[DATE]")
' change the date text
dateText.Text = System.DateTime.Now.ToString("yyyy")
' create the date text properties
Dim dateTextProperties As New Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties()
' change the date text color
dateTextProperties.Color = System.Drawing.Color.Blue
' change the date text size
dateTextProperties.FontSize = 24
' change the date text properties
dateText.SetTextProperties(dateTextProperties)
' change CHART
' create the chart data
Dim chartData As System.Nullable(Of Double)(,) = New System.Nullable(Of Double)(,) {{55, 32, 23}, {84, 48, 33}, {72, 53, 86}, {34, 82, 11}}
' change the chart data
editor.Charts(0).ChartData.SetData(chartData)
' create stream for changed document
Using changedDocumentStream As New System.IO.MemoryStream()
' save template changes
editor.Save(changedDocumentStream)
' change document stream
data.SetDocumentStream(changedDocumentStream)
End Using
End Using
End Using
End Sub
/// <summary>
/// Adds Office annotation with chart to a PDF document.
/// </summary>
/// <param name="filePath">The PDF file path.</param>
/// <param name="annotationRectInPoint">The annotation area on PDF page in points (1/72 inch).</param>
public static void AnnotatePdfFile(string filePath, System.Drawing.RectangleF annotationRectInPoint)
{
// register appearance generator for Office annotation
Vintasoft.Imaging.Pdf.Tree.Annotations.PdfOfficeDocumentAnnotationAppearanceGenerator.Register();
// path to a template DOCX document with chart
string templateDocDocumentPath = "ChartTemplate.docx";
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument pdfDocument = new Vintasoft.Imaging.Pdf.PdfDocument(filePath, false))
{
// get PDF document page
Vintasoft.Imaging.Pdf.Tree.PdfPage pdfPage = pdfDocument.Pages[0];
// create PDF Office document annotation data
Vintasoft.Imaging.Pdf.Tree.Annotations.PdfOfficeDocumentAnnotation annotationData =
new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfOfficeDocumentAnnotation(pdfPage);
// set DOCX document for PDF Office document annotation data
annotationData.SetDocumentFilename(templateDocDocumentPath);
// set the annotation position
annotationData.Rectangle = annotationRectInPoint;
annotationData.UseGraphicObjectReleativeSize = true;
// add annotation to the PDF page
pdfPage.Annotations.Add(annotationData);
// fill template document with real data
FillDocumentTemplate(annotationData);
// create appearance for changed annotation
annotationData.UpdateAppearance();
// save changes in PDF document
pdfDocument.SaveChanges();
}
}
/// <summary>
/// Fills the template DOCX document with real data.
/// </summary>
/// <param name="data">The annotation data.</param>
private static void FillDocumentTemplate(Vintasoft.Imaging.Pdf.Tree.Annotations.PdfOfficeDocumentAnnotation data)
{
// get DOCX document stream
using (System.IO.MemoryStream documentStream = data.OfficeDocument.GetAsMemoryStream())
{
// create DOCX document editor
using (Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(documentStream))
{
// get DOCX document body
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement documentBody = editor.Body;
// change TITLE
// find title
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent titleText = documentBody.FindText("[TITLE]");
// change the title text
titleText.Text = "Sales";
// highlight the title in bold
titleText.SetTextProperties(Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties.BoldText);
// change DATE
// find date
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent dateText = documentBody.FindText("[DATE]");
// change the date text
dateText.Text = System.DateTime.Now.ToString("yyyy");
// create the date text properties
Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties dateTextProperties =
new Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextProperties();
// change the date text color
dateTextProperties.Color = System.Drawing.Color.Blue;
// change the date text size
dateTextProperties.FontSize = 24;
// change the date text properties
dateText.SetTextProperties(dateTextProperties);
// change CHART
// create the chart data
double?[,] chartData = new double?[,] {
{ 55, 32, 23 },
{ 84, 48, 33 },
{ 72, 53, 86 },
{ 34, 82, 11 } };
// change the chart data
editor.Charts[0].ChartData.SetData(chartData);
// create stream for changed document
using (System.IO.MemoryStream changedDocumentStream = new System.IO.MemoryStream())
{
// save template changes
editor.Save(changedDocumentStream);
// change document stream
data.SetDocumentStream(changedDocumentStream);
}
}
}
}
Иерархия наследования
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также