protected IPDFRemoteComponent GetRemoteComponent(IPDFComponent owner) { IPDFComponent comp = owner; while (null != comp) { if (comp is IPDFRemoteComponent && !String.IsNullOrEmpty(((IPDFRemoteComponent)comp).LoadedSource)) { return((IPDFRemoteComponent)comp); } else { comp = comp.Parent; } } IPDFDocument doc = owner.Document; if (doc is IPDFRemoteComponent) { return((IPDFRemoteComponent)doc); } else { return(null); } }
public static PDFImageData LoadImageFromUriData(string src, IPDFDocument document, IPDFComponent owner) { if (null == document) { throw new ArgumentNullException("document"); } if (null == owner) { throw new ArgumentNullException("owner"); } var dataUri = ParseDataURI(src); if (dataUri.encoding != "base64") { throw new ArgumentException("src", $"unsupported encoding {dataUri.encoding}; expected base64"); } if (dataUri.mediaType != "image/png") { throw new ArgumentException("src", $"unsupported encoding {dataUri.mediaType}; expected image/png"); } var binary = Convert.FromBase64String(dataUri.data); using (var ms = new System.IO.MemoryStream(binary)) { return(PDFImageData.LoadImageFromStream(document.GetIncrementID(owner.Type) + "data_png", ms, owner)); } }
public PDFImageData LoadImageData(IPDFDocument document, IPDFComponent owner, string path) { try { var uri = new Uri(path); var param = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped); var name = System.IO.Path.GetFileNameWithoutExtension(param); // Standard System.Drawing routines to draw a bitmap // could load an image from SQL, use parameters, whatever is needed Bitmap bmp = new Bitmap(300, 100); using (Graphics graphics = Graphics.FromImage(bmp)) { graphics.FillRectangle(new SolidBrush(Color.LightBlue), new Rectangle(0, 0, 300, 100)); graphics.DrawString(name, new Font("Times", 12), new SolidBrush(Color.Blue), PointF.Empty); graphics.Flush(); } var dir = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal); var png = System.IO.Path.Combine(dir, "Temp.png"); bmp.Save(png); PDFImageData data = PDFImageData.LoadImageFromBitmap(path, bmp, false); return(data); } catch (Exception ex) { throw new ArgumentException("The image creation failed", ex); } }
/// <summary> /// Creates a new instance of the PDFItemCollection and adds the specified items to the collection /// </summary> /// <param name="contents"></param> /// <param name="doc">The document that owns this item collection</param> public PDFItemCollection(IDictionary <string, object> contents, IPDFDocument doc) : this(doc) { if (null != contents) { foreach (string str in contents.Keys) { this.BaseAdd(str, contents[str]); } } }
/// <summary> /// Loads an image from a base 64 data image /// </summary> /// <param name="document"></param> /// <param name="owner"></param> /// <param name="path"></param> /// <returns></returns> public Scryber.Drawing.PDFImageData LoadImageData(IPDFDocument document, IPDFComponent owner, string path) { path = GetBase64FromPath(path); var binary = Convert.FromBase64String(path); using (var ms = new System.IO.MemoryStream(binary)) { return(PDFImageData.LoadImageFromStream(document.GetIncrementID(owner.Type) + "data_png", ms, owner)); } }
/// <summary> /// 1) Remove carbon black field(K) from CMYK colorspace from PDF file. /// 2) Make Ncoded PDF with Ncode image files. /// </summary> /// <param name="inputPdfFilename"></param> /// <param name="outputPdfFilename"></param> /// <param name="ncodeIamgeFilenames"></param> void RemoveK_and_AddNcode(string inputPdfFilename, string outputPdfFilename, string[] ncodeIamgeFilenames) { Console.WriteLine("2) Remove carbon black field(K) from CMYK colorspace from PDF file"); Console.WriteLine(); IPDFDocument doc = lib.openDocument(inputPdfFilename); IPDFDocument newDoc = lib.copyDocumentOnlyPageStructure(doc); for (int i = 0; i < newDoc.getPageCount(); i++) { using (IPDFPage newPage = newDoc.getPageObj(i)) { newPage.convertColorSpaceToCMY(doc); } } Console.WriteLine("3) Make Ncoded PDF with Ncode image files."); Console.WriteLine(); ///////////////////////////////////////////////////////////////////// // caution : This code will not work unless Ncode image's bpp is 1. ///////////////////////////////////////////////////////////////////// System.IO.MemoryStream[] ms = new System.IO.MemoryStream[ncodeIamgeFilenames.Length]; for (int i = 0; i < ncodeIamgeFilenames.Length; ++i) { ms[i] = new System.IO.MemoryStream(); System.Drawing.Image ss = System.Drawing.Image.FromFile(ncodeIamgeFilenames[i]); ss.Save(ms[i], System.Drawing.Imaging.ImageFormat.Tiff); } for (int j = 0; j < newDoc.getPageCount(); ++j) { using (var page = newDoc.getPageObj(j)) { double x0, y0, x1, y1; x0 = y0 = x1 = y1 = 0; page.getPageMediaBox(ref x0, ref y0, ref x1, ref y1, true); page.addImageContentOver_usingStream(ms[j], true, x0, y0, x1, y1); } ms[j].Dispose(); } newDoc.saveDocumentAs(outputPdfFilename); newDoc.closeDocument(); }
public PDFContextBase(PDFItemCollection items, PDFTraceLog log, PDFPerformanceMonitor perfmon, IPDFDocument document) { this._format = OutputFormat.PDF; this._log = log; if (null == log) { _log = new Logging.DoNothingTraceLog(Scryber.TraceRecordLevel.Off); } _shouldLogDebug = TraceRecordLevel.Diagnostic >= _log.RecordLevel; _shouldLogVerbose = TraceRecordLevel.Verbose >= _log.RecordLevel; _shouldLogMessage = TraceRecordLevel.Messages >= _log.RecordLevel; this._items = items; this._perfmon = perfmon; this._doc = document; }
private bool TryGetFont(IPDFDocument doc, PDFContextBase context, out PDFFontDefinition definition) { System.Drawing.FontStyle style = System.Drawing.FontStyle.Regular; if (this.FontBold) { style |= System.Drawing.FontStyle.Bold; } if (this.FontItalic) { style |= System.Drawing.FontStyle.Italic; } string name = this.FontFamily.FamilyName; PDFFontFactory.TryEnsureFont(doc, context, this.Source, name, style, out definition); return(null != definition); }
internal PDFContextStyleBase(Styles.StyleStack stylesstack, PDFItemCollection items, PDFTraceLog log, PDFPerformanceMonitor perfmon, IPDFDocument document) : base(items, log, perfmon, document) { this._stylestack = stylesstack; }
public PDFInitContext(PDFItemCollection items, PDFTraceLog log, PDFPerformanceMonitor perfmon, IPDFDocument document) : base(items, log, perfmon, document) { }
/// <summary> /// Creates a new PDFDataContext with the item collection, trace log, and data stack /// </summary> /// <param name="items"></param> /// <param name="log"></param> /// <param name="stack"></param> public PDFDataContext(PDFItemCollection items, PDFTraceLog log, PDFPerformanceMonitor perfmon, PDFDataStack stack, IPDFDocument document) : base(items, log, perfmon, document) { this._datastack = stack; }
// // IPDFTemplate Instantiate method. // public IEnumerable <IPDFComponent> Instantiate(int index, IPDFComponent owner) { if (!_initialised) { this.InitTemplate(this.XmlContent, this.NamespacePrefixMappings); } if (null == owner) { throw new ArgumentNullException("owner"); } using (System.IO.StringReader sr = new System.IO.StringReader(this._toparse)) { //Get the closest remote component IPDFRemoteComponent remote = this.GetRemoteComponent(owner); IPDFDocument doc = owner.Document; IPDFComponent comp; if (doc is IPDFTemplateParser) { comp = ((IPDFTemplateParser)doc).ParseTemplate(remote, sr); } else { throw RecordAndRaise.NullReference(Errors.ParentDocumentMustBeTemplateParser); } if (this.IsBlock) { if (!(comp is TemplateBlockInstance)) { throw RecordAndRaise.InvalidCast(Errors.CannotConvertObjectToType, comp.GetType(), typeof(TemplateBlockInstance)); } TemplateBlockInstance template = (TemplateBlockInstance)comp; if (null != this.Style && (template is IPDFStyledComponent)) { this.Style.MergeInto((template as IPDFStyledComponent).Style); } template.StyleClass = this.StyleClass; template.ElementName = this.ElementName; } else { if (!(comp is TemplateInstance)) { throw RecordAndRaise.InvalidCast(Errors.CannotConvertObjectToType, comp.GetType(), typeof(TemplateInstance)); } TemplateInstance template = (TemplateInstance)comp; if (null != this.Style && (template is IPDFStyledComponent)) { this.Style.MergeInto((template as IPDFStyledComponent).Style); } template.StyleClass = this.StyleClass; template.ElementName = this.ElementName; } if (comp is Component && owner is Component && this.UseDataStyleIdentifier) { var stem = this.DataStyleStem; if (string.IsNullOrEmpty(stem)) { stem = ((Component)owner).UniqueID; } DataStyleIdentifierVisitor visitor = new DataStyleIdentifierVisitor(stem, 1); visitor.PushToComponents(comp as Component); } List <IPDFComponent> all = new List <IPDFComponent>(1); all.Add(comp); return(all); } }
// // .ctors // #region public PDFDataContext(PDFItemCollection items, PDFTraceLog log) /// <summary> /// Creates a new PDFDataContext with the item collection and trace log /// </summary> /// <param name="items"></param> /// <param name="log"></param> public PDFDataContext(PDFItemCollection items, PDFTraceLog log, PDFPerformanceMonitor perfmon, IPDFDocument document) : this(items, log, perfmon, new PDFDataStack(), document) { }
internal PDFRenderContext(DrawingOrigin origin, int pageCount, PDFOutputFormatting format, Styles.StyleStack stack, PDFItemCollection items, PDFTraceLog log, PDFPerformanceMonitor perfmon, IPDFDocument document) : base(stack, items, log, perfmon, document) { this._origin = origin; this._offset = new PDFPoint(); this._space = new PDFSize(); this._pgCount = pageCount; this._pgindex = 0; this._format = format; }
// // .ctor // public PDFRenderContext(DrawingOrigin origin, int pageCount, PDFOutputFormatting format, Styles.Style root, PDFItemCollection items, PDFTraceLog log, PDFPerformanceMonitor perfmon, IPDFDocument document) : this(origin,pageCount,format, new Scryber.Styles.StyleStack(root), items, log, perfmon, document) { }
public PDFLayoutContext(Style style, PDFOutputFormatting format, PDFItemCollection items, PDFTraceLog log, PDFPerformanceMonitor perfmon, IPDFDocument document) : base(new StyleStack(style), items, log, perfmon, document) { this._format = format; }
/// <summary> /// 1) Remove carbon black field(K) from CMYK colorspace from PDF file. /// 2) Make Ncoded PDF with Ncode image files. /// </summary> /// <param name="inputPdfFilename"></param> /// <param name="outputPdfFilename"></param> /// <param name="ncodeIamgeFilenames"></param> void RemoveK_and_AddNcode_from_Image(string inputPdfFilename, string outputPdfFilename, string[] ncodeIamgeFilenames) { IPDFDocument doc = lib.openDocument(inputPdfFilename); IPDFDocument newDoc = lib.copyDocument(doc); if (doc == null) { Console.WriteLine(" Cannot open input PDF file."); return; } if (doc.getPageCount() != ncodeImageFilename.Length) { Console.WriteLine(" Page count is not correct"); Console.WriteLine(" input PDF pages : " + doc.getPageCount().ToString()); Console.WriteLine(" Ncode image pages : " + ncodeImageFilename.Length.ToString()); return; } Console.WriteLine(); Console.WriteLine("1) Removing carbon black field(K) from CMYK colorspace from PDF file"); Console.WriteLine(" ! This step does not work if you have not entered the correct libKey."); Console.WriteLine(); if (newDoc.controller().IsLibKeyOk() == true) { for (int i = 0; i < newDoc.getPageCount(); i++) { using (IPDFPage newPage = newDoc.getPageObj(i)) { if (newPage.convertColorSpaceToCMY(doc, 200) == false) { Console.WriteLine(newPage.lastErrorMsg()); return; } } } } Console.WriteLine("2) Making Ncoded PDF with Ncode image files."); Console.WriteLine(); ///////////////////////////////////////////////////////////////////// // caution : This code will not work unless Ncode image's bpp is 1. ///////////////////////////////////////////////////////////////////// System.IO.MemoryStream[] ms = new System.IO.MemoryStream[ncodeIamgeFilenames.Length]; for (int i = 0; i < ncodeIamgeFilenames.Length; ++i) { ms[i] = new System.IO.MemoryStream(); System.Drawing.Image ss = System.Drawing.Image.FromFile(ncodeIamgeFilenames[i]); ss.Save(ms[i], System.Drawing.Imaging.ImageFormat.Tiff); using (var page = newDoc.getPageObj(i)) { double x0, y0, x1, y1; x0 = y0 = 0; x1 = ss.Width * 72 / 600; y1 = ss.Height * 72 / 600; if (page.addImageContentOver_usingStream(ms[i], true, x0, y0, x1, y1) == false) { Console.WriteLine(page.lastErrorMsg()); return; } } ms[i].Dispose(); } newDoc.saveDocumentAs(outputPdfFilename); newDoc.closeDocument(); doc.closeDocument(); }
public Program() { // Initializing libraries Console.WriteLine("//////////////////////////"); Console.WriteLine(" Initializing libraries"); Console.WriteLine("//////////////////////////"); Console.WriteLine(); sdk = new CNcodeSDK(); lib = new PDFControl(); // This is sample app key for testing. // If you have your app key, enter here. string appKey_NcodeSDK = "184b265d3aed5ccfab05c6b5167f3"; if (!sdk.Init(appKey_NcodeSDK)) { Console.WriteLine("Initializing Ncode SDK failed."); return; } else { Console.WriteLine("Ncode SDK version : " + sdk.GetVersion()); Console.WriteLine(); } // Enter resource folder path string libPath = Directory.GetCurrentDirectory(); // This libKey is not the license key. // Please refer below description of Datalogics' sample code. // This libKey is needed for remove K from CMYK color space. // If you don't need remove K, leave libKey blank. // Datalogics description /* You may find that you receive exceptions when you attempt to open * PDF files that contain permissions restrictions on content or image * extraction. This is due to the APIs used for viewing: these can * also be used in other contexts for content extraction or enabling * save-as-image capabilities. If you are making a PDF file viewer and * you encounter this situation, please contact your support * representative or [email protected] to request a key to enable * bypassing this restriction check. */ string libKey = ""; if (!lib.init(libPath, libKey)) { Console.WriteLine("Initializing Adobe PDF lib failed."); return; } Console.WriteLine("///////////////////"); Console.WriteLine(" Creating Ncode"); Console.WriteLine("///////////////////"); Console.WriteLine(); IPDFDocument doc = lib.openDocument("input_sample.pdf"); int pageCount = doc.getPageCount(); ncodeImageFilename = new string[pageCount]; GenerateNcode(pageCount); Console.WriteLine("////////////////////////"); Console.WriteLine(" Creating Ncoded PDF"); Console.WriteLine("////////////////////////"); RemoveK_and_AddNcode_from_Image("input_sample.pdf", "output.pdf", ncodeImageFilename); lib.libraryCleanUp(); Console.WriteLine(); Console.WriteLine("-- Complete --"); Console.ReadLine(); }
public PDFCreator(IPDFDocument document) { this.document = document ?? throw new System.ArgumentNullException(nameof(document)); System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); }