/// <summary> /// Parses the given XPS file and saves the PDF Version. /// </summary> void ParseFile(string file) { string filename = file + ".xps"; PdfDocument document = new PdfDocument(); try { XpsModel.XpsDocument xpsDoc = XpsModel.XpsDocument.Open(filename); foreach (XpsModel.FixedDocument doc in xpsDoc.Documents) { foreach (XpsModel.FixedPage page in doc.Pages) { page.GetType(); //Render PDF Page PdfPage pdfpage = document.AddPage(); pdfpage.Width = page.PointWidth; pdfpage.Height = page.PointHeight; PdfRenderer renderer = new PdfRenderer(); renderer.RenderPage(pdfpage, page); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } document.Save(Path.Combine(OutputDirectory, file + ".pdf")); }
/// <summary> /// Parses the given XPS file and saves the PDF Version. /// </summary> void ParseFile(string file) { string baseDir = GetDirectory("PdfSharp/testing/PdfSharp.Xps.UnitTests/Primitives.Glyphs/GlyphFiles"); string filename = System.IO.Path.Combine(baseDir, file + ".xps"); PdfDocument document = new PdfDocument(); try { XpsModel.XpsDocument xpsDoc = XpsModel.XpsDocument.Open(filename); foreach (XpsModel.FixedDocument doc in xpsDoc.Documents) { foreach (XpsModel.FixedPage page in doc.Pages) { page.GetType(); //Render PDF Page PdfPage pdfpage = document.AddPage(); pdfpage.Width = page.PointWidth; pdfpage.Height = page.PointHeight; PdfRenderer renderer = new PdfRenderer(); renderer.RenderPage(pdfpage, page); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } document.Save(Path.Combine(OutputDirectory, file + ".pdf")); }
/// <summary> /// /// </summary> public static void Parser_Test01(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 { XpsDocument document = new PdfSharp.Xps.XpsModel.XpsDocument(xpsFilename); FixedDocument fdoc = document.GetDocument(); FixedPage fpage = fdoc.GetFixedPage(0); //string s = fpage.c.Source; //s.GetType(); //Uri uri; //ZipPackage package = ZipPackage.Open(xpsFilename) as ZipPackage; //PackagePartCollection parts = package.GetParts(); //PackageRelationshipCollection relships = package.GetRelationships(); //uri = new Uri("/FixedDocSeq.fdseq", UriKind.Relative); //string x = GetPartAsString(package, uri); ////ZipPackagePart part = pack.GetPart(uri) as ZipPackagePart; //PdfSharp.Xps.Parsing.XpsParser.Parse(GetPartAsXmlReader(package, uri)); // 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); // } // } } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } }
public FixedPayload(XpsDocument document) { this.resMngr = new ResourceManager(this); this.document = document; // HACK: find fdseq string fdseqString = null; foreach (ZipPackagePart part in XpsDocument.Parts) { //string uriString = part.Uri.OriginalString; //if (uriString.EndsWith("fdseq", true, CultureInfo.InvariantCulture)) //File extension for fdseq file can be different. //[Content_Types].xml contains the informations needed and already pared by ZipPackage. if (part.ContentType.Contains("application/vnd.ms-package.xps-fixeddocumentsequence+xml")) { fdseqString = part.Uri.OriginalString; break; } } Debug.Assert(fdseqString != null); this.fdseq = XpsParser.Parse(GetPartAsXmlReader(fdseqString)) as FixedDocumentSequence; this.fdocs = new FixedDocument[this.fdseq.DocumentReferences.Count]; }
public void TestParsingAllSamples() { string[] files = Directory.GetFiles("../../../XPS-TestDocuments", "*.xps", SearchOption.AllDirectories); if (files.Length == 0) throw new Exception("No sample file found. Copy sample files to the \"SampleXpsDocuments_1_0\" folder!"); foreach (string filename in files) { try { XpsDocument xpsDoc = new XpsDocument(filename); foreach (FixedDocument doc in xpsDoc.Documents) { foreach (FixedPage page in doc.Pages) { page.GetType(); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } } }
/// <summary> /// Initializes a new instance of the <see cref="XpsConverter"/> class. /// </summary> /// <param name="pdfDocument">The PDF document.</param> /// <param name="xpsDocument">The XPS document.</param> public XpsConverter(PdfDocument pdfDocument, XpsDocument xpsDocument) { if (pdfDocument == null) throw new ArgumentNullException("pdfDocument"); if (xpsDocument == null) throw new ArgumentNullException("xpsDocument"); this.pdfDocument = pdfDocument; this.xpsDocument = xpsDocument; Initialize(); }
/// <summary> /// Implements the PDF file to XPS file conversion. /// </summary> public static void Convert(string xpsFilename, string pdfFilename, int docIndex, bool createComparisonDocument) { if (String.IsNullOrEmpty(xpsFilename)) throw new ArgumentNullException("xpsFilename"); if (String.IsNullOrEmpty(pdfFilename)) { pdfFilename = xpsFilename; if (IOPath.HasExtension(pdfFilename)) pdfFilename = pdfFilename.Substring(0, pdfFilename.LastIndexOf('.')); pdfFilename += ".pdf"; } XpsDocument xpsDocument = null; try { xpsDocument = XpsDocument.Open(xpsFilename); FixedDocument fixedDocument = xpsDocument.GetDocument(); PdfDocument pdfDocument = new PdfDocument(); PdfRenderer renderer = new PdfRenderer(); int pageIndex = 0; foreach (FixedPage page in fixedDocument.Pages) { if (page == null) continue; Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); PdfPage pdfPage = renderer.CreatePage(pdfDocument, page); renderer.RenderPage(pdfPage, page); pageIndex++; #if DEBUG // stop at page... if (pageIndex == 50) break; #endif } pdfDocument.Save(pdfFilename); xpsDocument.Close(); xpsDocument = null; if (createComparisonDocument) { System.Windows.Xps.Packaging.XpsDocument xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(xpsFilename, FileAccess.Read); System.Windows.Documents.FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence(); if (docSeq == null) throw new InvalidOperationException("docSeq"); XPdfForm form = XPdfForm.FromFile(pdfFilename); PdfDocument pdfComparisonDocument = new PdfDocument(); pageIndex = 0; foreach (PdfPage page in pdfDocument.Pages) { if (page == null) continue; Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); PdfPage pdfPage = /*renderer.CreatePage(pdfComparisonDocument, page);*/pdfComparisonDocument.AddPage(); double width = page.Width; double height = page.Height; pdfPage.Width = page.Width * 2; pdfPage.Height = page.Height; DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageIndex); //byte[] png = PngFromPage(docPage, 96); BitmapSource bmsource = BitmapSourceFromPage(docPage, 96 * 2); XImage image = XImage.FromBitmapSource(bmsource); XGraphics gfx = XGraphics.FromPdfPage(pdfPage); form.PageIndex = pageIndex; gfx.DrawImage(form, 0, 0, width, height); gfx.DrawImage(image, width, 0, width, height); //renderer.RenderPage(pdfPage, page); pageIndex++; #if DEBUG // stop at page... if (pageIndex == 50) break; #endif } string pdfComparisonFilename = pdfFilename; if (IOPath.HasExtension(pdfComparisonFilename)) pdfComparisonFilename = pdfComparisonFilename.Substring(0, pdfComparisonFilename.LastIndexOf('.')); pdfComparisonFilename += "-comparison.pdf"; pdfComparisonDocument.ViewerPreferences.FitWindow = true; //pdfComparisonDocument.PageMode = PdfPageMode. pdfComparisonDocument.PageLayout = PdfPageLayout.SinglePage; pdfComparisonDocument.Save(pdfComparisonFilename); } } catch (Exception ex) { Debug.WriteLine(ex.Message); if (xpsDocument != null) xpsDocument.Close(); throw; } finally { if (xpsDocument != null) xpsDocument.Close(); } }
/// <summary> /// Implements the PDF file to XPS file conversion. /// </summary> public static void Convert(XpsDocument xpsDoc, string pdfFilename) { int docIndex = 0; XpsDocument xpsDocument = null; try { xpsDocument = xpsDoc; FixedDocument fixedDocument = xpsDocument.GetDocument(); PdfDocument pdfDocument = new PdfDocument(); PdfRenderer renderer = new PdfRenderer(); int pageIndex = 0; foreach (FixedPage page in fixedDocument.Pages) { if (page == null) continue; Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); PdfPage pdfPage = renderer.CreatePage(pdfDocument, page); renderer.RenderPage(pdfPage, page); pageIndex++; #if DEBUG // stop at page... if (pageIndex == 50) break; #endif } pdfDocument.Save(pdfFilename); xpsDocument.Close(); xpsDocument = null; } catch (Exception ex) { Debug.WriteLine(ex.Message); if (xpsDocument != null) xpsDocument.Close(); throw; } finally { if (xpsDocument != null) xpsDocument.Close(); } }
/// <summary> /// Implements the PDF file to XPS file conversion. /// </summary> public static void Convert(XpsDocument xpsDocument, string pdfFilename, int docIndex) { if (xpsDocument == null) throw new ArgumentNullException("xpsDocument"); if (String.IsNullOrEmpty(pdfFilename)) throw new ArgumentNullException("pdfFilename"); FixedDocument fixedDocument = xpsDocument.GetDocument(); PdfDocument pdfDocument = new PdfDocument(); PdfRenderer renderer = new PdfRenderer(); int pageIndex = 0; foreach (FixedPage page in fixedDocument.Pages) { if (page == null) continue; Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); PdfPage pdfPage = renderer.CreatePage(pdfDocument, page); renderer.RenderPage(pdfPage, page); pageIndex++; #if DEBUG // stop at page... if (pageIndex == 50) break; #endif } pdfDocument.Save(pdfFilename); }