static void Main(string[] args) { Console.WriteLine("Em qual diretório esta os arquivos?"); string path = Console.ReadLine(); string[] folders = Directory.GetDirectories(path); foreach (string folder in folders) { string[] files = Directory.GetFiles(folder); foreach (string item in files) { PdfDocument pdf = PdfDocument.FromFile(item); for (int i = 0; i < pdf.PageCount; i++) { if ((i + 1) % 2 == 0) { pdf.CopyPages(i - 1, i).SaveAs(item + i.ToString() + "-" + (i + 1).ToString() + ".pdf"); } } if (pdf.PageCount % 2 != 0) { pdf.CopyPages(pdf.PageCount - 1, pdf.PageCount - 1).SaveAs(item + pdf.PageCount.ToString() + ".pdf"); } } } Console.WriteLine("Quebra concluida"); Console.ReadLine(); }
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 = "ExtractPaths.pdf"; using (var pdf = new PdfDocument(@"..\Sample Data\gmail-cheat-sheet.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()) { if (obj.Type != PdfPageObjectType.Path) { continue; } target.SaveState(); { PdfPath path = (PdfPath)obj; target.Transform(path.TransformationMatrix); setClipRegion(target, path.ClipRegion); setBrushAndPen(target, path); appendPath(target, path); drawPath(target, path); } 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. using (PdfDocument pdf = new PdfDocument(@"Sample Data\jfif3.pdf")) { // copy third and first pages to a new PDF document (page indexes are zero-based) using (PdfDocument copy = pdf.CopyPages(new int[] { 2, 0 })) copy.Save("CopyPages.pdf"); } Process.Start("CopyPages.pdf"); }
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. using (var pdf = new PdfDocument(@"Sample Data\jfif3.pdf")) { // copy third and first pages to a new PDF document (page indexes are zero-based) using (PdfDocument copy = pdf.CopyPages(new int[] { 2, 0 })) { // Helps to reduce file size in cases when the copied pages reference // unused resources such as fonts, images, patterns. copy.RemoveUnusedResources(); copy.Save("CopyPages.pdf"); } } Process.Start("CopyPages.pdf"); }
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); }
public void Execute() { string transcriptPath = Configuration.ConfigPath.TranscriptsPath; Log.Information("Beginning processing of graduation transcripts PDF"); if (transcriptPath == null) { Log.Error("Transcripts path configuration variable has not been set by administrator"); return; } string transcriptSourcePath = Path.Combine(transcriptPath, "transcripts.pdf"); if (!File.Exists(transcriptSourcePath)) { Log.Error("Executing GenerateTranscripts - Unable to find transcripts.pdf"); return; } // Calculate the current graduating year for seniors int schoolYear = DateTime.Now.Year; int currentMonth = DateTime.Now.Month; if (currentMonth > 7) { schoolYear++; } string transcriptProcessPath = Path.Combine(transcriptPath, schoolYear + ""); Directory.CreateDirectory(transcriptProcessPath); PdfDocument PDF = null; try { PDF = PdfDocument.FromFile(transcriptSourcePath); } catch (Exception e) { Log.Error(e, "Unable to open transcripts pdf"); return; } Dictionary <string, List <int> > studentIndex = new Dictionary <string, List <int> >(); for (int i = 0; i < PDF.PageCount; i++) { string text = ""; try { text = PDF.ExtractTextFromPage(i); } catch (Exception e) { Log.Error(e, "Error extracting pdf text"); } Match m = Regex.Match(text, "Student Number: ([0-9]{5})", RegexOptions.Multiline); if (m.Groups.Count > 1) { string id = m.Groups[1].Value; if (id.Length != 5) { continue; } if (!studentIndex.ContainsKey(id)) { studentIndex.Add(id, new List <int>()); } studentIndex[id].Add(i); } } foreach (string id in studentIndex.Keys) { PdfDocument transcript = PDF.CopyPages(studentIndex[id]); string transcriptSavePath = Path.Combine(transcriptProcessPath, id + ".pdf"); try { transcript.SaveAs(transcriptSavePath); } catch (Exception e) { Log.Error(e, "Unable to save pdf file: {0}", transcriptSavePath); } } Log.Information("Ending processing of graduation transcripts PDF"); }