public static void Run( String email, // authorization email String licenseKey, // authorization license key String openPath, // source PDF document String savePath, // output PDF document String configPath, // configuration file PdfHtmlParams htmlParams // html conversion params ) { Pdfix pdfix = new Pdfix(); if (pdfix == null) { throw new Exception("Pdfix initialization fail"); } if (!pdfix.Authorize(email, licenseKey)) { throw new Exception(pdfix.GetError()); } PdfToHtml pdfToHtml = new PdfToHtml(); if (pdfToHtml == null) { throw new Exception("PdfToHtml initialization fail"); } if (!pdfToHtml.Initialize(pdfix)) { throw new Exception(pdfix.GetError()); } PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } // customize conversion PsFileStream stm = pdfix.CreateFileStream(configPath, PsFileMode.kPsReadOnly); if (stm != null) { PdfDocTemplate docTmpl = doc.GetDocTemplate(); if (docTmpl == null) { throw new Exception(pdfix.GetError()); } if (!docTmpl.LoadFromStream(stm, PsDataFormat.kDataFormatJson)) { throw new Exception(pdfix.GetError()); } stm.Destroy(); } // set html conversion params //htmlParams.type = PdfHtmlType.kPdfHtmlResponsive; //htmlParams.width = 1200; //htmlParams.flags |= PdfToHtml.kHtmlExportJavaScripts; //htmlParams.flags |= PdfToHtml.kHtmlExportFonts; //htmlParams.flags |= PdfToHtml.kHtmlRetainFontSize; //htmlParams.flags |= PdfToHtml.kHtmlRetainTextColor; htmlParams.flags |= PdfToHtml.kHtmlNoExternalCSS | PdfToHtml.kHtmlNoExternalJS | PdfToHtml.kHtmlNoExternalIMG | PdfToHtml.kHtmlNoExternalFONT; PdfHtmlDoc htmlDoc = pdfToHtml.OpenHtmlDoc(doc); if (htmlDoc == null) { throw new Exception(pdfix.GetError()); } if (!htmlDoc.Save(savePath, htmlParams, null, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } htmlDoc.Close(); doc.Close(); pdfToHtml.Destroy(); pdfix.Destroy(); }
public static void Run( String email, // authorization email String licenseKey, // authorization license key String openPath, // source PDF document String savePath, // output PDF document String configPath // configuration file ) { Pdfix pdfix = new Pdfix(); if (pdfix == null) { throw new Exception("Pdfix initialization fail"); } if (!pdfix.Authorize(email, licenseKey)) { throw new Exception(pdfix.GetError()); } PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } PsFileStream stm = pdfix.CreateFileStream(configPath, PsFileMode.kPsReadOnly); if (stm != null) { PdfDocTemplate docTmpl = doc.GetDocTemplate(); if (docTmpl == null) { throw new Exception(pdfix.GetError()); } docTmpl.LoadFromStream(stm, PsDataFormat.kDataFormatJson); stm.Destroy(); } // define a cancel progress callback PdfCancelProc cancel_callback = (data) => { // to cancel the process return 1 Console.WriteLine("PdfCancelProc callback was called"); return(0); }; PdfPage page = doc.AcquirePage(0); PdePageMap pageMap = page.AcquirePageMap(null, IntPtr.Zero); // define an event callback PdfEventProc event_callback = (data) => { Console.WriteLine("Page contents did change. Releasing pageMap..."); if (pageMap != null) { pageMap.Release(); pageMap = null; } }; if (!pdfix.RegisterEvent(PdfEventType.kEventPageContentsDidChange, event_callback, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } if (!doc.AddTags(cancel_callback, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } if (!doc.Save(savePath, PdfSaveFlags.kSaveFull)) { throw new Exception(pdfix.GetError()); } doc.Close(); pdfix.Destroy(); }
public static void Run( String openPath, // source PDF document String savePath, // output PDF document String configPath, // configuration file PdfHtmlParams htmlParams // html conversion params ) { Pdfix pdfix = PdfixEngine.Instance; PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } var html_conv = doc.CreateHtmlConversion(); if (html_conv == null) { throw new Exception(pdfix.GetError()); } // customize conversion PsFileStream stm = pdfix.CreateFileStream(configPath, PsFileMode.kPsReadOnly); if (stm != null) { var doc_prelight = doc.GetTemplate(); if (doc_prelight == null) { throw new Exception(pdfix.GetError()); } if (!doc_prelight.LoadFromStream(stm, PsDataFormat.kDataFormatJson)) { throw new Exception(pdfix.GetError()); } stm.Destroy(); } // set html conversion params //htmlParams.type = PdfHtmlType.kPdfHtmlResponsive; //htmlParams.width = 1200; //htmlParams.flags |= Pdfix.kHtmlExportJavaScripts; //htmlParams.flags |= Pdfix.kHtmlExportFonts; //htmlParams.flags |= Pdfix.kHtmlRetainFontSize; //htmlParams.flags |= Pdfix.kHtmlRetainTextColor; htmlParams.flags |= Pdfix.kHtmlNoExternalCSS | Pdfix.kHtmlNoExternalJS | Pdfix.kHtmlNoExternalIMG | Pdfix.kHtmlNoExternalFONT; htmlParams.image_params.format = PdfImageFormat.kImageFormatJpg; if (!html_conv.SetParams(htmlParams)) { throw new Exception(pdfix.GetError()); } if (!html_conv.Save(savePath, null, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } html_conv.Destroy(); doc.Close(); }
public static void Run( String openPath, // source PDF document String configPath ) { Pdfix pdfix = PdfixEngine.Instance; PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } var doc_preflight = doc.GetTemplate(); if (doc_preflight == null) { throw new Exception(pdfix.GetError()); } // load user-defined confguration PsFileStream stm = pdfix.CreateFileStream(configPath, PsFileMode.kPsReadOnly); if (stm != null) { var doc_prelight = doc.GetTemplate(); if (doc_prelight == null) { throw new Exception(pdfix.GetError()); } doc_prelight.LoadFromStream(stm, PsDataFormat.kDataFormatJson); stm.Destroy(); } // add reference pages to preflight for (var i = 0; i < doc.GetNumPages(); i++) { if (!doc_preflight.AddPage(i, null, null)) { throw new Exception(pdfix.GetError()); } } // run document preflight if (!doc_preflight.Update(null, null)) { throw new Exception(pdfix.GetError()); } // save preflight to stream if needed var preflight_stm = pdfix.CreateMemStream(); doc_preflight.SaveToStream(preflight_stm, PsDataFormat.kDataFormatJson, Pdfix.kSaveUncompressed); byte[] bytes = new byte[preflight_stm.GetSize()]; preflight_stm.Read(0, bytes); System.Console.Write(System.Text.Encoding.UTF8.GetString(bytes)); // output some document preflight values from the config for (var i = 0; i < doc.GetNumPages(); i++) { Console.WriteLine("Preflight results for page " + i.ToString()); var page_preflight = doc_preflight.GetPageTemplate(i); if (page_preflight == null) { throw new Exception(pdfix.GetError()); } Console.WriteLine("logical rotate: " + page_preflight.GetLogicalRotate()); Console.WriteLine("columns: " + page_preflight.GetNumColumns()); var header_box = page_preflight.GetHeaderBBox(); Console.WriteLine("header bbox: " + header_box.left + ", " + header_box.bottom + ", " + header_box.right + ", " + header_box.top); var footer_box = page_preflight.GetFooterBBox(); Console.WriteLine("footer bbox: " + footer_box.left + ", " + footer_box.bottom + ", " + footer_box.right + ", " + footer_box.top); Console.WriteLine(""); } doc.Close(); }
public static void Run( String openPath, // source PDF document String configPath, // configuration file PdfHtmlParams htmlParams // html conversion params ) { Pdfix pdfix = PdfixEngine.Instance; PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } var html_conv = doc.CreateHtmlConversion(); if (html_conv == null) { throw new Exception(pdfix.GetError()); } // customize conversion PsFileStream stm = pdfix.CreateFileStream(configPath, PsFileMode.kPsReadOnly); if (stm != null) { var doc_prelight = doc.GetTemplate(); if (doc_prelight == null) { throw new Exception(pdfix.GetError()); } if (!doc_prelight.LoadFromStream(stm, PsDataFormat.kDataFormatJson)) { throw new Exception(pdfix.GetError()); } stm.Destroy(); } //htmlParams.type = PdfHtmlType.kPdfHtmlResponsive; htmlParams.flags |= Pdfix.kHtmlNoExternalCSS | Pdfix.kHtmlNoExternalJS | Pdfix.kHtmlNoExternalIMG | Pdfix.kHtmlNoExternalFONT; //htmlParams.image_params.format = PdfImageFormat.kImageFormatJpg; //htmlParams.image_params.quality = 80; if (!html_conv.SetParams(htmlParams)) { throw new Exception(pdfix.GetError()); } var docStm = pdfix.CreateFileStream(Utils.GetAbsolutePath("output") + "/pages.html", PsFileMode.kPsTruncate); // prepare head docStm.Write(0, System.Text.Encoding.Default.GetBytes("<html>\n<head>\n<title>PDFix sample</title>\n</head>\n<body>\n")); docStm.Write(docStm.GetSize(), System.Text.Encoding.Default.GetBytes("<script>\n")); html_conv.SaveJavaScript(docStm); docStm.Write(docStm.GetSize(), System.Text.Encoding.Default.GetBytes("\n</script>\n<style>\n")); html_conv.SaveCSS(docStm); docStm.Write(docStm.GetSize(), System.Text.Encoding.Default.GetBytes("\n</style>\n")); // convert pages for (int i = 0; i < doc.GetNumPages(); i++) { if (!html_conv.AddPage(i, null, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } } if (!html_conv.SaveToStream(docStm, null, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } docStm.Write(docStm.GetSize(), System.Text.Encoding.Default.GetBytes("</body>\n</html>")); html_conv.Destroy(); docStm.Destroy(); doc.Close(); }
public static void Run( String email, // authorization email String licenseKey, // authorization license key String openPath, // source PDF document String savePath, // output folder for HTML content String configPath, // configuration file PdfHtmlParams htmlParams // html conversion params ) { Pdfix pdfix = new Pdfix(); if (pdfix == null) { throw new Exception("Pdfix initialization fail"); } if (!pdfix.Authorize(email, licenseKey)) { throw new Exception(pdfix.GetError()); } PdfToHtml pdfToHtml = new PdfToHtml(); if (pdfToHtml == null) { throw new Exception("PdfToHtml initialization fail"); } if (!pdfToHtml.Initialize(pdfix)) { throw new Exception(pdfix.GetError()); } PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } // customize conversion PsFileStream stm = pdfix.CreateFileStream(configPath, PsFileMode.kPsReadOnly); if (stm != null) { PdfDocTemplate docTmpl = doc.GetDocTemplate(); if (docTmpl == null) { throw new Exception(pdfix.GetError()); } if (!docTmpl.LoadFromStream(stm, PsDataFormat.kDataFormatJson)) { throw new Exception(pdfix.GetError()); } stm.Destroy(); } // set html conversion params //htmlParams.type = PdfHtmlType.kPdfHtmlResponsive; //htmlParams.width = 1200; //htmlParams.flags |= PdfToHtml.kHtmlExportJavaScripts; //htmlParams.flags |= PdfToHtml.kHtmlExportFonts; //htmlParams.flags |= PdfToHtml.kHtmlRetainFontSize; //htmlParams.flags |= PdfToHtml.kHtmlRetainTextColor; htmlParams.flags |= PdfToHtml.kHtmlNoExternalCSS | PdfToHtml.kHtmlNoExternalJS | PdfToHtml.kHtmlNoExternalIMG | PdfToHtml.kHtmlNoExternalFONT; PdfHtmlDoc htmlDoc = pdfToHtml.OpenHtmlDoc(doc); if (htmlDoc == null) { throw new Exception(pdfix.GetError()); } // save common js and css for all pages PsStream docCss = pdfix.CreateFileStream(savePath + "/document.css", PsFileMode.kPsTruncate); if (!pdfToHtml.SaveCSS(docCss)) { throw new Exception(pdfix.GetError()); } docCss.Destroy(); PsStream docJs = pdfix.CreateFileStream(savePath + "/document.js", PsFileMode.kPsTruncate); if (!pdfToHtml.SaveJavaScript(docJs)) { throw new Exception(pdfix.GetError()); } docJs.Destroy(); PsStream docStm = pdfix.CreateFileStream(savePath + "/document.html", PsFileMode.kPsTruncate); if (!htmlDoc.SaveDocHtml(docStm, htmlParams, null, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } docStm.Destroy(); for (int i = 0; i < doc.GetNumPages(); i++) { string pageFile = savePath + "/document_page" + i + ".html"; PsStream pageStm = pdfix.CreateFileStream(pageFile, PsFileMode.kPsTruncate); if (!htmlDoc.SavePageHtml(pageStm, htmlParams, i, null, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } pageStm.Destroy(); } htmlDoc.Close(); doc.Close(); pdfToHtml.Destroy(); pdfix.Destroy(); }