public static void Run(String open_file) { Pdfix pdfix = PdfixEngine.Instance; // open doc using file stream var fileStm = pdfix.CreateFileStream(open_file, PsFileMode.kPsReadOnly); if (fileStm == null) { throw new Exception(pdfix.GetError()); } PdfDoc doc0 = pdfix.OpenDocFromStream(fileStm, ""); if (doc0 == null) { throw new Exception(pdfix.GetError()); } Console.WriteLine(@"Document open from file stream: {0} pages", doc0.GetNumPages()); doc0.Close(); // open doc using memory stream byte[] fileData = new byte[fileStm.GetSize()]; fileStm.Read(0, fileData); var memStm = pdfix.CreateMemStream(); memStm.Write(0, fileData); PdfDoc doc1 = pdfix.OpenDocFromStream(memStm, ""); if (doc1 == null) { throw new Exception(pdfix.GetError()); } Console.WriteLine(@"Document open from memory stream: {0} pages", doc1.GetNumPages()); doc1.Close(); memStm.Destroy(); // open doc using custom stream PsStreamReadProc read_proc = (offset, buffer, size, client_data) => { // read byte[] data from the source and copy to buffer var stm = (((GCHandle)client_data).Target as PsStream); byte[] buf = new byte[size]; var ret = stm.Read(offset, buf); if (!ret) { return(-1); } Marshal.Copy(buf, 0, buffer, size); return(size); }; PsStreamGetSizeProc get_size_proc = (client_data) => { // return the size of stream var stm = (((GCHandle)client_data).Target as PsStream); return(stm.GetSize()); }; var client_data_handle = GCHandle.Alloc(fileStm); var customStm = pdfix.CreateCustomStream(read_proc, (IntPtr)client_data_handle); customStm.SetGetSizeProc(get_size_proc); PdfDoc doc2 = pdfix.OpenDocFromStream(customStm, ""); if (doc2 == null) { throw new Exception(pdfix.GetError()); } Console.WriteLine(@"Document open from custom stream: {0} pages", doc2.GetNumPages()); doc2.Close(); customStm.Destroy(); client_data_handle.Free(); fileStm.Destroy(); }
public static void Run( String openPath, // source PDF document String savePath, // output PDF document String imgPath // watermark to apply ) { Pdfix pdfix = PdfixEngine.Instance; PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } // identify image format from file path PdfImageFormat format = PdfImageFormat.kImageFormatJpg; if (Path.GetExtension(imgPath).ToLower() == ".png") { format = PdfImageFormat.kImageFormatPng; } // load image file data into memory stream byte[] bytes = File.ReadAllBytes(imgPath); var memStm = pdfix.CreateMemStream(); if (memStm == null) { throw new Exception(pdfix.GetError()); } memStm.Write(0, bytes); // create XObject from the image var image_obj = doc.CreateXObjectFromImage(memStm, format); if (image_obj == null) { throw new Exception(pdfix.GetError()); } // add annotation on the first page var page = doc.AcquirePage(0); if (page == null) { throw new Exception(pdfix.GetError()); } var page_view = page.AcquirePageView(1, PdfRotate.kRotate0); if (page_view == null) { throw new Exception(pdfix.GetError()); } // rect for the new annotation PdfRect annot_rect = new PdfRect() { left = 100, right = 300, bottom = 100, top = 200 }; PdfAnnot annot = page.CreateAnnot(PdfAnnotSubtype.kAnnotStamp, annot_rect); page.AddAnnot(0, annot); // create content var content = doc.CreateContent(); // add image to content var xobjdict = image_obj.GetStreamDict(); var width = xobjdict.GetNumber("Width"); var height = xobjdict.GetNumber("Height"); var ratio = height / width; var res_width = annot_rect.right - annot_rect.left; var res_height = res_width * ratio; var center_adj = ((annot_rect.top - annot_rect.bottom) - res_height) / 2; var imageobject = content.AddNewImage(-1, image_obj, new PdfMatrix(res_width, 0, 0, res_height, 0, center_adj)); PdfGraphicState imageGs = new PdfGraphicState(); imageGs.color_state.fill_opacity = 255; imageobject.SetGState(imageGs); // create text state PdfTextState textState = new PdfTextState(); var colorSpace = doc.CreateColorSpace(PdfColorSpaceFamily.kColorSpaceDeviceRGB); var fontName = "Segoe UI"; var fn = fontName.Trim(); var sysFont = pdfix.FindSysFont(fn, 0, PdfFontCodepage.kFontDefANSICodepage); if (sysFont == null) { throw new Exception(pdfix.GetError()); } textState.font = doc.CreateFont(sysFont, PdfFontCharset.kFontAnsiCharset, 0); sysFont.Destroy(); if (textState.font == null) { throw new Exception(pdfix.GetError()); } textState.font_size = 11; textState.color_state.fill_type = PdfFillType.kFillTypeSolid; var fill_color = colorSpace.CreateColor(); fill_color.SetValue(0, 0.5f); fill_color.SetValue(1, 0.5f); fill_color.SetValue(2, 0.5f); textState.color_state.fill_color = fill_color; textState.color_state.fill_opacity = 255; textState.color_state.stroke_type = PdfFillType.kFillTypeSolid; var stroke_color = colorSpace.CreateColor(); stroke_color.SetValue(0, 0); stroke_color.SetValue(1, 0); stroke_color.SetValue(2, 0); textState.color_state.stroke_color = stroke_color; textState.color_state.stroke_opacity = 255; textState.char_spacing = 2; // add text to content var line = "test text in appearance"; var textObject = content.AddNewText(-1, textState.font, new PdfMatrix(1, 0, 0, 1, 10, 10)); if (textObject == null) { throw new Exception(pdfix.GetError()); } textObject.SetTextState(textState); textObject.SetText(line); // set annotation appearance PdsContentParams contentParams = new PdsContentParams(); contentParams.bbox = new PdfRect(annot_rect.right - annot_rect.left, 0, 0, annot_rect.top - annot_rect.bottom); contentParams.matrix = new PdfMatrix(1, 0, 0, 1, 0, 0); contentParams.form_type = 1; contentParams.flags = 2; var appearance_stream = content.ToObject(doc, contentParams); annot.SetAppearanceFromXObject(appearance_stream, PdfAnnotAppearanceMode.kAppearanceNormal); page.Release(); // save document if (!doc.Save(savePath, Pdfix.kSaveFull)) { throw new Exception(pdfix.GetError()); } doc.Close(); }
public static void Run( String openPath, // source PDF document String savePath, // output PDF document String imgPath // watermark to apply ) { Pdfix pdfix = PdfixEngine.Instance; PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } // identify image format from file path PdfImageFormat format = PdfImageFormat.kImageFormatJpg; if (Path.GetExtension(imgPath).ToLower() == ".png") { format = PdfImageFormat.kImageFormatPng; } // load image file data into memory stream byte[] bytes = File.ReadAllBytes(imgPath); var memStm = pdfix.CreateMemStream(); if (memStm == null) { throw new Exception(pdfix.GetError()); } memStm.Write(0, bytes); // create XObject from the image var image_obj = doc.CreateXObjectFromImage(memStm, format); if (image_obj == null) { throw new Exception(pdfix.GetError()); } // find or add annotation on the first page var page = doc.AcquirePage(0); if (page == null) { throw new Exception(pdfix.GetError()); } PdfAnnot annot = null;// page.GetAnnot(0); if (annot == null) { // create new annotation var page_view = page.AcquirePageView(1, PdfRotate.kRotate0); if (page_view == null) { throw new Exception(pdfix.GetError()); } // rect for the new annotation PdfRect annot_rect = new PdfRect() { left = 100, right = 300, bottom = 100, top = 200 }; annot = page.CreateAnnot(PdfAnnotSubtype.kAnnotStamp, annot_rect); page.AddAnnot(0, annot); } // set annotation appearance if (!annot.SetAppearanceFromXObject(image_obj, PdfAnnotAppearanceMode.kAppearanceNormal)) { throw new Exception(pdfix.GetError()); } page.Release(); // save document if (!doc.Save(savePath, Pdfix.kSaveFull)) { throw new Exception(pdfix.GetError()); } 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(); }