// Pick a page from current pdf, and generate a new pdf with this page public bool PageToNewPdf(int pageindex, string newpdffilename) { // Get page from current pdf var page = this.GetPdfDocument().GetPage(pageindex); // Generate a new pdf with the page // init new pdf var outfile = newpdffilename; var writer = new PdfWriter(outfile); var pdfDoc = new PdfDocument(writer); // copy page to new pdf var tempPage = page.CopyTo(pdfDoc); pdfDoc.AddPage(tempPage); // important! copy forms from source page to new page PdfPageFormCopier cp = new PdfPageFormCopier(); cp.Copy(page, tempPage); // close document to save new pdf var document = new iText.Layout.Document(pdfDoc); document.Close(); pdfDoc.Close(); writer.Close(); return(true); }
public new void ExportToPdf(string path, double width = 700, double height = 370) { // exports spectrum annotation w/o base seq annotation string tempPdfPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), "temp.pdf"); string tempPngPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), "annotation.png"); base.ExportToPdf(tempPdfPath, width, height); // scales for desired DPI double dpiScale = MetaDrawSettings.CanvasPdfExportDpi / 96.0; // save base seq as PNG SequenceDrawingCanvas.Measure(new Size((int)SequenceDrawingCanvas.Width, (int)SequenceDrawingCanvas.Height)); SequenceDrawingCanvas.Arrange(new Rect(new Size((int)SequenceDrawingCanvas.Width, (int)SequenceDrawingCanvas.Height))); RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)(dpiScale * SequenceDrawingCanvas.Width), (int)(dpiScale * SequenceDrawingCanvas.Height), MetaDrawSettings.CanvasPdfExportDpi, MetaDrawSettings.CanvasPdfExportDpi, PixelFormats.Pbgra32); renderBitmap.Render(SequenceDrawingCanvas); PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); using (FileStream file = File.Create(tempPngPath)) { encoder.Save(file); } // adds base seq annotation to pdf PdfDocument pdfDoc = new PdfDocument(new PdfReader(tempPdfPath), new PdfWriter(path)); iText.Layout.Document document = new iText.Layout.Document(pdfDoc); ImageData imgData = ImageDataFactory.Create(tempPngPath); iText.Layout.Element.Image img = new iText.Layout.Element.Image(imgData); img.SetMarginLeft((float)(-1.0 * SequenceDrawingCanvas.Margin.Left) + 10); img.SetMarginTop(-30); img.ScaleToFit((float)SequenceDrawingCanvas.Width, (float)SequenceDrawingCanvas.Height); document.Add(img); document.Close(); pdfDoc.Close(); // delete temp files File.Delete(tempPdfPath); File.Delete(tempPngPath); }
private void Print <T>(bool isRaw, List <T> selectedPersons, int rawpagesCount = 0) where T : IPerson { var filePath = Path.Combine(Constants.WorkingDirectory, typeof(T) == typeof(Rozhodci) ? "vyplatni-listina-rozhodci.pdf" : "vyplatni-listina-ceta.pdf"); using (var writer = new PdfWriter(filePath)) { using (var pdf = new PdfDocument(writer)) { var doc = new Document(pdf, PageSize.A4.Rotate()); doc.SetMargins(23, 38, 20, 38); var pagesCount = (int)Math.Ceiling((double)selectedPersons.Count / 10); if (pagesCount == 0) { pagesCount = 1; } if (isRaw) { pagesCount = rawpagesCount; } var font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA, PdfEncodings.CP1250); var bold = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA_BOLD, PdfEncodings.CP1250); var mainHeaderTitle = _settings.IsClubNameEnabled ? _settings.ClubName : new string('.', 80); var documentMainHeader = new Paragraph($"TJ, Sportovní klub, Atletický oddíl, Atletický klub: {mainHeaderTitle}") .SetFont(bold).SetFontSize(16).SetTextAlignment(TextAlignment.CENTER); var listinaHeadText = "VÝPLATNÍ LISTINA ODMĚN "; listinaHeadText += typeof(T) == typeof(Rozhodci) ? "ROZHODČÍCH" : "TECHNICKÉ ČETY"; var vyplatniListinaHead = new Paragraph(listinaHeadText) .SetFont(bold).SetFontSize(16).SetTextAlignment(TextAlignment.CENTER).SetMarginTop(-3); var rules = new Paragraph( "Níže podepsaní účastníci soutěže souhlasili s uvedením svých osobních údajů na této výplatní listině (jméno, příjmení, datum narození a adresa).") .SetFont(font).SetFontSize(9).SetTextAlignment(TextAlignment.CENTER).SetMarginTop(-1).SetMarginBottom(1); var evidenceText = new Paragraph( $"Tyto údaje budou součástí evidence {new string('.', 123)} a budou jen pro vnitřní potřebu.") .SetFont(font).SetFontSize(9).SetTextAlignment(TextAlignment.CENTER).SetMarginTop(-1); var aboutcompetition = new Table(UnitValue.CreatePercentArray(new[] { 52.15f, 47.85f })).SetMarginTop(1).UseAllAvailableWidth(); aboutcompetition.AddCell(_settings.IsCompetitionNameEnabled ? AboutCompetitionCell($"Název soutěže: {_settings.CompetitionName}", bold) : AboutCompetitionCell($"Název soutěže {new string('.', 89)}", bold)); if (_settings.IsCompetitionDateEnabled) { if (_settings.CompetitionStartDate.HasValue && _settings.CompetitionEndDate == null) { aboutcompetition.AddCell( AboutCompetitionCell($"Datum konání soutěže: {_settings.CompetitionStartDate.Value:dd.MM.yyyy}", bold)); } else if (_settings.CompetitionStartDate.HasValue && _settings.CompetitionEndDate.HasValue) { aboutcompetition.AddCell(AboutCompetitionCell( $"Datum konání soutěže: {_settings.CompetitionStartDate.Value:dd.MM.yyyy} - {_settings.CompetitionEndDate.Value:dd.MM.yyyy}", bold)); } else { aboutcompetition.AddCell(AboutCompetitionCell($"Datum konání soutěže {new string('.', 70)}", bold)); } } else { aboutcompetition.AddCell(AboutCompetitionCell($"Datum konání soutěže {new string('.', 70)}", bold)); } var aboutcompetition2 = new Table(UnitValue.CreatePercentArray(new[] { 52.15f, 47.85f })).SetMarginTop(3).UseAllAvailableWidth(); if (_settings.IsCompetitionTimeEnabled) { if (_settings.CompetitionStartTime.HasValue && _settings.CompetitionEndTime == null) { aboutcompetition2.AddCell( AboutCompetitionCell($"Doba konání soutěže: {_settings.CompetitionStartTime.Value:HH:mm} - ", bold)); } else if (_settings.CompetitionStartTime.HasValue && _settings.CompetitionEndTime.HasValue) { aboutcompetition2.AddCell(AboutCompetitionCell( $"Doba konání soutěže: {_settings.CompetitionStartTime.Value:HH:mm} - {_settings.CompetitionEndTime.Value:HH:mm}", bold)); } else { aboutcompetition2.AddCell(AboutCompetitionCell($"Doba konání soutěže {new string('.', 78)}", bold)); } } else { aboutcompetition2.AddCell(AboutCompetitionCell($"Doba konání soutěže {new string('.', 78)}", bold)); } aboutcompetition2.AddCell(_settings.IsCompetitionPlaceEnabled ? AboutCompetitionCell($"Místo konání soutěže: {_settings.CompetitionPlace}", bold) : AboutCompetitionCell($"Místo konání soutěže {new string('.', 72)}", bold)); #region TableHead var pdfNumber = new Cell(2, 1).SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetPadding(0) .SetBorder(new SolidBorder(1)); pdfNumber.Add(new Paragraph("Pořadové")); pdfNumber.Add(new Paragraph("číslo")); var nameCell = new Cell().SetTextAlignment(TextAlignment.LEFT) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetHeight(17) .SetBorder(new SolidBorder(1.2f)) .SetBorderBottom(new GrooveBorder(DeviceCmyk.BLACK, 1, 0.5f)) .SetPadding(0); nameCell.Add(new Paragraph("Jméno a příjmení")).SetPaddingLeft(17); var addressCell = new Cell().SetTextAlignment(TextAlignment.LEFT) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetHeight(17) .SetBorder(new SolidBorder(1.2f)) .SetBorderTop(Border.NO_BORDER) .SetPadding(0); addressCell.Add(new Paragraph("Přesná adresa")).SetPaddingLeft(17); var birthDate = new Cell(2, 1).SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetPadding(0); birthDate.Add(new Paragraph("Datum")); birthDate.Add(new Paragraph("narození")); var awardCell = new Cell(2, 1).SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetPadding(0); awardCell.Add(new Paragraph("Odměna")); awardCell.Add(new Paragraph("Kč")); var signCell = new Cell(2, 1).SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetPadding(0); signCell.Add(new Paragraph("Potvrzení o přijetí odměny")); signCell.Add(new Paragraph("Podpis")); #endregion #region Tabulka Bottom var emptyCell = new Cell().SetBorder(Border.NO_BORDER).SetPadding(0f); var sumtextCell = new Cell(1, 2).SetFont(bold).SetFontSize(15).SetTextAlignment(TextAlignment.RIGHT) .SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetPadding(0); sumtextCell.Add(new Paragraph("CELKEM VYPLACENO: ")).SetPaddingRight(2); var last = new Paragraph( "Vyplatil............................................... Dne............................................... Podpis...............................................") .SetFont(font) .SetFontSize(12) .SetMarginTop(8); #endregion for (int i = 0; i < pagesCount; i++) { doc.Add(documentMainHeader); doc.Add(vyplatniListinaHead); doc.Add(rules); doc.Add(evidenceText); doc.Add(aboutcompetition); doc.Add(aboutcompetition2); #region Table var rozhodciTable = new Table(new float[] { 65, 355, 100, 80, 170 }) .UseAllAvailableWidth() .SetMarginTop(6f) .SetFontSize(FontSize); rozhodciTable.SetWidth(765f); rozhodciTable.SetBorder(Border.NO_BORDER); rozhodciTable.AddCell(pdfNumber); rozhodciTable.AddCell(nameCell); rozhodciTable.AddCell(birthDate); rozhodciTable.AddCell(awardCell); rozhodciTable.AddCell(signCell); rozhodciTable.AddCell(addressCell); for (int j = 0; j < 10; j++) { var index = j + i * 10; var pdfNumberData = new Cell(2, 1).SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetFontSize(15) .SetHeight(30) .SetPadding(0) .SetBorder(new SolidBorder(1)) .SetBorderLeft(new SolidBorder(1)); pdfNumberData.Add(new Paragraph((j + 1).ToString())); var nameCellData = new Cell().SetTextAlignment(TextAlignment.LEFT) .SetFont(font) .SetHeight(15) .SetBorder(new SolidBorder(1.2f)) .SetBorderBottom(new GrooveBorder(DeviceCmyk.BLACK, 1, 0.5f)) .SetPadding(0); var addressCellData = new Cell().SetTextAlignment(TextAlignment.LEFT) .SetFont(font) .SetHeight(15) .SetBorder(new SolidBorder(1.2f)) .SetBorderTop(Border.NO_BORDER) .SetPadding(0); var birthDateData = new Cell(2, 1).SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetPadding(0) .SetHeight(30); var awardCellData = new Cell(2, 1).SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetPadding(0) .SetHeight(30); var signCellData = new Cell(2, 1).SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE) .SetFont(font) .SetPadding(0) .SetHeight(30); signCellData.Add(new Paragraph("")); if (!isRaw && selectedPersons.Count > index) { nameCellData.Add(new Paragraph(selectedPersons[index].FullName)).SetPaddingLeft(17); addressCellData.Add(new Paragraph($"{selectedPersons[index].Address}, {selectedPersons[index].City}")) .SetPaddingLeft(17); birthDateData.Add(new Paragraph(selectedPersons[index].BirthDate.ToShortDateString())); awardCellData.Add(new Paragraph(selectedPersons[index].Reward.HasValue ? selectedPersons[index].Reward.Value.ToString() : "")); } else { nameCellData.Add(new Paragraph("")).SetPaddingLeft(17); addressCellData.Add(new Paragraph("")).SetPaddingLeft(17); birthDateData.Add(new Paragraph("")); awardCellData.Add(new Paragraph("")); } rozhodciTable.AddCell(pdfNumberData); rozhodciTable.AddCell(nameCellData); rozhodciTable.AddCell(birthDateData); rozhodciTable.AddCell(awardCellData); rozhodciTable.AddCell(signCellData); rozhodciTable.AddCell(addressCellData); } #endregion var sumCell = new Cell().SetBackgroundColor(ColorConstants.LIGHT_GRAY) .SetPadding(0) .SetFont(bold) .SetFontSize(15) .SetTextAlignment(TextAlignment.CENTER) .SetVerticalAlignment(VerticalAlignment.MIDDLE); sumCell.Add(selectedPersons.Count <= i * 10 + 10 ? new Paragraph(!isRaw ? CountSum(selectedPersons.GetRange(i * 10, selectedPersons.Count - i * 10)) : "") : new Paragraph(!isRaw ? CountSum(selectedPersons.GetRange(i * 10, 10)) : "")); rozhodciTable.AddCell(emptyCell); rozhodciTable.AddCell(sumtextCell); rozhodciTable.AddCell(sumCell); rozhodciTable.AddCell(emptyCell); doc.Add(rozhodciTable); doc.Add(last); } doc.Close(); } } Browser.OpenLink(filePath); }
private async void Generate_Button_Click(object sender, RoutedEventArgs e) { try { DisplayLog.Document.Blocks.Clear(); Progress.IsIndeterminate = true; string excelPath = Excel_File.Text; int amount = int.Parse(Amount.Text); if (amount <= 0) { throw new Exception("Die Anzahl der Testungen muss größer als 0 sein."); } if (String.IsNullOrWhiteSpace(excelPath)) { throw new Exception("Es sieht so aus, als wäre kein ExcelFile angegeben worden. Bitte hole das nach."); } SaveFileDialog dlg = new SaveFileDialog(); dlg.FileName = $"Reihentestung_generiert_{DateTime.Now.ToString("yyyy_MM_dd_HH_mm")}"; // Default file name dlg.DefaultExt = ".pdf"; // Default file extension dlg.Filter = "PDF documents (.pdf)|*.pdf"; // Filter files by extension // Show save file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result == true) { // Save document Generate_Button.IsEnabled = false; Button_Select_Excel_File.IsEnabled = false; Amount.IsEnabled = false; string outPutPath = dlg.FileName; output = outPutPath; AddToLog($"Einlesen der Exceldatei von {excelPath}"); List <Testee> persons = ExcelParser.Parse(excelPath); AddToLog("Exceldatei erfolgreich eingelesen"); Progress.Maximum = persons.Count * amount + 1; Progress.IsIndeterminate = false; AdvanceProgressValue(); PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new FileStream(outPutPath, FileMode.Create, FileAccess.Write))); iText.Layout.Document document = new iText.Layout.Document(pdfDocument); int count = persons.Count; for (var j = 1; j <= count; j++) { var person = persons[j - 1]; AddToLog($"Person {j} von {count}: {person.Surname} {person.Forename}"); for (int i = 0; i < amount; i++) { AddToLog(String.Format("Generiere {0} von {1} für {2} {3}", i + 1, amount, person.Surname, person.Forename)); var entry = new Entry(person, i); AddToLog("Hole ID aus dem Internet"); await entry.GetID(); //entry.ID = "100"; //await Task.Delay(500); AddToLog(String.Format("ID erhalten ({0})", entry.ID)); AddToLog("Füge an PDF an"); entry.CreatePdf(pdfDocument, document, j == count && i == amount - 1); AddToLog("An PDF angefügt"); AdvanceProgressValue(); } } document.Close(); AddToLog($"PDF erstellt unter {outPutPath}"); MessageBox.Show($"Vorgang abgeschlossen.\nPDF erstellt unter {outPutPath}"); Progress.Value = 0; Generate_Button.IsEnabled = true; Button_Select_Excel_File.IsEnabled = true; Amount.IsEnabled = true; } } catch (Exception ex) { Progress.Value = 0; Progress.IsIndeterminate = false; AddToLog(ex.Message); AddToLog($"Löschen der beschädigten PDF-Datei unter {output}"); File.Delete(output); MessageBox.Show(ex.Message + "\r"); Generate_Button.IsEnabled = true; Button_Select_Excel_File.IsEnabled = true; Amount.IsEnabled = true; } }