本文整理汇总了C#中System.IO.Packaging.Package.GetParts方法的典型用法代码示例。如果您正苦于以下问题:C# Package.GetParts方法的具体用法?C# Package.GetParts怎么用?C# Package.GetParts使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Packaging.Package
的用法示例。
在下文中一共展示了Package.GetParts方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyPackage
private static void CopyPackage(Package inputPackage, Package outputPackage)
{
foreach (var inputPart in inputPackage.GetParts())
{
CreateNewPart(inputPart, outputPackage);
}
}
示例2: Open
public bool Open(string path)
{
try
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
memStream = new MemoryStream();
memStream.Write(buffer, 0, (int)fs.Length);
buffer = null;
fs.Close();
package = Package.Open(memStream, FileMode.Open, FileAccess.ReadWrite);
docParts = new Dictionary<string, DocumentPart>();
PackagePartCollection parts = package.GetParts();
foreach (PackagePart part in parts)
{
DocumentPart docPart = new DocumentPart(part);
docParts.Add(part.Uri.ToString(), docPart);
}
filePath = path;
dirty = false;
return true;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
return false;
}
}
开发者ID:VitorX,项目名称:Open-XML-Package-Editor-Power-Tool-for-Visual-Studio,代码行数:33,代码来源:DocumentPackage.cs
示例3: Sign
private Package Sign(Package package)
{
foreach (var packagePart in package.GetParts())
{
SignPackagePart(packagePart);
}
return package;
}
示例4: PackageFilter
/// <summary>
/// Constructor.
/// </summary>
/// <param name="package">package to filter</param>
internal PackageFilter(Package package)
{
if (package == null)
{
throw new ArgumentNullException("package");
}
_package = package;
_partIterator = _package.GetParts().GetEnumerator();
}
示例5: SignAllParts
/// <summary>
/// Main signing process
/// </summary>
/// <param name="package"></param>
/// <returns></returns>
private bool SignAllParts(Package package)
{
if (package == null)
throw new ArgumentNullException("SignAllParts(package)");
// Create the DigitalSignature Manager
PackageDigitalSignatureManager dsm =
new PackageDigitalSignatureManager(package);
dsm.CertificateOption =
CertificateEmbeddingOption.InSignaturePart;
// Create a list of all the part URIs in the package to sign
// (GetParts() also includes PackageRelationship parts).
System.Collections.Generic.List<Uri> toSign =
new System.Collections.Generic.List<Uri>();
foreach (PackagePart packagePart in package.GetParts())
{
// Add all package parts to the list for signing.
toSign.Add(packagePart.Uri);
}
// Add the URI for SignatureOrigin PackageRelationship part.
// The SignatureOrigin relationship is created when Sign() is called.
// Signing the SignatureOrigin relationship disables counter-signatures.
toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin));
// Also sign the SignatureOrigin part.
toSign.Add(dsm.SignatureOrigin);
// Add the package relationship to the signature origin to be signed.
toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));
// Sign() will prompt the user to select a Certificate to sign with.
try
{
var cert = new X509Certificate2(this.CertificatePath, (String.IsNullOrEmpty(this.CertificatePassword) ? null : this.CertificatePassword));
dsm.Sign(toSign, cert);
}
// If there are no certificates or the SmartCard manager is
// not running, catch the exception and show an error message.
catch (CryptographicException ex)
{
Console.WriteLine(
"Cannot Sign: {0}", ex.Message);
}
return dsm.IsSigned && dsm.VerifySignatures(true) == VerifyResult.Success;
}
示例6: SignAllParts
private static void SignAllParts(Package package)
{
if (package == null)
throw new ArgumentNullException("SignAllParts(package)");
// Create the DigitalSignature Manager
PackageDigitalSignatureManager dsm =
new PackageDigitalSignatureManager(package);
dsm.CertificateOption =
CertificateEmbeddingOption.InSignaturePart;
// Create a list of all the part URIs in the package to sign
// (GetParts() also includes PackageRelationship parts).
System.Collections.Generic.List<Uri> toSign =
new System.Collections.Generic.List<Uri>();
foreach (PackagePart packagePart in package.GetParts())
{
// Add all package parts to the list for signing.
toSign.Add(packagePart.Uri);
}
// Add the URI for SignatureOrigin PackageRelationship part.
// The SignatureOrigin relationship is created when Sign() is called.
// Signing the SignatureOrigin relationship disables counter-signatures.
toSign.Add(PackUriHelper.GetRelationshipPartUri(dsm.SignatureOrigin));
// Also sign the SignatureOrigin part.
toSign.Add(dsm.SignatureOrigin);
// Add the package relationship to the signature origin to be signed.
toSign.Add(PackUriHelper.GetRelationshipPartUri(new Uri("/", UriKind.RelativeOrAbsolute)));
// Sign() will prompt the user to select a Certificate to sign with.
try
{
dsm.Sign(toSign);
}
// If there are no certificates or the SmartCard manager is
// not running, catch the exception and show an error message.
catch (CryptographicException ex)
{
MessageBox.Show(
"Cannot Sign\n" + ex.Message,
"No Digital Certificates Available",
MessageBoxButton.OK,
MessageBoxImage.Exclamation);
}
}
示例7: UnzipTo
private void UnzipTo(Package package, string outPath)
{
var packageParts=package.GetParts();
foreach (PackagePart part in packageParts)
{
string outFileName = Path.Combine(outPath, part.Uri.OriginalString.Substring(1));
using (FileStream outFileStream = new FileStream(outFileName, FileMode.Create))
{
using (Stream inFileStream = part.GetStream())
{
CopyStream(inFileStream, outFileStream);
}
}
}
}
示例8: AddDefaultNumberingXml
/// <summary>
/// If this document does not contain a /word/numbering.xml add the default one generated by Microsoft Word
/// when the default bullet, numbered and multilevel lists are added to a blank document
/// </summary>
/// <param name="package"></param>
/// <param name="mainDocumentPart"></param>
/// <returns></returns>
internal static XDocument AddDefaultNumberingXml(Package package)
{
XDocument numberingDoc;
// Create the main document part for this package
PackagePart wordNumbering = package.CreatePart(new Uri("/word/numbering.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", CompressionOption.Maximum);
numberingDoc = DecompressXMLResource("Novacode.Resources.numbering.xml.gz");
// Save /word/numbering.xml
using (TextWriter tw = new StreamWriter(wordNumbering.GetStream(FileMode.Create, FileAccess.Write)))
numberingDoc.Save(tw, SaveOptions.None);
PackagePart mainDocumentPart = package.GetParts().Single(p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) ||
p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase));
mainDocumentPart.CreateRelationship(wordNumbering.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering");
return numberingDoc;
}
示例9: VerifyPartsAreTheSame
void VerifyPartsAreTheSame(Package package1, Package package2)
{
foreach (var part1 in package1.GetParts())
{
if (part1.Uri.OriginalString.EndsWith("psmdcp"))
{
continue;
}
if (part1.Uri.OriginalString.EndsWith("rels"))
{
continue;
}
var part2 = package2.GetPart(part1.Uri);
var hash1 = GetFileHash(part1);
var hash2 = GetFileHash(part2);
Assert.AreEqual(hash1, hash2, part1.Uri.OriginalString);
}
}
示例10: AddDefaultStylesXml
/// <summary>
/// If this document does not contain a /word/styles.xml add the default one generated by Microsoft Word.
/// </summary>
/// <param name="package"></param>
/// <param name="mainDocumentPart"></param>
/// <returns></returns>
internal static XDocument AddDefaultStylesXml(Package package)
{
XDocument stylesDoc;
// Create the main document part for this package
PackagePart word_styles = package.CreatePart(new Uri("/word/styles.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", CompressionOption.Maximum);
stylesDoc = HelperFunctions.DecompressXMLResource("Novacode.Resources.default_styles.xml.gz");
XElement lang = stylesDoc.Root.Element(XName.Get("docDefaults", DocX.w.NamespaceName)).Element(XName.Get("rPrDefault", DocX.w.NamespaceName)).Element(XName.Get("rPr", DocX.w.NamespaceName)).Element(XName.Get("lang", DocX.w.NamespaceName));
lang.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), CultureInfo.CurrentCulture);
// Save /word/styles.xml
using (TextWriter tw = new StreamWriter(word_styles.GetStream(FileMode.Create, FileAccess.Write)))
stylesDoc.Save(tw, SaveOptions.None);
PackagePart mainDocumentPart = package.GetParts().Where
(
p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase)||p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase)
).Single();
mainDocumentPart.CreateRelationship(word_styles.Uri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles");
return stylesDoc;
}
示例11: OpcToFlatOpc
/// <summary>
/// Returns an XDocument
/// </summary>
/// <param name="package">System.IO.Packaging.Package</param>
/// <returns></returns>
public static XDocument OpcToFlatOpc(Package package)
{
XNamespace
pkg = "http://schemas.microsoft.com/office/2006/xmlPackage";
XDeclaration
declaration = new XDeclaration("1.0", "UTF-8", "yes");
XDocument doc = new XDocument(
declaration,
new XProcessingInstruction("mso-application", "progid=\"Word.Document\""),
new XElement(pkg + "package",
new XAttribute(XNamespace.Xmlns + "pkg", pkg.ToString()),
package.GetParts().Select(part => GetContentsAsXml(part))
)
);
return doc;
}
示例12: CreateOrGetSettingsPart
internal static PackagePart CreateOrGetSettingsPart(Package package)
{
PackagePart settingsPart;
Uri settingsUri = new Uri("/word/settings.xml", UriKind.Relative);
if (!package.PartExists(settingsUri))
{
settingsPart = package.CreatePart(settingsUri, "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", CompressionOption.Maximum);
PackagePart mainDocumentPart = package.GetParts().Single(p => p.ContentType.Equals(DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) ||
p.ContentType.Equals(TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase));
mainDocumentPart.CreateRelationship(settingsUri, TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings");
XDocument settings = XDocument.Parse
(@"<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<w:settings 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:w10='urn:schemas-microsoft-com:office:word' xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' xmlns:sl='http://schemas.openxmlformats.org/schemaLibrary/2006/main'>
<w:zoom w:percent='100' />
<w:defaultTabStop w:val='720' />
<w:characterSpacingControl w:val='doNotCompress' />
<w:compat />
<w:rsids>
<w:rsidRoot w:val='00217F62' />
<w:rsid w:val='001915A3' />
<w:rsid w:val='00217F62' />
<w:rsid w:val='00A906D8' />
<w:rsid w:val='00AB5A74' />
<w:rsid w:val='00F071AE' />
</w:rsids>
<m:mathPr>
<m:mathFont m:val='Cambria Math' />
<m:brkBin m:val='before' />
<m:brkBinSub m:val='--' />
<m:smallFrac m:val='off' />
<m:dispDef />
<m:lMargin m:val='0' />
<m:rMargin m:val='0' />
<m:defJc m:val='centerGroup' />
<m:wrapIndent m:val='1440' />
<m:intLim m:val='subSup' />
<m:naryLim m:val='undOvr' />
</m:mathPr>
<w:themeFontLang w:val='en-IE' w:bidi='ar-SA' />
<w:clrSchemeMapping w:bg1='light1' w:t1='dark1' w:bg2='light2' w:t2='dark2' w:accent1='accent1' w:accent2='accent2' w:accent3='accent3' w:accent4='accent4' w:accent5='accent5' w:accent6='accent6' w:hyperlink='hyperlink' w:followedHyperlink='followedHyperlink' />
<w:shapeDefaults>
<o:shapedefaults v:ext='edit' spidmax='2050' />
<o:shapelayout v:ext='edit'>
<o:idmap v:ext='edit' data='1' />
</o:shapelayout>
</w:shapeDefaults>
<w:decimalSymbol w:val='.' />
<w:listSeparator w:val=',' />
</w:settings>"
);
XElement themeFontLang = settings.Root.Element(XName.Get("themeFontLang", DocX.w.NamespaceName));
themeFontLang.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), CultureInfo.CurrentCulture);
// Save the settings document.
using (TextWriter tw = new StreamWriter(settingsPart.GetStream()))
settings.Save(tw);
}
else
settingsPart = package.GetPart(settingsUri);
return settingsPart;
}
示例13: PopulateDocument
private static void PopulateDocument(DocX document, Package package)
{
Headers headers = new Headers();
headers.odd = document.GetHeaderByType("default");
headers.even = document.GetHeaderByType("even");
headers.first = document.GetHeaderByType("first");
Footers footers = new Footers();
footers.odd = document.GetFooterByType("default");
footers.even = document.GetFooterByType("even");
footers.first = document.GetFooterByType("first");
//// Get the sectPr for this document.
//XElement sect = document.mainDoc.Descendants(XName.Get("sectPr", DocX.w.NamespaceName)).Single();
//if (sectPr != null)
//{
// // Extract the even header reference
// var header_even_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "headerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "even");
// string id = header_even_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
// var res = document.mainPart.GetRelationship(id);
// string ans = res.SourceUri.OriginalString;
// headers.even.xml_filename = ans;
// // Extract the odd header reference
// var header_odd_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "headerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "default");
// string id2 = header_odd_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
// var res2 = document.mainPart.GetRelationship(id2);
// string ans2 = res2.SourceUri.OriginalString;
// headers.odd.xml_filename = ans2;
// // Extract the first header reference
// var header_first_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "h
//eaderReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "first");
// string id3 = header_first_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
// var res3 = document.mainPart.GetRelationship(id3);
// string ans3 = res3.SourceUri.OriginalString;
// headers.first.xml_filename = ans3;
// // Extract the even footer reference
// var footer_even_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "footerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "even");
// string id4 = footer_even_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
// var res4 = document.mainPart.GetRelationship(id4);
// string ans4 = res4.SourceUri.OriginalString;
// footers.even.xml_filename = ans4;
// // Extract the odd footer reference
// var footer_odd_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "footerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "default");
// string id5 = footer_odd_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
// var res5 = document.mainPart.GetRelationship(id5);
// string ans5 = res5.SourceUri.OriginalString;
// footers.odd.xml_filename = ans5;
// // Extract the first footer reference
// var footer_first_ref = sectPr.Elements().SingleOrDefault(x => x.Name.LocalName == "footerReference" && x.Attribute(XName.Get("type", DocX.w.NamespaceName)) != null && x.Attribute(XName.Get("type", DocX.w.NamespaceName)).Value == "first");
// string id6 = footer_first_ref.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
// var res6 = document.mainPart.GetRelationship(id6);
// string ans6 = res6.SourceUri.OriginalString;
// footers.first.xml_filename = ans6;
//}
document.Xml = document.mainDoc.Root.Element(w + "body");
document.headers = headers;
document.footers = footers;
document.settingsPart = HelperFunctions.CreateOrGetSettingsPart(package);
var ps = package.GetParts();
//document.endnotesPart = HelperFunctions.GetPart();
foreach (var rel in document.mainPart.GetRelationships())
{
switch (rel.RelationshipType)
{
case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes":
document.endnotesPart = package.GetPart(new Uri("/word/" + rel.TargetUri.OriginalString.Replace("/word/", ""), UriKind.Relative));
using (TextReader tr = new StreamReader(document.endnotesPart.GetStream()))
document.endnotes = XDocument.Load(tr);
break;
case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes":
document.footnotesPart = package.GetPart(new Uri("/word/" + rel.TargetUri.OriginalString.Replace("/word/", ""), UriKind.Relative));
using (TextReader tr = new StreamReader(document.footnotesPart.GetStream()))
document.footnotes = XDocument.Load(tr);
break;
case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles":
document.stylesPart = package.GetPart(new Uri("/word/" + rel.TargetUri.OriginalString.Replace("/word/", ""), UriKind.Relative));
using (TextReader tr = new StreamReader(document.stylesPart.GetStream()))
document.styles = XDocument.Load(tr);
break;
case "http://schemas.microsoft.com/office/2007/relationships/stylesWithEffects":
document.stylesWithEffectsPart = package.GetPart(new Uri("/word/" + rel.TargetUri.OriginalString.Replace("/word/", ""), UriKind.Relative));
using (TextReader tr = new StreamReader(document.stylesWithEffectsPart.GetStream()))
document.stylesWithEffects = XDocument.Load(tr);
break;
case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable":
//.........这里部分代码省略.........
示例14: PostLoad
internal static DocX PostLoad(ref Package package)
{
DocX document = new DocX(null, null);
document.package = package;
document.Document = document;
#region MainDocumentPart
document.mainPart = package.GetParts().Where
(
p => p.ContentType.Equals(HelperFunctions.DOCUMENT_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase) ||
p.ContentType.Equals(HelperFunctions.TEMPLATE_DOCUMENTTYPE, StringComparison.CurrentCultureIgnoreCase)
).Single();
using (TextReader tr = new StreamReader(document.mainPart.GetStream(FileMode.Open, FileAccess.Read)))
document.mainDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace);
#endregion
PopulateDocument(document, package);
using (TextReader tr = new StreamReader(document.settingsPart.GetStream()))
document.settings = XDocument.Load(tr);
document.paragraphLookup.Clear();
foreach (var paragraph in document.Paragraphs)
{
if (!document.paragraphLookup.ContainsKey(paragraph.endIndex))
document.paragraphLookup.Add(paragraph.endIndex, paragraph);
}
return document;
}
示例15: ReloadFromMemory
public void ReloadFromMemory()
{
Debug.Assert(memStream != null);
this.docParts = null;
package.Close();
package = null;
package = Package.Open(memStream, FileMode.Open, FileAccess.ReadWrite);
docParts = new Dictionary<string, DocumentPart>();
PackagePartCollection parts = package.GetParts();
foreach (PackagePart part in parts)
{
DocumentPart docPart = new DocumentPart(part);
docParts.Add(part.Uri.ToString(), docPart);
}
}
开发者ID:VitorX,项目名称:Open-XML-Package-Editor-Power-Tool-for-Visual-Studio,代码行数:17,代码来源:DocumentPackage.cs