public static XpsElement Parse(XmlTextReader xmlReader) { XpsParser parser = new XpsParser(xmlReader); XpsElement element = parser.Parse(); return(element); }
XpsElement ParseElement() { if (reader.NodeType != XmlNodeType.Element) { throw new InvalidOperationException(PSXSR.MustStandOnElement); } XpsElement element = null; switch (reader.Name) { case "Canvas": element = ParseCanvas(); break; case "FixedPage": element = ParseFixedPage(); break; case "Glyphs": element = ParseGlyphs(); break; case "MatrixTransform": element = ParseMatrixTransform(); break; case "Path": element = ParsePath(); break; case "FixedDocumentSequence": element = ParseFixedDocumentSequence(); break; case "FixedDocument": element = ParseFixedDocument(); break; case "DocumentReference": // TODO MoveBeyondThisElement(); break; case "mc:AlternateContent": // TODO MoveBeyondThisElement(); break; default: Debugger.Break(); break; } return(element); }
/// <summary> /// Parses a Visual element. /// </summary> Visual ParseVisual() { bool isEmptyElement = this.reader.IsEmptyElement; Visual visual = new Visual(); while (MoveToNextAttribute()) { switch (this.reader.Name) { default: UnexpectedAttribute(this.reader.Name); break; } } if (!isEmptyElement) { MoveToNextElement(); while (this.reader.IsStartElement()) { XpsElement element = null; switch (this.reader.Name) { case "Canvas": element = ParseCanvas(); visual.Content.Add(element); element.Parent = visual; break; case "Path": element = ParsePath(); visual.Content.Add(element); element.Parent = visual; break; case "Glyphs": element = ParseGlyphs(); visual.Content.Add(element); element.Parent = visual; break; default: Debugger.Break(); break; } } } MoveToNextElement(); return(visual); }
static T FindStaticResource <T>(string key, ResourceDictionary dict) where T : XpsElement { XpsElement elem = null; while (dict != null) { elem = dict[key]; if (elem != null) { break; } dict = dict.ResourceParent; } T result = elem as T; if (elem != null && result == null) { throw new InvalidCastException("Resource type mismatch."); } return(result); }
/// <summary> /// Writes the specified element to the content stream. /// </summary> public void WriteElement(XpsElement element) { Canvas canvas; Path path; Glyphs glyphs; Visual visual; Comment comment; if ((canvas = element as Canvas) != null) { BeginGraphic(); WriteCanvas(canvas); } else if ((path = element as Path) != null) { BeginGraphic(); WritePath(path); } else if ((glyphs = element as Glyphs) != null) { //BeginText(); WriteGlyphs(glyphs); } else if ((visual = element as Visual) != null) { WriteVisual(visual); //foreach (XpsElement visualElement in visual.Content) // WriteElement(visualElement); } else if ((comment = element as Comment) != null) { //DaSt : Comment? //WriteGlyphs(glyphs); } else { throw new InvalidOperationException("Invalid element type."); } }
/// <summary> /// Writes the specified element to the content stream. /// </summary> public void WriteElement(XpsElement element) { Canvas canvas; Path path; Glyphs glyphs; Visual visual; Comment comment; if ((canvas = element as Canvas) != null) { BeginGraphic(); WriteCanvas(canvas); } else if ((path = element as Path) != null) { BeginGraphic(); WritePath(path); } else if ((glyphs = element as Glyphs) != null) { //BeginText(); WriteGlyphs(glyphs); } else if ((visual = element as Visual) != null) { WriteVisual(visual); //foreach (XpsElement visualElement in visual.Content) // WriteElement(visualElement); } else if ((comment = element as Comment) != null) { //DaSt : Comment? //WriteGlyphs(glyphs); } else throw new InvalidOperationException("Invalid element type."); }
/// <summary> /// A first hack to do the job... /// </summary> public static void RenderPage_Test01(PdfPage page, string xpsFilename) { //XpsDocument xpsdoc = new XpsDocument(xpsFilename, System.IO.FileAccess.Read); //FixedDocument fds = xpsdoc.GetFixedDocument(); //DocumentReferenceCollection docrefs = fds.References; //DocumentReference docref = docrefs[0]; //Uri uri1 = docref.Source; //FixedDocument fixeddoc = docref.GetDocument(false); //PageContent content = fixeddoc.Pages[0]; //Uri uri2 = content.Source; //FixedPage fixedPage = content.Child; //xpsdoc.Close(); // /Documents/1/Pages/1.fpage try { #if true XpsDocument doc = XpsDocument.Open(xpsFilename); FixedPage fpage = doc.GetDocument().GetFixedPage(0); //ZipPackage pack = ZipPackage.Open(xpsFilename) as ZipPackage; Uri uri = new Uri("/Documents/1/Pages/1.fpage", UriKind.Relative); ZipPackagePart part = doc.Package.GetPart(uri) as ZipPackagePart; using (Stream stream = part.GetStream()) { using (StreamReader sr = new StreamReader(stream)) { string xml = sr.ReadToEnd(); #if true string xmlPath = IOPath.Combine(IOPath.GetDirectoryName(xpsFilename), IOPath.GetFileNameWithoutExtension(xpsFilename)) + ".xml"; using (StreamWriter sw = new StreamWriter(xmlPath)) { sw.Write(xml); } #endif DocumentRenderingContext context = new DocumentRenderingContext(page.Owner); //XpsElement el = PdfSharp.Xps.Parsing.XpsParser.Parse(xml); PdfRenderer renderer = new PdfRenderer(); renderer.RenderPage(page, fpage); } } #else ZipPackage pack = ZipPackage.Open(xpsFilename) as ZipPackage; Uri uri = new Uri("/Documents/1/Pages/1.fpage", UriKind.Relative); ZipPackagePart part = pack.GetPart(uri) as ZipPackagePart; using (Stream stream = part.GetStream()) { using (StreamReader sr = new StreamReader(stream)) { string xml = sr.ReadToEnd(); #if true string xmlPath = IOPath.Combine(IOPath.GetDirectoryName(xpsFilename), IOPath.GetFileNameWithoutExtension(xpsFilename)) + ".xml"; using (StreamWriter sw = new StreamWriter(xmlPath)) { sw.Write(xml); } #endif XpsElement el = PdfSharp.Xps.Parsing.XpsParser.Parse(xml); PdfRenderer renderer = new PdfRenderer(); renderer.RenderPage(page, el as PdfSharp.Xps.XpsModel.FixedPage); } } #endif } catch { //DaSt : } }
/// <summary> /// Parses a FixedPage element. /// </summary> FixedPage ParseFixedPage() { Debug.Assert(this.reader.Name == "FixedPage"); FixedPage fpage = new FixedPage(); try { bool isEmptyElement = this.reader.IsEmptyElement; Debug.Assert(this.fpage == null); this.fpage = fpage; while (this.reader.MoveToNextAttribute()) { switch (this.reader.Name) { case "Name": fpage.Name = this.reader.Value; break; case "Width": fpage.Width = ParseDouble(this.reader.Value); break; case "Height": fpage.Height = ParseDouble(this.reader.Value); break; case "ContentBox": fpage.ContentBox = Rect.Parse(this.reader.Value); break; case "BleedBox": fpage.BleedBox = Rect.Parse(this.reader.Value); break; case "xmlns": break; case "xmlns:xps": break; case "xmlns:false": break; case "xmlns:mc": break; case "xmlns:x": break; case "xmlns:xml": break; case "xmlns:xsi": break; case "xmlns:v2": break; case "xml:lang": fpage.Lang = this.reader.Value; break; case "xml:space": break; case "xsi:schemaLocation": break; case "mc:MustUnderstand": break; default: UnexpectedAttribute(this.reader.Name); break; } } if (!isEmptyElement) { MoveToNextElement(); while (this.reader.IsStartElement()) { XpsElement element = null; switch (reader.Name) { case "Path": element = ParsePath(); fpage.Content.Add(element); element.Parent = fpage; break; case "Canvas": element = ParseCanvas(); fpage.Content.Add(element); element.Parent = fpage; break; case "Glyphs": element = ParseGlyphs(); fpage.Content.Add(element); element.Parent = fpage; break; case "MatrixTransform": Debugger.Break(); ParseMatrixTransform(); //fpage. //element = ParseGlyphs(); //fpage.Content.Add(element); break; case "FixedPage.Resources": MoveToNextElement(); ResourceDictionary rd = new ResourceDictionary(); fpage.Resources = rd; rd.Parent = fpage; rd.ResourceParent = ResourceDictionaryStack.Current; ResourceDictionaryStack.Push(rd); ParseResourceDictionary(rd); MoveToNextElement(); break; case "mc:AlternateContent": MoveToNextElement(); break; case "mc:Choice": MoveToNextElement(); break; case "v2:Circle": MoveToNextElement(); break; case "v2:Watermark": MoveToNextElement(); break; case "v2:Blink": MoveToNextElement(); break; default: Debugger.Break(); break; } } } MoveToNextElement(); } finally { // If the current ResourceDictionary is from this FixedPage, pop it. if (fpage != null && fpage.Resources != null) { if (Object.ReferenceEquals(fpage.Resources, ResourceDictionaryStack.Current)) { ResourceDictionaryStack.Pop(); } } } return(fpage); }