本文整理汇总了C#中DocumentFormat.OpenXml.Packaging.MainDocumentPart.GetIdOfPart方法的典型用法代码示例。如果您正苦于以下问题:C# MainDocumentPart.GetIdOfPart方法的具体用法?C# MainDocumentPart.GetIdOfPart怎么用?C# MainDocumentPart.GetIdOfPart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DocumentFormat.OpenXml.Packaging.MainDocumentPart
的用法示例。
在下文中一共展示了MainDocumentPart.GetIdOfPart方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InsertAfter
public override void InsertAfter(OpenXmlElement openXmlNode, MainDocumentPart mainDocumentPart)
{
_mainDocumentPart = mainDocumentPart;
var imagePart = CreateImagePart();
openXmlNode.InsertAfterSelf(CreateImageElement(mainDocumentPart.GetIdOfPart(imagePart), Path.GetFileName(_content)));
}
示例2: AppendTo
public override void AppendTo(OpenXmlElement openXmlNode, MainDocumentPart mainDocumentPart)
{
_mainDocumentPart = mainDocumentPart;
var imagePart = CreateImagePart();
openXmlNode.Append(CreateImageElement(mainDocumentPart.GetIdOfPart(imagePart), Path.GetFileName(_content)));
}
示例3: ClearHeaderFooter
internal static void ClearHeaderFooter(MainDocumentPart mdp)
{
mdp.DeleteParts(mdp.HeaderParts);
mdp.DeleteParts(mdp.FooterParts);
var hp = mdp.AddNewPart<HeaderPart>();
var fp = mdp.AddNewPart<FooterPart>();
hp.Header = new Header();
fp.Footer = new Footer();
foreach (var sps in mdp.Document.Body.Elements<SectionProperties>())
{
sps.RemoveAllChildren<HeaderReference>();
sps.RemoveAllChildren<FooterReference>();
sps.PrependChild(new HeaderReference { Id = mdp.GetIdOfPart(hp) });
sps.PrependChild(new FooterReference { Id = mdp.GetIdOfPart(fp) });
}
mdp.Document.Save();
}
示例4: insertChart
private String insertChart(MainDocumentPart mainPart, wp.Paragraph par, String templateName, int dimx, int dimy)
{
String relId;
// vai buscar o gráfico e coloca-o no local com a dimensão definida HARCODED
wp.Paragraph chartP = new wp.Paragraph();
wp.Run chartR = new wp.Run();
chartP.Append(chartR);
wp.Drawing drawing = new wp.Drawing();
chartR.Append(drawing);
clearParagraphText(par);
//Open Excel spreadsheet
using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(templateName, true))
{
//Get all the appropriate parts
// assume que há apenas um gráfico por ficheiro template
// isto deveria evoluir para um ficheiro apenas
WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById("rId1");
DrawingsPart drawingPart = worksheetPart.DrawingsPart;
ChartPart chartPart = (ChartPart)drawingPart.GetPartById("rId1");
//Clone the chart part and add it to my Word document
ChartPart importedChartPart = mainPart.AddPart<ChartPart>(chartPart);
relId = mainPart.GetIdOfPart(importedChartPart);
//The frame element contains information for the chart
ssd.GraphicFrame frame = drawingPart.WorksheetDrawing.Descendants<ssd.GraphicFrame>().First();
string chartName = frame.NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name;
//Clone this node so we can add it to my Word document
Graphic clonedGraphic = (Graphic)frame.Graphic.CloneNode(true);
// clonedGraphic.GraphicData
ChartReference c = clonedGraphic.GraphicData.GetFirstChild<ChartReference>();
c.Id = relId;
//Give the chart a unique id and name
wpd.DocProperties docPr = new wpd.DocProperties();
docPr.Name = chartName;
docPr.Id = GetMaxDocPrId(mainPart) + 1;
//add the chart data to the inline drawing object
// wpd.Inline inline = new wpd.Inline(new wpd.Extent() { Cx = 5372100, Cy = 1914525 });
wpd.Inline inline;
if (dimx == 0 && dimy == 0)
inline = new wpd.Inline();
else
inline = new wpd.Inline(new wpd.Extent() { Cx = dimx, Cy = dimy });
inline.Append(docPr, clonedGraphic);
drawing.Append(inline);
}
// JUNTA O GRÁFICO
par.Append(chartP);
// retorna o ID do gráfico que necessita update
return relId;
}
示例5: InitEmptyHeaderOrFooter
private static void InitEmptyHeaderOrFooter(MainDocumentPart mainDocPart, XElement sect, XName referenceXName, string toType)
{
XDocument xDoc = null;
if (referenceXName == W.headerReference)
{
xDoc = XDocument.Parse(
@"<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<w:hdr xmlns:wpc='http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas'
xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:r='http://schemas.openxmlformats.org/officeDocument/2006/relationships'
xmlns:m='http://schemas.openxmlformats.org/officeDocument/2006/math'
xmlns:v='urn:schemas-microsoft-com:vml'
xmlns:wp14='http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing'
xmlns:wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'
xmlns:w10='urn:schemas-microsoft-com:office:word'
xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'
xmlns:w14='http://schemas.microsoft.com/office/word/2010/wordml'
xmlns:w15='http://schemas.microsoft.com/office/word/2012/wordml'
xmlns:wpg='http://schemas.microsoft.com/office/word/2010/wordprocessingGroup'
xmlns:wpi='http://schemas.microsoft.com/office/word/2010/wordprocessingInk'
xmlns:wne='http://schemas.microsoft.com/office/word/2006/wordml'
xmlns:wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
mc:Ignorable='w14 w15 wp14'>
<w:p>
<w:pPr>
<w:pStyle w:val='Header' />
</w:pPr>
<w:r>
<w:t></w:t>
</w:r>
</w:p>
</w:hdr>");
var newHeaderPart = mainDocPart.AddNewPart<HeaderPart>();
newHeaderPart.PutXDocument(xDoc);
var referenceToAdd = new XElement(W.headerReference,
new XAttribute(W.type, toType),
new XAttribute(R.id, mainDocPart.GetIdOfPart(newHeaderPart)));
sect.AddFirst(referenceToAdd);
}
else
{
xDoc = XDocument.Parse(@"<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<w:ftr xmlns:wpc='http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas'
xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:r='http://schemas.openxmlformats.org/officeDocument/2006/relationships'
xmlns:m='http://schemas.openxmlformats.org/officeDocument/2006/math'
xmlns:v='urn:schemas-microsoft-com:vml'
xmlns:wp14='http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing'
xmlns:wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing'
xmlns:w10='urn:schemas-microsoft-com:office:word'
xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'
xmlns:w14='http://schemas.microsoft.com/office/word/2010/wordml'
xmlns:w15='http://schemas.microsoft.com/office/word/2012/wordml'
xmlns:wpg='http://schemas.microsoft.com/office/word/2010/wordprocessingGroup'
xmlns:wpi='http://schemas.microsoft.com/office/word/2010/wordprocessingInk'
xmlns:wne='http://schemas.microsoft.com/office/word/2006/wordml'
xmlns:wps='http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
mc:Ignorable='w14 w15 wp14'>
<w:p>
<w:pPr>
<w:pStyle w:val='Footer' />
</w:pPr>
<w:r>
<w:t></w:t>
</w:r>
</w:p>
</w:ftr>");
var newFooterPart = mainDocPart.AddNewPart<FooterPart>();
newFooterPart.PutXDocument(xDoc);
var referenceToAdd = new XElement(W.footerReference,
new XAttribute(W.type, toType),
new XAttribute(R.id, mainDocPart.GetIdOfPart(newFooterPart)));
sect.AddFirst(referenceToAdd);
}
}
示例6: CreateFooter
public static string CreateFooter(MainDocumentPart mainPart, OpenXmlCompositeElement element)
{
FooterPart footerPart = mainPart.AddNewPart<FooterPart>();
Footer footer = new Footer();
AddHeaderFooterNamespaceDeclaration(footer);
if (element != null)
footer.AppendChild(element);
footerPart.Footer = footer;
return mainPart.GetIdOfPart(footerPart);
}
示例7: CreateHeader
public static string CreateHeader(MainDocumentPart mainPart, OpenXmlCompositeElement element)
{
HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>();
Header header = new Header();
AddHeaderFooterNamespaceDeclaration(header);
if (element != null)
header.AppendChild(element);
headerPart.Header = header;
return mainPart.GetIdOfPart(headerPart);
}
示例8: AddImageParagraph
private static Paragraph AddImageParagraph(MainDocumentPart mainDocumentPart, List<TextPositon> listTextPositon)
{
ImagePart imagePart = mainDocumentPart.AddImagePart(ImagePartType.Jpeg);
imageRelationshipID = mainDocumentPart.GetIdOfPart(imagePart);
using (Stream imgStream = imagePart.GetStream())
{
System.Drawing.Bitmap logo = new System.Drawing.Bitmap(string.Format(@"{0}\Resources\Wedding_EnclosureCards.jpg", Environment.CurrentDirectory));
foreach (var item in listTextPositon)
{
System.Drawing.Font font = new System.Drawing.Font("Thaoma", 10, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(logo);
float actualWidth = g.MeasureString(item.Text, font).Width;
System.Drawing.RectangleF rectf = new System.Drawing.RectangleF((330 - actualWidth) / 2 + item.X, item.Y, 330, 50);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.DrawString(item.Text, font, System.Drawing.Brushes.Gray, rectf);
g.Flush();
}
logo.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
Paragraph p = new Paragraph();
Run r = new Run();
Drawing drawing = BuildImage(imageRelationshipID, "sw.gif", 600, 850);
r.Append(drawing);
p.Append(r);
return p;
}
示例9: ImportSmartArtFromPowerPoint
void ImportSmartArtFromPowerPoint(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename)
{
string docLayoutPartId = "";
string docDataPartId = "";
string docColorsPartId = "";
string docStylePartId = "";
byte[] byteArray = filename.OpenBinary();
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
using (PresentationDocument myPres = PresentationDocument.Open(mem, true))
{
PresentationPart presPart = myPres.PresentationPart;
// Get the slide that contains the SmartArt graphic.
SlidePart slide = (SlidePart)presPart.GetPartById("rId3");
// Get all the appropriate parts associated with the SmartArt.
DiagramLayoutDefinitionPart layoutPart =
slide.DiagramLayoutDefinitionParts.First();
DiagramDataPart dataPart = slide.DiagramDataParts.First();
DiagramColorsPart colorsPart = slide.DiagramColorsParts.First();
DiagramStylePart stylePart = slide.DiagramStyleParts.First();
// Get some of the appropriate properties off the SmartArt graphic.
PPT.GraphicFrame graphicFrame =
slide.Slide.Descendants<PPT.GraphicFrame>().First();
PPT.NonVisualDrawingProperties drawingPr = graphicFrame
.Descendants<PPT.NonVisualDrawingProperties>().First();
Draw.Extents extents =
graphicFrame.Descendants<Draw.Extents>().First();
// Import SmartArt into the Word document.
// Add the SmartArt parts to the Word document.
DiagramLayoutDefinitionPart docLayoutPart =
mainPart.AddPart<DiagramLayoutDefinitionPart>(layoutPart);
DiagramDataPart docDataPart =
mainPart.AddPart<DiagramDataPart>(dataPart);
DiagramColorsPart docColorsPart =
mainPart.AddPart<DiagramColorsPart>(colorsPart);
DiagramStylePart docStylePart =
mainPart.AddPart<DiagramStylePart>(stylePart);
// Get all the relationship ids of the added parts.
docLayoutPartId = mainPart.GetIdOfPart(docLayoutPart);
docDataPartId = mainPart.GetIdOfPart(docDataPart);
docColorsPartId = mainPart.GetIdOfPart(docColorsPart);
docStylePartId = mainPart.GetIdOfPart(docStylePart);
// Use the document reflector to figure out how to add a SmartArt
// graphic to Word.
// Change attribute values based on specifics related to the SmartArt.
Word.Paragraph p = new Word.Paragraph(
new Word.Run(
new Word.Drawing(
new WP.Inline(
new WP.Extent() { Cx = extents.Cx, Cy = extents.Cy },
new WP.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new WP.DocProperties() { Id = drawingPr.Id, Name = drawingPr.Name },
new WP.NonVisualGraphicFrameDrawingProperties(),
new Draw.Graphic(
new Draw.GraphicData(
new Dgm.RelationshipIds()
{
DataPart = docDataPartId,
LayoutPart = docLayoutPartId,
StylePart = docStylePartId,
ColorPart = docColorsPartId
}) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/diagram" }))
{
DistanceFromTop = (UInt32Value)0U,
DistanceFromBottom = (UInt32Value)0U,
DistanceFromLeft = (UInt32Value)0U,
DistanceFromRight = (UInt32Value)0U
})));
// Swap out the content control for the SmartArt.
OpenXmlElement parent = sdt.Parent;
parent.InsertAfter(p, sdt);
sdt.Remove();
}
}
}
示例10: ImportChartFromSpreadsheet
void ImportChartFromSpreadsheet(MainDocumentPart mainPart, Word.SdtElement sdt,
SPFile spreadsheetFileName)
{
// Create a paragraph that has an inline drawing object.
Word.Paragraph p = new Word.Paragraph();
Word.Run r = new Word.Run();
p.Append(r);
Word.Drawing drawing = new Word.Drawing();
r.Append(drawing);
// These dimensions work perfectly for the template document.
WP.Inline inline = new WP.Inline(new WP.Extent() { Cx = 5486400, Cy = 3200400 });
byte[] byteArray = spreadsheetFileName.OpenBinary();
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
// Open the Excel spreadsheet.
using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true))
{
// Get all of the appropriate parts.
WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet,
"Sheet2");
DrawingsPart drawingPart = worksheetPart.DrawingsPart;
ChartPart chartPart = (ChartPart)drawingPart.GetPartById("rId1");
// Clone the chart part and add it to the Word document.
ChartPart importedChartPart = mainPart.AddPart<ChartPart>(chartPart);
string relId = mainPart.GetIdOfPart(importedChartPart);
// The frame element contains information for the chart.
GraphicFrame frame =
drawingPart.WorksheetDrawing.Descendants<GraphicFrame>().First();
string chartName = frame.NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name;
// Clone this node so that you can add it to the Word document.
Draw.Graphic clonedGraphic = (Draw.Graphic)frame.Graphic.CloneNode(true);
ChartReference c = clonedGraphic.GraphicData.GetFirstChild<ChartReference>();
c.Id = relId;
// Give the chart a unique ID and name.
WP.DocProperties docPr = new WP.DocProperties();
docPr.Name = chartName;
docPr.Id = GetMaxDocPrId(mainPart) + 1;
// Add the chart data to the inline drawing object.
inline.Append(docPr, clonedGraphic);
drawing.Append(inline);
}
}
OpenXmlElement parent = sdt.Parent;
parent.InsertAfter(p, sdt);
sdt.Remove();
}
示例11: FillContent
private void FillContent(MainDocumentPart mainDocumentPart, IEnumerable<ComplexContentType> controlList)
{
foreach (var type in controlList)
{
var contentControlByTag = GetContentControlByTag(mainDocumentPart, type.Tag);
var paragraph = contentControlByTag.Descendants<Paragraph>().FirstOrDefault();
if (type.ContentType == ContentType.PlainText || type.ContentType == ContentType.RichText)
{
var text = paragraph.Descendants<Text>().FirstOrDefault();
if (text != null)
{
text.Text = contentOfContentControls[type.Tag];
var run = text.Parent as Run;
if (run != null)
run.RunProperties.RunStyle = new RunStyle {Val = "Normal"};
}
}
if (type.ContentType == ContentType.Picture)
{
var imagePart = mainDocumentPart.AddImagePart(ImagePartType.Gif);
using (var fileStream = File.Open("Images\\chess_b_w.gif", FileMode.Open))
{
imagePart.FeedData(fileStream);
}
var blipFill = paragraph.Descendants<BlipFill>().FirstOrDefault();
var blip = blipFill.GetFirstChild<Blip>();
blip.Embed = mainDocumentPart.GetIdOfPart(imagePart);
blipFill.SourceRectangle.Remove();
contentControlByTag.GetFirstChild<SdtProperties>().GetFirstChild<ShowingPlaceholder>().Remove();
}
}
}
示例12: AddRows
private void AddRows(MainDocumentPart mainpart, Table t)
{
var pr = new PictureRow();
foreach (var i in q)
{
var b = ImageDb.FetchBytes(DbUtil.Db, i.PictureId, 160, 200);
string iid = "";
if (b != null)
{
ImagePart imagePart = mainpart.AddImagePart(ImagePartType.Jpeg);
using (var writer = new BinaryWriter(imagePart.GetStream()))
{
writer.Write(b);
}
iid = mainpart.GetIdOfPart(imagePart);
}
var r = pr.GenerateTableRow(i, iid);
t.Append(r);
}
}
示例13: AddChartToDoc
//private void AddChartToDoc(MainDocumentPart mainPart, ChartItem chartItem, string controlName) { }
private void AddChartToDoc(MainDocumentPart mainPart, ChartItem chartItem, string controlName)
{
// open Word documant and remove existing content from control
Paragraph para = findAndRemoveContent(mainPart, controlName);
// generate new ChartPart and ChartSpace
ChartPart chartPart = mainPart.AddNewPart<ChartPart>();
string relId = mainPart.GetIdOfPart(chartPart);
GraphData gd = chartItem.GraphData;
gd.AddEmbeddedToChartPart(chartPart);
C.ChartSpace chartSpace = GraphSpace.GenerateChartSpaceWithData(chartItem.Chart, gd.ExternalDataId);
chartPart.ChartSpace = chartSpace;
// generate a new Wordprocessing Drawing, add to a new Run,
// and relate to new ChartPart
Run run = new Run();
Drawing drawing = GraphDrawing.GenerateDrawing(relId, controlName, docPrId, Graph.Cx, Graph.Cy);
docPrId++;
para.Append(run);
run.Append(drawing);
}
示例14: InsertBarChartIntoWord
public void InsertBarChartIntoWord(MainDocumentPart mainPart, string title)
{
// open Word documant and remove existing content from control
Paragraph para = findAndRemoveContent(mainPart, "Chart1");
// generate new ChartPart and ChartSpace
ChartPart chartPart = mainPart.AddNewPart<ChartPart>();
string relId = mainPart.GetIdOfPart(chartPart);
AllocationComparisonBarChart bc = new AllocationComparisonBarChart();
C.Chart chart = bc.GenerateChart(title, null);
C.ChartSpace chartSpace = GraphSpace.GenerateChartSpace(chart);
// set ChartPart ChartSpace
chartPart.ChartSpace = chartSpace;
// generate Pie Chart and add to ChartSpace
//StressBarChart bc = new StressBarChart();
chartSpace.Append(chart);
// set ChartPart ChartSpace
chartPart.ChartSpace = chartSpace;
// generate a new Wordprocessing Drawing, add to a new Run,
// and relate to new ChartPart
Run run = new Run();
Drawing drawing = GraphDrawing.GenerateDrawing(relId, "Chart 1", 2U, Graph.Cx, Graph.Cy);
para.Append(run);
run.Append(drawing);
// save and close document
mainPart.Document.Save();
}
示例15: CreateImagePart
private string CreateImagePart(MainDocumentPart mainPart, string picturePath)
{
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
using (FileStream stream = new FileStream(picturePath, FileMode.Open)) {
imagePart.FeedData(stream);
}
return mainPart.GetIdOfPart(imagePart);
}