/// <summary> /// Parses the given XPS file and saves the PDF Version. /// </summary> void ParseFile(string file) { string baseDir = "../../../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 = WidthInPoint; pdfpage.Height = HeightInPoint; PdfRenderer renderer = new PdfRenderer(); renderer.RenderPage(pdfpage, page); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } document.Save(Path.Combine(OutputDirectory, file + ".pdf")); }
public void TestExternalXpsFiles() { string currentDirectory = Directory.GetCurrentDirectory(); #if true string[] files = Directory.GetFiles("../../../../../testing/PdfSharp.Xps.UnitTests/xternal", "*.xps", SearchOption.AllDirectories); #else string[] files = Directory.GetFiles("../../../XPS-TestDocuments", "*.xps", SearchOption.AllDirectories); #endif if (files.Length == 0) throw new Exception("No sample file found."); foreach (string filename in files) { // No negative tests here if (filename.Contains("\\ConformanceViolations\\")) continue; Debug.WriteLine(filename); try { int docIndex = 0; XpsDocument xpsDoc = XpsDocument.Open(filename); foreach (FixedDocument doc in xpsDoc.Documents) { PdfDocument pdfDoc = new PdfDocument(); PdfRenderer renderer = new PdfRenderer(); int pageIndex = 0; foreach (FixedPage page in doc.Pages) { Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); PdfPage pdfPage = renderer.CreatePage(pdfDoc, page); renderer.RenderPage(pdfPage, page); pageIndex++; } string pdfFilename = IOPath.GetFileNameWithoutExtension(filename); if (docIndex != 0) pdfFilename += docIndex.ToString(); pdfFilename += ".pdf"; pdfFilename = IOPath.Combine(IOPath.GetDirectoryName(filename), pdfFilename); pdfDoc.Save(pdfFilename); docIndex++; } } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } } }
public void TestRenderingAllSamples() { string dir = Directory.GetCurrentDirectory(); int slash = dir.LastIndexOf("\\"); while ((slash = dir.LastIndexOf("\\")) != -1) { if (dir.EndsWith("PdfSharp")) break; dir = dir.Substring(0, slash); } dir += "/testing/SampleXpsDocuments_1_0"; #if true string[] files = Directory.GetFiles(dir, "*.xps", SearchOption.AllDirectories); #else string[] files = Directory.GetFiles("../../../XPS-TestDocuments", "*.xps", SearchOption.AllDirectories); #endif //files = Directory.GetFiles(@"G:\!StLa\PDFsharp-1.20\SourceCode\PdfSharp\testing\SampleXpsDocuments_1_0\MXDW", "*.xps", SearchOption.AllDirectories); //files = Directory.GetFiles(@"G:\!StLa\PDFsharp-1.20\SourceCode\PdfSharp\testing\SampleXpsDocuments_1_0\Handcrafted", "*.xps", SearchOption.AllDirectories); //files = Directory.GetFiles(@"G:\!StLa\PDFsharp-1.20\SourceCode\PdfSharp\testing\SampleXpsDocuments_1_0\Showcase", "*.xps", SearchOption.AllDirectories); files = Directory.GetFiles(@"G:\!StLa\PDFsharp-1.20\SourceCode\PdfSharp\testing\SampleXpsDocuments_1_0\QualityLogicMinBar", "*.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) { // No negative tests here if (filename.Contains("\\ConformanceViolations\\")) continue; //if (filename.Contains("\\Showcase\\")) // continue; //if (!filename.Contains("Vista")) // continue; Debug.WriteLine(filename); try { int docIndex = 0; XpsDocument xpsDoc = XpsDocument.Open(filename); foreach (FixedDocument doc in xpsDoc.Documents) { try { PdfDocument pdfDoc = new PdfDocument(); PdfRenderer renderer = new PdfRenderer(); int pageIndex = 0; foreach (FixedPage page in doc.Pages) { if (page == null) continue; Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); PdfPage pdfPage = renderer.CreatePage(pdfDoc, page); renderer.RenderPage(pdfPage, page); pageIndex++; // stop at page... if (pageIndex == 50) break; } string pdfFilename = IOPath.GetFileNameWithoutExtension(filename); if (docIndex != 0) pdfFilename += docIndex.ToString(); pdfFilename += ".pdf"; pdfFilename = IOPath.Combine(IOPath.GetDirectoryName(filename), pdfFilename); pdfDoc.Save(pdfFilename); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } docIndex++; } } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } } }
/// <summary> /// Renders an XPS document page to the specified PDF page. /// </summary> /// <param name="page">The target PDF page. The page must belong to the PDF document of this converter.</param> /// <param name="xpsPageIndex">The zero-based XPS page number.</param> public void RenderPage(PdfPage page, int xpsPageIndex) { if (page == null) throw new ArgumentNullException("page"); if (!ReferenceEquals(page.Owner, this.pdfDocument)) throw new InvalidOperationException(PSXSR.PageMustBelongToPdfDocument); // Debug.Assert(xpsPageIndex==0, "xpsPageIndex must be 0 at this stage of implementation."); try { FixedPage fpage = this.xpsDocument.GetDocument().GetFixedPage(xpsPageIndex); // ZipPackage pack = ZipPackage.Open(xpsFilename) as ZipPackage; Uri uri = new Uri("/Documents/1/Pages/1.fpage", UriKind.Relative); ZipPackagePart part = this.xpsDocument.Package.GetPart(uri) as ZipPackagePart; if (part != null) { using (Stream stream = part.GetStream()) using (StreamReader sr = new StreamReader(stream)) { string xml = sr.ReadToEnd(); #if true && DEBUG if (!String.IsNullOrEmpty(this.xpsDocument.Path)) { string xmlPath = IOPath.Combine(IOPath.GetDirectoryName(this.xpsDocument.Path), IOPath.GetFileNameWithoutExtension(this.xpsDocument.Path)) + ".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, fpage); } } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); throw; } }
/// <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 : } }
public void TestRenderingTypographySamples() { #if true string path = "PdfSharp/testing/SampleXpsDocuments_1_0/MXDW"; string dir = GetDirectory(path); if (dir == null) throw new FileNotFoundException("Path not found: " + path + ". Follow instructions in ../../../SampleXpsDocuments_1_0/!readme.txt to download samples from the Internet."); if (!Directory.Exists(dir)) throw new FileNotFoundException("Path not found: " + path + ". Follow instructions in ../../../SampleXpsDocuments_1_0/!readme.txt to download samples from the Internet."); string[] files = Directory.GetFiles(dir, "*Poster.xps", SearchOption.TopDirectoryOnly); if (files.Length == 0) throw new Exception("No sample file found."); foreach (string filename in files) { //if (!filename.EndsWith("CalibriPoster.xps")) // continue; Debug.WriteLine(filename); try { XpsDocument xpsDoc = XpsDocument.Open(filename); int docIndex = 0; foreach (FixedDocument doc in xpsDoc.Documents) { PdfDocument pdfDocument = new PdfDocument(); //PdfRenderer renderer = new PdfRenderer(); XpsConverter converter = new XpsConverter(pdfDocument, xpsDoc); int pageIndex = 0; foreach (FixedPage page in doc.Pages) { Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); // HACK: API is senseless PdfPage pdfPage = converter.CreatePage(pageIndex); converter.RenderPage(pdfPage, pageIndex); pageIndex++; } string pdfFilename = IOPath.GetFileNameWithoutExtension(filename); if (docIndex != 0) pdfFilename += docIndex.ToString(); pdfFilename += ".pdf"; pdfFilename = IOPath.Combine(IOPath.GetDirectoryName(filename), pdfFilename); pdfDocument.Save(pdfFilename); docIndex++; } } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } } #else //string data = "2.11592697149066,169.466971230985 7.31945717924454E-09,161.067961604689"; //TokenizerHelper helper = new TokenizerHelper(data); //string t = helper.NextTokenRequired(); //t = helper.NextTokenRequired(); //t = helper.NextTokenRequired(); //t = helper.NextTokenRequired(); string[] files = Directory.GetFiles("../../../../../testing/PdfSharp.Xps.UnitTests/Typography", "*.xps", SearchOption.AllDirectories); //files = Directory.GetFiles(@"G:\!StLa\PDFsharp-1.10\OpenSource\PDFsharp\WPF\SampleXpsDocuments_1_0\FontPoster", "*.xps", SearchOption.AllDirectories); if (files.Length == 0) throw new Exception("No sample file found."); foreach (string filename in files) { // No negative tests here if (filename.Contains("\\ConformanceViolations\\")) continue; //if (!filename.EndsWith("CalibriPoster.xps")) // continue; Debug.WriteLine(filename); try { #if true XpsDocument xpsDoc = XpsDocument.Open(filename); int docIndex = 0; foreach (FixedDocument doc in xpsDoc.Documents) { PdfDocument pdfDocument = new PdfDocument(); //PdfRenderer renderer = new PdfRenderer(); XpsConverter converter = new XpsConverter(pdfDocument, xpsDoc); int pageIndex = 0; foreach (FixedPage page in doc.Pages) { Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); // HACK: API is senseless PdfPage pdfPage = converter.CreatePage(pageIndex); converter.RenderPage(pdfPage, pageIndex); pageIndex++; } string pdfFilename = IOPath.GetFileNameWithoutExtension(filename); if (docIndex != 0) pdfFilename += docIndex.ToString(); pdfFilename += ".pdf"; pdfFilename = IOPath.Combine(IOPath.GetDirectoryName(filename), pdfFilename); pdfDocument.Save(pdfFilename); docIndex++; } #else int docIndex = 0; XpsDocument xpsDoc = XpsDocument.Open(filename); foreach (FixedDocument doc in xpsDoc.Documents) { PdfDocument pdfDoc = new PdfDocument(); PdfRenderer renderer = new PdfRenderer(); int pageIndex = 0; foreach (FixedPage page in doc.Pages) { Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); PdfPage pdfPage = renderer.CreatePage(pdfDoc, page); renderer.RenderPage(pdfPage, page); pageIndex++; } string pdfFilename = IOPath.GetFileNameWithoutExtension(filename); if (docIndex != 0) pdfFilename += docIndex.ToString(); pdfFilename += ".pdf"; pdfFilename = IOPath.Combine(IOPath.GetDirectoryName(filename), pdfFilename); pdfDoc.Save(pdfFilename); docIndex++; } #endif } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } } #endif }
public void TestExternalXpsFiles() { string path = "PdfSharp/testing/SampleXpsDocuments_1_0/"; string dir = GetDirectory(path); if (dir == null) throw new FileNotFoundException("Path not found: " + path + ". Follow instructions in ../../../SampleXpsDocuments_1_0/!readme.txt to download samples from the Internet."); if (!Directory.Exists(dir)) throw new FileNotFoundException("Path not found: " + path + ". Follow instructions in ../../../SampleXpsDocuments_1_0/!readme.txt to download samples from the Internet."); string[] files = Directory.GetFiles(dir, "*.xps", SearchOption.AllDirectories); if (files.Length == 0) throw new Exception("No sample file found. Follow instructions in ../../../SampleXpsDocuments_1_0/!readme.txt to download samples from the Internet."); foreach (string filename in files) { // No negative tests here if (filename.Contains("\\ConformanceViolations\\")) continue; Debug.WriteLine(filename); try { int docIndex = 0; XpsDocument xpsDoc = XpsDocument.Open(filename); foreach (FixedDocument doc in xpsDoc.Documents) { PdfDocument pdfDoc = new PdfDocument(); PdfRenderer renderer = new PdfRenderer(); int pageIndex = 0; foreach (FixedPage page in doc.Pages) { Debug.WriteLine(String.Format(" doc={0}, page={1}", docIndex, pageIndex)); PdfPage pdfPage = renderer.CreatePage(pdfDoc, page); renderer.RenderPage(pdfPage, page); pageIndex++; } string pdfFilename = IOPath.GetFileNameWithoutExtension(filename); if (docIndex != 0) pdfFilename += docIndex.ToString(); pdfFilename += ".pdf"; pdfFilename = IOPath.Combine(IOPath.GetDirectoryName(filename), pdfFilename); pdfDoc.Save(pdfFilename); docIndex++; } } catch (Exception ex) { Debug.WriteLine(ex.Message); GetType(); } } }
/// <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); }