/// <summary> /// init /// </summary> public void Init(PdfDictionary functionDict) { var domainArray = functionDict.Elements.GetArray(PdfKeys.Domain); var rangeArray = functionDict.Elements.GetArray(PdfKeys.Range); var functionsArray = functionDict.Elements.GetArray(PdfKeys.Functions); var encodeArray = functionDict.Elements.GetArray(PdfKeys.Encode); var e0 = encodeArray.Elements.GetReal(0); var e1 = encodeArray.Elements.GetReal(1); // for Illustrator it doesn't make sense to revert only one function // we assume either none or all functions are reverted reverse = e0 > e1; var boundsArray = functionDict.Elements.GetArray(PdfKeys.Bounds); bounds = PdfUtilities.CreateDoubleList(boundsArray); bounds.Insert(0, domainArray.Elements.GetReal(0)); bounds.Add(domainArray.Elements.GetReal(1)); functions = new List <IFunction>(); for (int i = 0; i < functionsArray.Elements.Count; i++) { var fDict = functionsArray.Elements.GetDictionary(i); var function = FunctionManager.ReadFunction(fDict); functions.Add(function); } }
public void GetPdfPageCountTest() { string inputPdfFile = "samples/vietsample1.pdf"; int expResult = 2; int result = PdfUtilities.GetPdfPageCount(inputPdfFile); Assert.AreEqual(expResult, result); }
private void backgroundWorkerMergePdf_DoWork(object sender, DoWorkEventArgs e) { ArrayList args = (ArrayList)e.Argument; string[] inputFiles = (string[])args[0]; string outputFile = (string)args[1]; PdfUtilities.MergePdf(inputFiles, outputFile); e.Result = outputFile; }
private void backgroundWorkerConvertPdf_DoWork(object sender, DoWorkEventArgs e) { ArrayList args = (ArrayList)e.Argument; string inputFile = (string)args[0]; string targetFile = (string)args[1]; string outputTiffFile = PdfUtilities.ConvertPdf2Tiff(inputFile); File.Delete(targetFile); File.Move(outputTiffFile, targetFile); e.Result = targetFile; }
private void backgroundWorkerConvertPdf_DoWork(object sender, DoWorkEventArgs e) { string[] inputFiles = (string[])e.Argument; foreach (string inputFile in inputFiles) { string outputTiffFile = PdfUtilities.ConvertPdf2Tiff(inputFile); string targetFile = Path.Combine(Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile) + ".tif"); File.Delete(targetFile); File.Move(outputTiffFile, targetFile); e.Result = targetFile; } }
/// <summary> /// init /// </summary> public void Init(PdfDictionary functionDict) { var domainArray = functionDict.Elements.GetArray(PdfKeys.Domain); //var rangeArray = functionDict.Elements.GetArray(PdfKeys.Range); var c0Array = functionDict.Elements.GetArray(PdfKeys.C0); var c1Array = functionDict.Elements.GetArray(PdfKeys.C1); var n = functionDict.Elements.GetReal(PdfKeys.N); domain0 = domainArray.Elements.GetReal(0); domain1 = domainArray.Elements.GetReal(1); c0 = PdfUtilities.CreateDoubleList(c0Array); c1 = PdfUtilities.CreateDoubleList(c1Array); }
private void combineFilesConvertedToPdf_Ordered() { ModalOverlayIsVisible = false; SetFileOrderModalIsVisible = false; UpdateLabelPdf = ""; try { var combined_file_names = new List <string>(); var combine_files = FilesConvertedToPdf_Ordered.ToList(); var ticket_atty = FilesConvertedToPdf.Where(x => !string.IsNullOrEmpty(x.TicketPlusAttorney)).Select(x => x.TicketPlusAttorney).FirstOrDefault(); var new_file_name = PdfUtilities.GenerateFilenameForNewPdf(DestinationFolderConvertedFiles, "CombinedPdf", ticket_atty); if (StaticSystemTests.IsAdobePdfPrinterAvailable()) { // create combined pdf Models.MergePdf.SimpleMergedPdf simpleMergedPdf; if (!string.IsNullOrEmpty(new_file_name)) { simpleMergedPdf = new Models.MergePdf.SimpleMergedPdf(combine_files, "Acrobat", true, new_file_name); } else { simpleMergedPdf = new Models.MergePdf.SimpleMergedPdf(combine_files, "Acrobat", true); } // add new file to FilesConvertedToPdf collection if (System.IO.File.Exists(simpleMergedPdf?.NewCombinedFile)) { if (!FilesConvertedToPdf.Any(x => x.FullName == simpleMergedPdf.NewCombinedFile)) { FilesConvertedToPdf.Add(new CockleFilePdf(simpleMergedPdf.NewCombinedFile, SourceFileTypeEnum.Combined_Pdf)); UpdateLabelPdf = "Created: " + simpleMergedPdf.NewCombinedFile; } } } else // using PdfSharp or iTextSharp ??? { //combined_file_name = new SimpleMergedPdf(SelectedPdfFiles, "PdfSharp").NewCombinedFile; //var combined_file = new SimpleMergedPdf(SelectedPdfFiles, "iTextSharp").NewFileName; } } catch (Exception ex) { Console.Write(ex.Message); UpdateLabelPdf = ex.Message; } }
public void Init(PdfDictionary functionDict) { var domainArray = functionDict.Elements.GetArray(PdfKeys.Domain); var rangeArray = functionDict.Elements.GetArray(PdfKeys.Range); domains = PdfUtilities.CreateDoubleList(domainArray); ranges = PdfUtilities.CreateDoubleList(rangeArray); script = functionDict.Stream.ToString(); script = script.Trim(); if (script[0] == '{') { script = script.Substring(1, script.Length - 2); } }
/// <summary> /// Get a brush descriptor /// </summary> public IBrushDescriptor GetBrushDescriptor(CSequence operands, Matrix matrix, double alpha) { CSequence newSequence = new CSequence(); var numberColorValues = baseColorSpace.GetNumberOfValuesPerColor(); int index = LimitIndexToBoundaries(PdfUtilities.GetInteger(operands[0])) * numberColorValues; for (int i = 0; i < numberColorValues; i++) { var real = new CReal(); real.Value = lookup[index + i]; newSequence.Add(real); } var brushDescriptor = baseColorSpace.GetBrushDescriptor(newSequence, matrix, alpha); return(new IndexedBrushDescriptor(brushDescriptor)); }
/// <summary> /// Performs OCR for bulk/batch and console operations. /// </summary> /// <param name="imageFile">Image file</param> /// <param name="outputFile">Output file without extension</param> /// <param name="langCode">language code</param> /// <param name="pageSegMode">page segmentation mode</param> /// <param name="outputFormat">format of output file. Possible values: <code>text</code>, <code>text+</code> (with post-corrections), <code>hocr</code></param> /// <param name="deskew">deskew</param> public static void PerformOCR(string imageFile, string outputFile, string langCode, string pageSegMode, string outputFormat, bool deskew) { DirectoryInfo dir = Directory.GetParent(outputFile); if (dir != null && !dir.Exists) { dir.Create(); } bool postprocess = "text+" == outputFormat; OCR <Image> ocrEngine = new OCRImages(); ocrEngine.PageSegMode = pageSegMode; ocrEngine.Language = langCode; ocrEngine.OutputFormat = outputFormat.Replace("+", string.Empty); ocrEngine.OutputFile = outputFile; ocrEngine.Deskew = deskew; // convert PDF to TIFF if (imageFile.ToLower().EndsWith(".pdf")) { imageFile = PdfUtilities.ConvertPdf2Tiff(imageFile); } ocrEngine.ProcessFile(imageFile); // post-corrections for text+ output if (postprocess) { string filename = outputFile + ".txt"; string result = File.ReadAllText(filename); // postprocess to correct common OCR errors result = Processor.PostProcess(result, langCode); // correct letter cases result = TextUtilities.CorrectLetterCases(result); using (StreamWriter sw = new StreamWriter(filename, false, new System.Text.UTF8Encoding())) { sw.Write(result); } } }
/// <summary> /// init /// </summary> public void Init(PdfDictionary patternDict) { var shadingDict = patternDict.Elements.GetDictionary(PdfKeys.Shading); shading = ShadingManager.ReadShading(shadingDict); var matrixArray = patternDict.Elements.GetArray(PdfKeys.Matrix); if (matrixArray != null) { matrix = PdfUtilities.GetMatrix(matrixArray); } else { matrix = Matrix.Identity; } // I think we don't need to handle the extGState here // because it can't change anything we are interested in var extGStateDict = patternDict.Elements.GetDictionary(PdfKeys.ExtGState); }
/// <summary> /// Sets the dash pattern /// </summary> private void SetDashPattern(CSequence operands) { var dashArray = (CArray)operands[0]; var dashPhase = PdfUtilities.GetDouble(operands[1]); if (dashArray.Count == 0) { currentGraphicsState.Dashes = null; currentGraphicsState.DashOffset = 0; return; } var dashes = new List <double>(); foreach (var val in dashArray) { var dbl = MatrixUtilities.TransformScale(PdfUtilities.GetDouble(val), currentGraphicsState.CurrentTransformationMatrix); dashes.Add(dbl); } currentGraphicsState.Dashes = dashes; currentGraphicsState.DashOffset = dashPhase; }
private void backgroundWorkerSplitPdf_DoWork(object sender, DoWorkEventArgs e) { SplitPdfArgs args = (SplitPdfArgs)e.Argument; if (args.Pages) { PdfUtilities.SplitPdf(args.InputFilename, args.OutputFilename, args.FromPage, args.ToPage); } else { string outputFilename = String.Empty; if (args.OutputFilename.EndsWith(".pdf")) { outputFilename = args.OutputFilename.Substring(0, args.OutputFilename.LastIndexOf(".pdf")); } int pageCount = PdfUtilities.GetPdfPageCount(args.InputFilename); if (pageCount == 0) { throw new ApplicationException("Split PDF failed."); } int pageRange = Int32.Parse(args.NumOfPages); int startPage = 1; while (startPage <= pageCount) { int endPage = startPage + pageRange - 1; string outputFile = outputFilename + startPage + ".pdf"; PdfUtilities.SplitPdf(args.InputFilename, outputFile, startPage.ToString(), endPage.ToString()); startPage = endPage + 1; } } e.Result = args.OutputFilename; }
/// <summary> /// Run the XObject with the given name /// </summary> private void RunXObject(string name) { var xobjectDict = xObjectManager.GetXObject(name); var subType = xobjectDict.Elements.GetName(PdfKeys.Subtype); if (subType != PdfKeys.Form) { return; } var cloneCurrentGraphicsState = currentGraphicsState.Clone(); var matrixArray = xobjectDict.Elements.GetArray(PdfKeys.Matrix); Matrix matrix = Matrix.Identity; if (matrixArray != null) { matrix = PdfUtilities.GetMatrix(matrixArray); cloneCurrentGraphicsState.TransformationMatrix *= matrix; } cloneCurrentGraphicsState.FillAlpha.Layer = 1.0; cloneCurrentGraphicsState.FillAlpha.Object = 1.0; cloneCurrentGraphicsState.StrokeAlpha.Layer = 1.0; cloneCurrentGraphicsState.StrokeAlpha.Object = 1.0; cloneCurrentGraphicsState.SoftMask = null; CSequence sequence = ContentReader.ReadContent(xobjectDict.Stream.UnfilteredValue); var interpreter = new ContentInterpreter(); var group = interpreter.Run(xobjectDict, sequence, cloneCurrentGraphicsState); // do some optimizations that the post-processor cannot do if (group.Children.Count == 1 && group.Clip == null && !DoubleUtilities.IsEqual(currentGraphicsState.FillAlpha.Object, 1.0)) { // the layer has only 1 child and the layer has an opacity set other than 1 -> // recreate the layer but with the layer opacity set which gets "added" to each single object // on that layer. Because there is only 1 object the result is the same as if the object sits // on a semi transparent layer. That saves a group for a single object. cloneCurrentGraphicsState = currentGraphicsState.Clone(); if (matrixArray != null) { cloneCurrentGraphicsState.TransformationMatrix *= matrix; } cloneCurrentGraphicsState.FillAlpha.Layer = currentGraphicsState.FillAlpha.Object; cloneCurrentGraphicsState.StrokeAlpha.Layer = currentGraphicsState.StrokeAlpha.Object; group = interpreter.Run(xobjectDict, sequence, cloneCurrentGraphicsState); graphicGroup.Children.Add(group.Children[0]); } else { group.Opacity = currentGraphicsState.FillAlpha.Object; graphicGroup.Children.Add(group); } }
private async void executeCenterPdf() { ModalOverlayIsVisible = false; CoverLengthSelectorIsVisible = false; UpdateLabelPdf = ""; try { // get new file name var cv_type = CenteredCoverType.NotSet; var custom_filename_text = string.Empty; if (BookletSelected_CenterPdf) { cv_type = CenteredCoverType.Booklet; custom_filename_text = "CenteredOnBookletSizedPage"; } else if (LetterSelected_CenterPdf) { cv_type = CenteredCoverType.Letter; custom_filename_text = "CenteredOnLetterSizedPage"; } var ticket_atty = FilesConvertedToPdf.Where(x => !string.IsNullOrEmpty(x.TicketPlusAttorney)).Select(x => x.TicketPlusAttorney).FirstOrDefault(); var new_file_name = PdfUtilities.GenerateFilenameForNewPdf(DestinationFolderConvertedFiles, custom_filename_text, ticket_atty); // typeset -- upper left if (UpperLeftPosition_CenterPdf) { // this is a simple typeset centering, done with the one step process int?cv_len = null; if (HasCover_CenterPdf) { // two ways to get cover length: 1) check other files in directory, 2) iText method if (FilesConvertedToPdf.Any(x => null != x.CoverLength)) { cv_len = FilesConvertedToPdf.Where(x => null != x.CoverLength).FirstOrDefault().CoverLength; } else { cv_len = PdfCropAndNUp.StaticUtils.GetCoverLength_FirstPageTypesetPdf(SelectedPdfFile.FullName); } } var c = await System.Threading.Tasks.Task.Run(() => new Models.CenterPdfText.CenterTypesetPdf(SelectedPdfFile, cv_len, new_file_name, cv_type)); if (null != c && null != c.NewFileCreated && System.IO.File.Exists(c.NewFileCreated.FullName)) { if (!FilesConvertedToPdf.Any(x => x.FullName == c.NewFileCreated.FullName)) { FilesConvertedToPdf.Add(c.NewFileCreated); } UpdateLabelPdf = "Created: " + System.IO.Path.GetFileNameWithoutExtension(c.NewFileCreated.FullName); } } // already centered else if (CenterPosition_CenterPdf) { // if already centered, no need to separate out cover Models.CenterPdfText.CenterCameraReadyBrief ctr = null; if (BookletSelected_CenterPdf) { ctr = await System.Threading.Tasks.Task.Run(() => new Models.CenterPdfText.CenterCameraReadyBrief(SelectedPdfFile, new_file_name, CenteredCoverType.Booklet)); } else if (LetterSelected_CenterPdf) { ctr = await System.Threading.Tasks.Task.Run(() => new Models.CenterPdfText.CenterCameraReadyBrief(SelectedPdfFile, new_file_name, CenteredCoverType.Letter)); } if (null != ctr && null != ctr.NewFileCreated && System.IO.File.Exists(ctr.NewFileCreated.FullName)) { if (!FilesConvertedToPdf.Any(x => x.FullName == ctr.NewFileCreated.FullName)) { FilesConvertedToPdf.Add(ctr.NewFileCreated); UpdateLabelPdf = "Created: " + System.IO.Path.GetFileNameWithoutExtension(ctr.NewFileCreated.FullName); } } } // need to develop this: method to crop (find text boundaries) and center else if (NotCentered_CenterPdf) { } } catch (Exception ex) { Console.Write(ex.Message); UpdateLabelPdf = ex.Message; } }
/// <summary> /// imposePdf() function: /// The only source is the modal SelectImpositionDetails.xaml, /// and the RunImposePdf Command. /// The imposition process is controlled by a group of /// boolean properties: /// 1) IsTypeset: Upper Left Corner Document Only /// All Centered Briefs are considered Camera Ready /// 2) IsSaddleStitchPdf: True or False (if false: IsPerfectBindPdf) /// 3) HasCover: self explanatory /// 4) BlankPagesAdded: Works as a Gatekeeper: User cannot run function /// without clicking Yes /// /// Function uses PDFCropAndNUp: /// How many functions in the SVM others here use this? /// /// Problem: When creating imposed files -- only CR? -- /// user may click NoCover, but program creates a cover anyway /// (one that won't open) /// Solved: Solved this issue by simply doing a null test on the result. /// /// /// /// </summary> private void imposePdf() { ModalOverlayIsVisible = false; SelectImpositionDetailsIsVisible = false; UpdateLabelPdf = ""; var new_files_created = new List <CockleFilePdf>(); try { if (IsTypesetPdf) { string[] new_file_name = null; var num_pages = new iTextSharp.text.pdf.PdfReader(SelectedPdfFile.FullName).NumberOfPages; new_file_name = generateImposedTypesetPdfFilename(num_pages); ImposeSingleTypesetPdf imposed_doc = null; if (HasCoverPdf && IsPerfectBindPdf && num_pages > 1) { // two docs // cover first: extract page 1 using (var file_stream = PdfUtilities.ConvertPdfFileToStream(SelectedPdfFile.FullName)) { // problem here: cv_stream is dying !!! using (var cv_stream = extractPagesFromPerfectBindStream(file_stream, 1, 1)) { imposed_doc = new ImposeSingleTypesetPdf(cv_stream, IsSaddleStitchPdf ? TypeOfBindEnum.SaddleStitch : TypeOfBindEnum.PerfectBind, hasCover: true); using (var fs = new System.IO.FileStream(new_file_name[0], System.IO.FileMode.Create, System.IO.FileAccess.Write)) { fs.Write(imposed_doc.NewDocMemStream.ToArray(), 0, imposed_doc.NewDocMemStream.ToArray().Length); } } using (var br_stream = PdfUtilities.ExtractPdfPagesToStream(file_stream, 2, num_pages)) { imposed_doc = new ImposeSingleTypesetPdf(br_stream, IsSaddleStitchPdf ? TypeOfBindEnum.SaddleStitch : TypeOfBindEnum.PerfectBind, hasCover: false); using (var fs = new System.IO.FileStream(new_file_name[1], System.IO.FileMode.Create, System.IO.FileAccess.Write)) { fs.Write(imposed_doc.NewDocMemStream.ToArray(), 0, imposed_doc.NewDocMemStream.ToArray().Length); } } } //new_files_created.Add(new CockleFilePdf( new_file_name[0]); } else { imposed_doc = new ImposeSingleTypesetPdf(SelectedPdfFile, IsSaddleStitchPdf ? TypeOfBindEnum.SaddleStitch : TypeOfBindEnum.PerfectBind, HasCoverPdf); using (var fs = new System.IO.FileStream(new_file_name[0], System.IO.FileMode.Create, System.IO.FileAccess.Write)) { fs.Write(imposed_doc.NewDocMemStream.ToArray(), 0, imposed_doc.NewDocMemStream.ToArray().Length); } } } else if (IsCameraReadyCenteredPdf) { var new_files = PdfCropAndNUp.PdfCookbook.CreatePrintReadyFile( SelectedPdfFile.FullName, HasCoverPdf, IsSaddleStitchPdf ? PdfCropAndNUp.PdfBindTypeEnum.SaddleStitch : PdfCropAndNUp.PdfBindTypeEnum.PerfectBind, null, IsCameraReadyCenteredPdf, IsCameraReadyOffsetPdf ); if (new_files.All(x => System.IO.File.Exists(x.FullName))) { UpdateLabelPdf = "Created:"; foreach (var x in new_files) { if (!FilesConvertedToPdf.Any(y => x.FullName == y.FullName)) { FilesConvertedToPdf.Add(new CockleFilePdf(x.FullName, SourceFileTypeEnum.Imposed_Pdf)); } UpdateLabelPdf += " " + System.IO.Path.GetFileNameWithoutExtension(x.FullName); } } } else if (IsCameraReadyOffsetPdf) { var new_files = PdfCropAndNUp.PdfCookbook.CreatePrintReadyFile( SelectedPdfFile.FullName, HasCoverPdf, IsSaddleStitchPdf ? PdfCropAndNUp.PdfBindTypeEnum.SaddleStitch : PdfCropAndNUp.PdfBindTypeEnum.PerfectBind, null, IsCameraReadyCenteredPdf, IsCameraReadyOffsetPdf ); if (new_files.All(x => System.IO.File.Exists(x.FullName))) { UpdateLabelPdf = "Created:"; new_files.ForEach(x => { FilesConvertedToPdf.Add(new CockleFilePdf(x.FullName, SourceFileTypeEnum.Imposed_Pdf)); UpdateLabelPdf += " " + System.IO.Path.GetFileNameWithoutExtension(x.FullName); }); } } } catch (Exception ex) { UpdateLabelPdf = ex.Message; } }
/// <summary> /// Parse a single PDF page /// </summary> public GraphicGroup Run(PdfDictionary form, CSequence sequence, GraphicsState graphicsState) { this.returnGraphicGroup = new GraphicGroup(); graphicGroup = returnGraphicGroup; graphicsStateStack = new Stack <GraphicsState>(); fontState = new FontState(); textVectorizer = new TextVectorizer(); currentGraphicsState = graphicsState; Init(form); InitColor(); Point currentPoint = new Point(0, 0); GraphicMoveSegment lastMove = null; ResetCurrentGeometry(); for (int index = 0; index < sequence.Count; index++) { var contentOperator = sequence[index] as COperator; switch (contentOperator.OpCode.OpCodeName) { // path construction operators // rectangle case OpCodeName.re: { if (currentGeometry == null) { currentGeometry = new GraphicPathGeometry(); } var x = PdfUtilities.GetDouble(contentOperator.Operands[0]); var y = PdfUtilities.GetDouble(contentOperator.Operands[1]); var width = PdfUtilities.GetDouble(contentOperator.Operands[2]); var height = PdfUtilities.GetDouble(contentOperator.Operands[3]); var point1 = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); var point2 = MatrixUtilities.TransformPoint(x + width, y + height, currentGraphicsState.CurrentTransformationMatrix); var move = new GraphicMoveSegment { StartPoint = point1 }; currentGeometry.Segments.Add(move); var lineTo = new GraphicLineSegment { To = new Point(point2.X, point1.Y) }; currentGeometry.Segments.Add(lineTo); lineTo = new GraphicLineSegment { To = new Point(point2.X, point2.Y) }; currentGeometry.Segments.Add(lineTo); lineTo = new GraphicLineSegment { To = new Point(point1.X, point2.Y) }; currentGeometry.Segments.Add(lineTo); move.IsClosed = true; lastMove = move; currentPoint = point1; break; } // move to case OpCodeName.m: { if (currentGeometry == null) { currentGeometry = new GraphicPathGeometry(); } var x = PdfUtilities.GetDouble(contentOperator.Operands[0]); var y = PdfUtilities.GetDouble(contentOperator.Operands[1]); var point = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); var move = new GraphicMoveSegment { StartPoint = point }; currentGeometry.Segments.Add(move); lastMove = move; currentPoint = point; break; } // line to case OpCodeName.l: { var x = PdfUtilities.GetDouble(contentOperator.Operands[0]); var y = PdfUtilities.GetDouble(contentOperator.Operands[1]); var point = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); var lineTo = new GraphicLineSegment { To = point }; currentGeometry.Segments.Add(lineTo); currentPoint = point; break; } // cubic bezier case OpCodeName.c: { var bezier = new GraphicCubicBezierSegment(); currentGeometry.Segments.Add(bezier); var x = PdfUtilities.GetDouble(contentOperator.Operands[0]); var y = PdfUtilities.GetDouble(contentOperator.Operands[1]); bezier.ControlPoint1 = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); x = PdfUtilities.GetDouble(contentOperator.Operands[2]); y = PdfUtilities.GetDouble(contentOperator.Operands[3]); bezier.ControlPoint2 = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); x = PdfUtilities.GetDouble(contentOperator.Operands[4]); y = PdfUtilities.GetDouble(contentOperator.Operands[5]); bezier.EndPoint = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); currentPoint = bezier.EndPoint; break; } // quadratic bezier case OpCodeName.v: { var bezier = new GraphicCubicBezierSegment(); currentGeometry.Segments.Add(bezier); bezier.ControlPoint1 = currentPoint; var x = PdfUtilities.GetDouble(contentOperator.Operands[0]); var y = PdfUtilities.GetDouble(contentOperator.Operands[1]); bezier.ControlPoint2 = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); x = PdfUtilities.GetDouble(contentOperator.Operands[2]); y = PdfUtilities.GetDouble(contentOperator.Operands[3]); bezier.EndPoint = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); currentPoint = bezier.EndPoint; break; } // quadratic bezier case OpCodeName.y: { var bezier = new GraphicCubicBezierSegment(); currentGeometry.Segments.Add(bezier); var x = PdfUtilities.GetDouble(contentOperator.Operands[0]); var y = PdfUtilities.GetDouble(contentOperator.Operands[1]); bezier.ControlPoint1 = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); x = PdfUtilities.GetDouble(contentOperator.Operands[2]); y = PdfUtilities.GetDouble(contentOperator.Operands[3]); bezier.ControlPoint2 = MatrixUtilities.TransformPoint(x, y, currentGraphicsState.CurrentTransformationMatrix); bezier.EndPoint = bezier.ControlPoint2; currentPoint = bezier.EndPoint; break; } // path painting operators // end the path without filling and stroking case OpCodeName.n: { ResetCurrentGeometry(); break; } // set clipping path case OpCodeName.W: case OpCodeName.Wx: { currentGraphicsState.ClippingPath = currentGeometry; graphicGroup = new GraphicGroup(); graphicGroup.Clip = currentGeometry; returnGraphicGroup.Children.Add(graphicGroup); break; } // close path case OpCodeName.h: lastMove.IsClosed = true; break; // close and fill the path case OpCodeName.s: { lastMove.IsClosed = true; var path = GetCurrentPathFilled(); graphicGroup.Children.Add(path); ResetCurrentGeometry(); break; } // stroke the path case OpCodeName.S: { var path = GetCurrentPathStroked(); graphicGroup.Children.Add(path); ResetCurrentGeometry(); break; } // close, fill and stroke the path case OpCodeName.b: case OpCodeName.bx: { lastMove.IsClosed = true; var path = GetCurrentPathFilledAndStroked(); graphicGroup.Children.Add(path); ResetCurrentGeometry(); break; } // fill and stroke the path case OpCodeName.B: { var path = GetCurrentPathFilledAndStroked(); currentGeometry.FillRule = GraphicFillRule.NoneZero; graphicGroup.Children.Add(path); ResetCurrentGeometry(); break; } // fill and stroke the path case OpCodeName.Bx: { var path = GetCurrentPathFilledAndStroked(); currentGeometry.FillRule = GraphicFillRule.NoneZero; currentGeometry.FillRule = GraphicFillRule.EvenOdd; graphicGroup.Children.Add(path); ResetCurrentGeometry(); break; } // fill the path case OpCodeName.F: case OpCodeName.f: { var path = GetCurrentPathFilled(); currentGeometry.FillRule = GraphicFillRule.NoneZero; graphicGroup.Children.Add(path); ResetCurrentGeometry(); break; } // fill the path case OpCodeName.fx: { var path = GetCurrentPathFilled(); currentGeometry.FillRule = GraphicFillRule.EvenOdd; graphicGroup.Children.Add(path); ResetCurrentGeometry(); break; } // set color space for stroking operations case OpCodeName.CS: { var colorSpaceName = ((CName)contentOperator.Operands[0]).Name; currentGraphicsState.StrokeColorSpace = colorSpaceManager.GetColorSpace(colorSpaceName); break; } // set color space for nonstroking operations case OpCodeName.cs: { var colorSpaceName = ((CName)contentOperator.Operands[0]).Name; currentGraphicsState.ColorSpace = colorSpaceManager.GetColorSpace(colorSpaceName); break; } // set /DeviceRGB and non-stroked color case OpCodeName.rg: { currentGraphicsState.ColorSpace = colorSpaceManager.GetColorSpace(PdfKeys.DeviceRGB); currentGraphicsState.FillBrush = currentGraphicsState.ColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.FillAlpha.Current); break; } // set /DeviceCMYK and non-stroked color case OpCodeName.k: { currentGraphicsState.ColorSpace = colorSpaceManager.GetColorSpace(PdfKeys.DeviceCMYK); currentGraphicsState.FillBrush = currentGraphicsState.ColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.FillAlpha.Current); break; } // set /DeviceGray and non-stroked color case OpCodeName.g: { currentGraphicsState.ColorSpace = colorSpaceManager.GetColorSpace(PdfKeys.DeviceGray); currentGraphicsState.FillBrush = currentGraphicsState.ColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.FillAlpha.Current); break; } // non-stroked color case OpCodeName.sc: { currentGraphicsState.FillBrush = currentGraphicsState.ColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.FillAlpha.Current); break; } // ICC based non-stroked color case OpCodeName.scn: { currentGraphicsState.FillBrush = currentGraphicsState.ColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.FillAlpha.Current); break; } // ICC based stroked color case OpCodeName.SCN: { currentGraphicsState.StrokeBrush = currentGraphicsState.StrokeColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.StrokeAlpha.Current); break; } // set /DeviceRGB and stroked color case OpCodeName.RG: { currentGraphicsState.StrokeColorSpace = colorSpaceManager.GetColorSpace(PdfKeys.DeviceRGB); currentGraphicsState.StrokeBrush = currentGraphicsState.StrokeColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.StrokeAlpha.Current); break; } // set /DeviceGray and stroked color case OpCodeName.G: { currentGraphicsState.StrokeColorSpace = colorSpaceManager.GetColorSpace(PdfKeys.DeviceGray); currentGraphicsState.StrokeBrush = currentGraphicsState.StrokeColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.StrokeAlpha.Current); break; } // set /DeviceCMYK and stroked color case OpCodeName.K: { currentGraphicsState.StrokeColorSpace = colorSpaceManager.GetColorSpace(PdfKeys.DeviceCMYK); currentGraphicsState.StrokeBrush = currentGraphicsState.StrokeColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.StrokeAlpha.Current); break; } // set stroked color case OpCodeName.SC: { currentGraphicsState.StrokeBrush = currentGraphicsState.StrokeColorSpace.GetBrushDescriptor(contentOperator.Operands, currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.StrokeAlpha.Current); break; } // shading case OpCodeName.sh: { var graphicPath = new GraphicPath(); var shadingDescriptor = shadingManager.GetShading(contentOperator.Operands); graphicPath.Geometry = currentGraphicsState.ClippingPath; graphicPath.FillBrush = shadingDescriptor.GetBrush(currentGraphicsState.CurrentTransformationMatrix, currentGraphicsState.ClippingPath.Bounds, currentGraphicsState.FillAlpha.Current, currentGraphicsState.SoftMask); graphicPath.ColorPrecision = shadingDescriptor.ColorPrecision; graphicGroup.Children.Add(graphicPath); break; } // begin text case OpCodeName.BT: { fontState.TextLineMatrix = Matrix.Identity; fontState.TextMatrix = Matrix.Identity; break; } // set current font case OpCodeName.Tf: { var fontName = ((CName)contentOperator.Operands[0]).Name; fontState.FontSize = PdfUtilities.GetDouble(contentOperator.Operands[1]); fontState.FontDescriptor = fontManager.GetFont(fontName); break; } // set rendering mode case OpCodeName.Tr: { fontState.RenderingMode = PdfUtilities.GetRenderingMode(contentOperator.Operands[0]); break; } // set font transformation matrix case OpCodeName.Tm: { var matrix = PdfUtilities.GetMatrix(contentOperator.Operands); fontState.TextMatrix = matrix; fontState.TextLineMatrix = matrix; break; } // translate line matrix case OpCodeName.Td: { var x = PdfUtilities.GetDouble(contentOperator.Operands[0]); var y = PdfUtilities.GetDouble(contentOperator.Operands[1]); // for some unknown reason the simple next statement doesn't // work, do it in small steps instead //fontState.TextLineMatrix.TranslatePrepend(x, y); var m = fontState.TextLineMatrix; m.TranslatePrepend(x, y); fontState.TextLineMatrix = m; fontState.TextMatrix = fontState.TextLineMatrix; break; } case OpCodeName.TD: { var x = PdfUtilities.GetDouble(contentOperator.Operands[0]); var y = PdfUtilities.GetDouble(contentOperator.Operands[1]); //fontState.TextLineMatrix.Translate(x, y); var m = fontState.TextLineMatrix; m.TranslatePrepend(x, y); fontState.TextLineMatrix = m; fontState.Leading = -y; fontState.TextMatrix = fontState.TextLineMatrix; break; } case OpCodeName.TL: { fontState.Leading = PdfUtilities.GetDouble(contentOperator.Operands[0]); break; } case OpCodeName.Tx: { var m = fontState.TextLineMatrix; m.TranslatePrepend(0, -fontState.Leading); fontState.TextLineMatrix = m; fontState.TextMatrix = fontState.TextLineMatrix; break; } case OpCodeName.QuoteSingle: { break; } case OpCodeName.QuoteDbl: { break; } // a single string case OpCodeName.Tj: { var text = (CString)contentOperator.Operands[0]; var textGraphic = textVectorizer.Vectorize(text.Value, currentGraphicsState, fontState); graphicGroup.Children.AddRange(textGraphic); break; } // multiple strings plus formatting case OpCodeName.TJ: { var array = (CArray)contentOperator.Operands[0]; HandleMultipleTextCommand(array); break; } // graphics state operators // push state onto the stack case OpCodeName.q: { var clone = currentGraphicsState.Clone(); graphicsStateStack.Push(clone); break; } // pop state from the stack case OpCodeName.Q: { currentGraphicsState = graphicsStateStack.Pop(); break; } // current transform matrix case OpCodeName.cm: { var matrix = PdfUtilities.GetMatrix(contentOperator.Operands); currentGraphicsState.TransformationMatrix *= matrix; break; } case OpCodeName.J: { currentGraphicsState.LineCap = GetLineCap(((CInteger)contentOperator.Operands[0]).Value); break; } case OpCodeName.j: { currentGraphicsState.LineJoin = GetLineJoin(((CInteger)contentOperator.Operands[0]).Value); break; } case OpCodeName.M: { currentGraphicsState.MiterLimit = PdfUtilities.GetDouble(contentOperator.Operands[0]); break; } case OpCodeName.d: { SetDashPattern(contentOperator.Operands); break; } // line width case OpCodeName.w: { currentGraphicsState.LineWidth = MatrixUtilities.TransformScale(PdfUtilities.GetDouble(contentOperator.Operands[0]), currentGraphicsState.CurrentTransformationMatrix); break; } // set parameters in the current graphic state of the given state name case OpCodeName.gs: { var name = contentOperator.Operands[0] as CName; extendedStatesManager.SetExtendedGraphicState(currentGraphicsState, fontState, name.Name); break; } case OpCodeName.Do: { var xObjectName = ((CName)contentOperator.Operands[0]).Name; RunXObject(xObjectName); break; } default: break; } } return(this.returnGraphicGroup); }