Класс OfficeDocumentFigure
В этом разделе
Представляет рисунок документа Office, который рисует DOCX документ.
Объектная модель
Синтаксис
Пример
Вот пример, который показывает, как аннотировать PDF файл (пример использует шаблон документаChartTemplate.docx):
''' <summary>
''' Add chart to a PDF document.
''' </summary>
''' <param name="filePath">The PDF file path.</param>
''' <param name="chartRectInPoint">The chart rect on PDF page in points (1/72 inch).</param>
Public Shared Sub AddChartToPdfFile(filePath As String, chartRectInPoint As System.Drawing.RectangleF)
' 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 Office document figure
Using officeDocumentFigure As New Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.OfficeDocumentFigure()
' update figure properties
officeDocumentFigure.SetDocumentFilename(templateDocDocumentPath)
officeDocumentFigure.Location = chartRectInPoint.Location
officeDocumentFigure.Size = chartRectInPoint.Size
officeDocumentFigure.UseGraphicObjectRelativeSize = True
' fill the document template with real data
FillDocumentTemplate(officeDocumentFigure)
' create PDF graphics
Using pdfGraphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = pdfPage.GetGraphics()
' draw chart on PDF page
officeDocumentFigure.DrawFigure(pdfGraphics)
End Using
' save changes in PDF document
pdfDocument.SaveChanges()
End Using
End Using
End Sub
''' <summary>
''' Fills the template DOCX document with real data.
''' </summary>
''' <param name="figure">The document figure.</param>
Private Shared Sub FillDocumentTemplate(figure As Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.OfficeDocumentFigure)
' create DOCX document editor
Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(figure.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 (disposed after use)
Dim changedDocumentStream As New System.IO.MemoryStream()
' save template changes
editor.Save(changedDocumentStream)
' change document stream
figure.SetDocumentStream(changedDocumentStream, True)
End Using
End Sub
/// <summary>
/// Add chart to a PDF document.
/// </summary>
/// <param name="filePath">The PDF file path.</param>
/// <param name="chartRectInPoint">The chart rect on PDF page in points (1/72 inch).</param>
public static void AddChartToPdfFile(string filePath, System.Drawing.RectangleF chartRectInPoint)
{
// 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 Office document figure
using (Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.OfficeDocumentFigure officeDocumentFigure = new Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.OfficeDocumentFigure())
{
// update figure properties
officeDocumentFigure.SetDocumentFilename(templateDocDocumentPath);
officeDocumentFigure.Location = chartRectInPoint.Location;
officeDocumentFigure.Size = chartRectInPoint.Size;
officeDocumentFigure.UseGraphicObjectRelativeSize = true;
// fill the document template with real data
FillDocumentTemplate(officeDocumentFigure);
// create PDF graphics
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics pdfGraphics = pdfPage.GetGraphics())
{
// draw chart on PDF page
officeDocumentFigure.DrawFigure(pdfGraphics);
}
// save changes in PDF document
pdfDocument.SaveChanges();
}
}
}
/// <summary>
/// Fills the template DOCX document with real data.
/// </summary>
/// <param name="figure">The document figure.</param>
private static void FillDocumentTemplate(Vintasoft.Imaging.Pdf.Drawing.GraphicsFigures.OfficeDocumentFigure figure)
{
// create DOCX document editor
using (Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor editor =
new Vintasoft.Imaging.Office.OpenXml.Editor.Docx.DocxDocumentEditor(figure.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 (disposed after use)
System.IO.MemoryStream changedDocumentStream = new System.IO.MemoryStream();
// save template changes
editor.Save(changedDocumentStream);
// change document stream
figure.SetDocumentStream(changedDocumentStream, true);
}
}
Иерархия наследования
Требования
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5
Смотрите также