private static void drawText(Graphics gr, PdfTextData td) { if (td.RenderingMode == PdfTextRenderingMode.NeitherFillNorStroke || td.RenderingMode == PdfTextRenderingMode.AddToPath) { return; } if (Math.Abs(td.FontSize) < 0.001) { return; } if (Math.Abs(td.Bounds.Width) < 0.001 || Math.Abs(td.Bounds.Height) < 0.001) { return; } saveStateAndDraw(gr, td.ClipRegion, () => { using (Font font = toGdiFont(td.Font, td.FontSize)) { using (Brush brush = toGdiBrush(td.Brush)) { gr.TranslateTransform((float)td.Position.X, (float)td.Position.Y); concatMatrix(gr, td.TransformationMatrix); gr.DrawString(td.Text, font, brush, PointF.Empty); } } }); }
private static PdfRectangle getIntersectionBounds(PdfTextData data, string text, int startIndex, int length) { if (startIndex == 0 && text.Length == length) { return(data.Bounds); } IEnumerable <PdfTextData> characters = data.GetCharacters(); if (!containsLtrCharactersOnly(data, text)) { // Process right-to-left chunks in the reverse order. // // Note that this approach might not work for chunks // containing both left-to-right and right-to-left characters. characters = characters.Reverse(); } IEnumerable <PdfRectangle> charBounds = characters .Skip(startIndex) .Take(length) .Select(c => c.Bounds); PdfRectangle union = charBounds.First(); foreach (var b in charBounds.Skip(1)) { union = PdfRectangle.Union(b, union); } return(union); }
private static bool containsLtrCharactersOnly(PdfTextData data, string textLogical) { // A simple trick to detect right-to-left / bidirectional text. // Compare logical and visual representations of text to detect directionality. var noBidiOptions = new PdfTextConversionOptions { UseBidi = false }; string textVisual = data.GetText(noBidiOptions); return(textLogical == textVisual); }
private static void drawText(PdfCanvas target, PdfTextData td, PdfDocument pdf) { target.TextRenderingMode = td.RenderingMode; setBrush(target.Brush, td.Brush); setPen(target.Pen, td.Pen); target.TextPosition = PdfPoint.Empty; target.FontSize = td.FontSize; target.Font = td.Font; target.TranslateTransform(td.Position.X, td.Position.Y); target.Transform(td.TransformationMatrix); target.DrawString(td.GetCharacterCodes()); }
private static void drawText(PdfCanvas target, PdfTextData td) { target.TextRenderingMode = td.RenderingMode; setBrush(target.Brush, td.Brush); setPen(target.Pen, td.Pen); target.TextPosition = PdfPoint.Empty; target.FontSize = td.FontSize; target.Font = td.Font; target.CharacterSpacing = td.CharacterSpacing; target.WordSpacing = td.WordSpacing; target.TextHorizontalScaling = td.HorizontalScaling; target.TranslateTransform(td.Position.X, td.Position.Y); target.Transform(td.TransformationMatrix); target.DrawString(td.GetCharacterCodes()); }
private static void drawText(PdfCanvas target, PdfTextData td, PdfDocument pdf) { target.TextRenderingMode = td.RenderingMode; target.Brush.Color = td.Brush.Color; target.Brush.Opacity = td.Brush.Opacity; target.Pen.Color = td.Pen.Color; target.Pen.Opacity = td.Pen.Opacity; target.Pen.Width = td.Pen.Width; target.TextPosition = PdfPoint.Empty; target.FontSize = td.FontSize; target.Font = td.Font; target.TranslateTransform(td.Position.X, td.Position.Y); target.Transform(td.TransformationMatrix); target.DrawString(td.GetCharacterCodes()); }
private static PdfRectangle getIntersectionBounds(PdfTextData data, int startIndex, int length) { string text = data.Text; if (startIndex == 0 && text.Length == length) { return(data.Bounds); } IEnumerable <PdfRectangle> charBounds = data.GetCharacters() .Skip(startIndex) .Take(length) .Select(c => c.Bounds); PdfRectangle union = charBounds.First(); foreach (var b in charBounds.Skip(1)) { union = PdfRectangle.Union(b, union); } return(union); }
public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. const string PathToFile = "CopyPageObjects.pdf"; using (var pdf = new PdfDocument(@"..\Sample Data\BRAILLE CODES WITH TRANSLATION.pdf")) { using (PdfDocument copy = pdf.CopyPages(0, 1)) { PdfPage sourcePage = copy.Pages[0]; PdfPage copyPage = copy.AddPage(); copyPage.UserUnit = sourcePage.UserUnit; copyPage.Rotation = sourcePage.Rotation; copyPage.MediaBox = sourcePage.MediaBox; if (sourcePage.CropBox != sourcePage.MediaBox) { copyPage.CropBox = sourcePage.CropBox; } PdfCanvas target = copyPage.Canvas; foreach (PdfPageObject obj in sourcePage.GetObjects()) { target.SaveState(); setClipRegion(target, obj.ClipRegion); if (obj.Type == PdfPageObjectType.Path) { PdfPath path = (PdfPath)obj; target.Transform(path.TransformationMatrix); if (path.PaintMode == PdfDrawMode.Fill || path.PaintMode == PdfDrawMode.FillAndStroke) { setBrush(target.Brush, path.Brush); } if (path.PaintMode == PdfDrawMode.Stroke || path.PaintMode == PdfDrawMode.FillAndStroke) { setPen(target.Pen, path.Pen); } appendPath(target, path); drawPath(target, path); } else if (obj.Type == PdfPageObjectType.Image) { PdfPaintedImage image = (PdfPaintedImage)obj; target.TranslateTransform(image.Position.X, image.Position.Y); target.Transform(image.TransformationMatrix); setBrush(target.Brush, image.Brush); target.DrawImage(image.Image, 0, 0, 0); } else if (obj.Type == PdfPageObjectType.Text) { PdfTextData text = (PdfTextData)obj; drawText(target, text); } target.RestoreState(); } copy.RemovePage(0); copy.Save(PathToFile); } } Console.WriteLine($"The output is located in {Environment.CurrentDirectory}"); }
public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. const string PathToFile = "CopyPageObjects.pdf"; using (var pdf = new PdfDocument(@"Sample Data/BRAILLE CODES WITH TRANSLATION.pdf")) { using (PdfDocument copy = pdf.CopyPages(0, 1)) { PdfPage sourcePage = copy.Pages[0]; PdfPage copyPage = copy.AddPage(); copyPage.Rotation = sourcePage.Rotation; copyPage.MediaBox = sourcePage.MediaBox; if (sourcePage.CropBox != sourcePage.MediaBox) { copyPage.CropBox = sourcePage.CropBox; } PdfCanvas target = copyPage.Canvas; foreach (PdfPageObject obj in sourcePage.GetObjects()) { target.SaveState(); setClipRegion(target, obj.ClipRegion); if (obj.Type == PdfPageObjectType.Path) { PdfPath path = (PdfPath)obj; target.Transform(path.TransformationMatrix); setBrushAndPen(target, path); appendPath(target, path); drawPath(target, path); } else if (obj.Type == PdfPageObjectType.Image) { PdfPaintedImage image = (PdfPaintedImage)obj; target.TranslateTransform(image.Position.X, image.Position.Y); target.Transform(image.TransformationMatrix); target.Brush.Color = new PdfRgbColor(255, 255, 255); target.DrawImage(image.Image, 0, 0, 0); } else if (obj.Type == PdfPageObjectType.Text) { PdfTextData text = (PdfTextData)obj; drawText(target, text, copy); } target.RestoreState(); } copy.RemovePage(0); copy.Save(PathToFile); } } Process.Start(PathToFile); }