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); 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); } } Process.Start(PathToFile); }
private static bool optimizeImage(PdfPaintedImage painted, PdfDocument pdf) { PdfImage image = painted.Image; // inline images can not be recompressed unless you move them to resources // using PdfCanvas.MoveInlineImagesToResources if (image.IsInline) { return(false); } // mask images are not good candidates for recompression if (image.IsMask || image.Width < 8 || image.Height < 8) { return(false); } // get size of the painted image int width = Math.Max(1, (int)painted.Bounds.Width); int height = Math.Max(1, (int)painted.Bounds.Height); if (width >= image.Width || height >= image.Height) { if (image.Mask != null) { return(false); } if (image.ComponentCount == 1 && image.BitsPerComponent == 1 && image.Compression != PdfImageCompression.Group4Fax) { image.RecompressWithGroup4Fax(); } else if (image.BitsPerComponent == 8 && image.ComponentCount >= 3 && image.Compression != PdfImageCompression.Jpeg && image.Compression != PdfImageCompression.Jpeg2000) { image.RecompressWithJpeg2000(10); // or image.RecompressWithJpeg(); } return(true); } // try to replace large masked images if (image.Mask != null) { if (image.Width < 300 || image.Height < 300) { return(false); } using (var stream = new MemoryStream()) { const double ResizeRatio = 1.4; double newWidth = width / ResizeRatio; double newHeight = height / ResizeRatio; PdfPage tempPage = pdf.AddPage(); tempPage.Canvas.DrawImage(image, 0, 0, newWidth, newHeight, 0); PdfPaintedImage tempPaintedImage = tempPage.GetPaintedImages()[0]; tempPaintedImage.SaveAsPainted(stream); pdf.RemovePage(pdf.PageCount - 1); image.ReplaceWith(stream); return(true); } } if (image.Compression == PdfImageCompression.Group4Fax || image.Compression == PdfImageCompression.Group3Fax) { // Fax documents usually looks better if integer-ratio scaling is used // Fractional-ratio scaling introduces more artifacts int ratio = Math.Min(image.Width / width, image.Height / height); // decrease the ratio when it is too high if (ratio > 6) { ratio -= 5; } else if (ratio > 3) { ratio -= 2; } image.ResizeTo(image.Width / ratio, image.Height / ratio, PdfImageCompression.Group4Fax); } else if (image.ComponentCount >= 3 && image.BitsPerComponent == 8) { image.ResizeTo(width, height, PdfImageCompression.Jpeg, 90); } else { image.ResizeTo(width, height, PdfImageCompression.Flate, 9); } return(true); }