private void btnImport_Click(object sender, System.EventArgs e) { PdfLoadedDocument ldDoc1 = new PdfLoadedDocument(txtTemplate1.Tag.ToString()); PdfLoadedDocument ldDoc2 = new PdfLoadedDocument(txtTemplate2.Tag.ToString()); PdfDocument doc = new PdfDocument(); for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i) { PdfPage page = doc.Pages.Add(); PdfGraphics g = page.Graphics; PdfPageBase lpage = ldDoc2.Pages[i]; PdfTemplate template = lpage.CreateTemplate(); if ((template.Width < page.GetClientSize().Width) || (template.Height < page.GetClientSize().Height)) { g.DrawPdfTemplate(template, new PointF((page.GetClientSize().Width - template.Width) / 2, (page.GetClientSize().Height - template.Height) / 2)); } else { g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); } lpage = ldDoc1.Pages[0]; template = lpage.CreateTemplate(); g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); } doc.Save("sample.pdf"); //Message box confirmation to view the created PDF document. if (MessageBox.Show("Do you want to view the PDF file?", "PDF File Created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { //Launching the PDF file using the default Application.[Acrobat Reader] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.pdf") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.pdf"); #endif this.Close(); } else { // Exit this.Close(); } }
public ActionResult OverlayDocuments(string InsideBrowser) { string basePath = _hostingEnvironment.WebRootPath; string dataPath = string.Empty; dataPath = basePath + @"/PDF/"; string dataPath1 = dataPath + "BorderTemplate.pdf"; string dataPath2 = dataPath + "SourceTemplate.pdf"; Stream stream1 = new FileStream(dataPath2, FileMode.Open, FileAccess.Read); FileStream file = new FileStream(dataPath1, FileMode.Open, FileAccess.Read, FileShare.Read); PdfLoadedDocument ldDoc1 = new PdfLoadedDocument(file); PdfLoadedDocument ldDoc2 = new PdfLoadedDocument(stream1); PdfDocument doc = new PdfDocument(); for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i) { PdfPage page = doc.Pages.Add(); PdfGraphics g = page.Graphics; PdfPageBase lpage = ldDoc2.Pages[i]; PdfTemplate template = lpage.CreateTemplate(); g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); lpage = ldDoc1.Pages[0]; template = lpage.CreateTemplate(); g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); } MemoryStream stream = new MemoryStream(); //Save the PDF document doc.Save(stream); stream.Position = 0; //Close the PDF document doc.Close(true); //Download the PDF document in the browser. FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf"); fileStreamResult.FileDownloadName = "Overlay.pdf"; return(fileStreamResult); }
private PdfGraphics CreateNewPage() { PdfPage page = document.Pages.Add(); PdfGraphics pdfGraphics = page.Graphics; //Draw the loaded template into new document. pdfGraphics.DrawPdfTemplate(template, new PointF(0, 0), page.Size); return(pdfGraphics); }
public ActionResult OverlayDocuments(string InsideBrowser) { string dataPath1, dataPath2; dataPath1 = ResolveApplicationDataPath("BorderTemplate.pdf"); dataPath2 = ResolveApplicationDataPath("SourceTemplate.pdf"); Stream stream1 = new FileStream(dataPath2, FileMode.Open, FileAccess.Read); FileStream file = new FileStream(dataPath1, FileMode.Open, FileAccess.Read, FileShare.Read); PdfLoadedDocument ldDoc1 = new PdfLoadedDocument(file); PdfLoadedDocument ldDoc2 = new PdfLoadedDocument(stream1); PdfDocument doc = new PdfDocument(); for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i) { PdfPage page = doc.Pages.Add(); PdfGraphics g = page.Graphics; PdfPageBase lpage = ldDoc2.Pages[i]; PdfTemplate template = lpage.CreateTemplate(); g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); lpage = ldDoc1.Pages[0]; template = lpage.CreateTemplate(); g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); } if (InsideBrowser == "Browser") { return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Open)); } else { return(doc.ExportAsActionResult("sample.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save)); } }
/// <summary> /// Create a simple PDF document /// </summary> /// <returns>Return the created PDF document as stream</returns> public MemoryStream CreatePdfDocument() { string dataPath1 = ResolveApplicationPath("border-template.pdf"); string dataPath2 = ResolveApplicationPath("source-template.pdf"); Stream stream1 = new FileStream(dataPath2, FileMode.Open, FileAccess.Read); FileStream file = new FileStream(dataPath1, FileMode.Open, FileAccess.Read, FileShare.Read); PdfLoadedDocument ldDoc1 = new PdfLoadedDocument(file); PdfLoadedDocument ldDoc2 = new PdfLoadedDocument(stream1); PdfDocument doc = new PdfDocument(); for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i) { PdfPage page = doc.Pages.Add(); PdfGraphics g = page.Graphics; PdfPageBase lpage = ldDoc2.Pages[i]; PdfTemplate template = lpage.CreateTemplate(); g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); lpage = ldDoc1.Pages[0]; template = lpage.CreateTemplate(); g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); } MemoryStream stream = new MemoryStream(); //Save the PDF document doc.Save(stream); doc.Close(); //If the position is not set to '0' then the PDF will be empty. stream.Position = 0; return(stream); }
private async void GeneratePDF_Click(object sender, RoutedEventArgs e) { Stream borderTemplateStream = typeof(OverlayDocuments).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.BorderTemplate.pdf"); Stream sourceTemplateStream = typeof(OverlayDocuments).GetTypeInfo().Assembly.GetManifestResourceStream("Syncfusion.SampleBrowser.UWP.Pdf.Pdf.Assets.SourceTemplate.pdf"); PdfLoadedDocument ldDoc1 = new PdfLoadedDocument(borderTemplateStream); PdfLoadedDocument ldDoc2 = new PdfLoadedDocument(sourceTemplateStream); PdfDocument doc = new PdfDocument(); for (int i = 0, count = ldDoc2.Pages.Count; i < count; ++i) { PdfPage page = doc.Pages.Add(); PdfGraphics g = page.Graphics; PdfPageBase lpage = ldDoc2.Pages[i]; PdfTemplate template = lpage.CreateTemplate(); if ((template.Width < page.GetClientSize().Width) || (template.Height < page.GetClientSize().Height)) { g.DrawPdfTemplate(template, new PointF((page.GetClientSize().Width - template.Width) / 2, (page.GetClientSize().Height - template.Height) / 2)); } else { g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); } lpage = ldDoc1.Pages[0]; template = lpage.CreateTemplate(); g.DrawPdfTemplate(template, PointF.Empty, page.GetClientSize()); } MemoryStream stream = new MemoryStream(); await doc.SaveAsync(stream); doc.Close(true); Save(stream, "OverlayDocument.pdf"); }
private void luoLaskuBtn_Click(object sender, EventArgs e) { bool voikoLuoda = true; //Luodaan asiakas ja laitetaan asiakkaalle tiedot Asiakas asiakas = new Asiakas(); asiakas.Sukunimi = sukunimiCBox.Text; asiakas.Etunimi = etunimiCBox.Text; asiakas = handler.HaeAsiakasNimellä(asiakas); //Luodaan yritys ja haetaan sille tiedot Yritys yritys = new Yritys(); yritys = handler.HaeYritys(id); DateTime dateAndTime = DateTime.Now; DateTime eräpäivä = dateAndTime.AddDays(14); //Luodaan lasku ja laitetaan laskulle tiedot Lasku lasku = new Lasku(); int laskunID; lasku.Päivämäärä = dateAndTime; lasku.Eräpäivä = eräpäivä; lasku.AsiakasID = asiakas.Id; try { lasku.Laskutuskausi = laskutusCbox.SelectedItem.ToString(); } catch (Exception) { MessageBox.Show("Et valinnut laskutuskautta", "!Huom"); voikoLuoda = false; } if (voikoLuoda == true) { laskunID = handler.TallennaLasku(lasku); string viitenumero; viitenumero = Viitenumero(asiakas, laskunID); // luodaan pdf tiedosto ja sille olennaiset ominaisuudet PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Properties.Resources.laskupohja); PdfLoadedForm loadedForm = loadedDocument.Form; PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage; PdfTemplate template = loadedPage.CreateTemplate(); PdfDocument document = new PdfDocument(); document.PageSettings.SetMargins(2); PdfPage page = document.Pages.Add(); PdfGraphics graphics = page.Graphics; graphics.DrawPdfTemplate(template, PointF.Empty, new SizeF(page.Size.Width, page.Size.Height)); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20); float veroYhteensä, määrä, verotonHinta, hinta, yhteensä; float.TryParse(hintaTbox.Text, out hinta); float.TryParse(määräTbox.Text, out määrä); verotonHinta = hinta * määrä; veroYhteensä = verotonHinta * 1.24f - verotonHinta; yhteensä = verotonHinta + veroYhteensä; //Viedään tietoa dokumenttiin (loadedForm.Fields[0] as PdfLoadedTextBoxField).Text = dateAndTime.ToString("dd/MM/yyyy"); (loadedForm.Fields[1] as PdfLoadedTextBoxField).Text = "7%"; //Viivästyskorko (loadedForm.Fields[2] as PdfLoadedTextBoxField).Text = laskunID.ToString(); //Laskunumero (loadedForm.Fields[3] as PdfLoadedTextBoxField).Text = asiakas.YTunnus; // Asiakkaan Y-Tunnus (loadedForm.Fields[4] as PdfLoadedTextBoxField).Text = maksuehtoTbox.Text; // Maksuehto (loadedForm.Fields[5] as PdfLoadedTextBoxField).Text = ""; // Viitteemme (loadedForm.Fields[6] as PdfLoadedTextBoxField).Text = eräpäivä.ToString("dd/MM/yyyy"); // Eräpäivä (loadedForm.Fields[7] as PdfLoadedTextBoxField).Text = ""; // Viittenne (loadedForm.Fields[8] as PdfLoadedTextBoxField).Text = toimitusehtoTbox.Text; // Toimitusehto (loadedForm.Fields[63] as PdfLoadedTextBoxField).Text = verotonHinta.ToString(); // Veroton yhteensä (loadedForm.Fields[64] as PdfLoadedTextBoxField).Text = veroYhteensä.ToString(); // ALV yhteensä (loadedForm.Fields[65] as PdfLoadedTextBoxField).Text = yhteensä.ToString(); // Verollinen yhteensä (loadedForm.Fields[70] as PdfLoadedTextBoxField).Text = yhteensä.ToString(); // Yhteensä (loadedForm.Fields[71] as PdfLoadedTextBoxField).Text = EtunimiLbl.Text + " " + sukunimiLbl.Text + "\n" + osoiteLbl.Text + "\n" + postinumeroLbl.Text + " " + postitoimipaikkaLbl.Text; // Asiakkaan tiedot (loadedForm.Fields[68] as PdfLoadedTextBoxField).Text = eräpäivä.ToString("dd/MM/yyyy"); // Eräpäivä (loadedForm.Fields[75] as PdfLoadedTextBoxField).Text = yritys.Nimi + "\n" + yritys.Osoite + "\n" // Laskun lähettäjän tiedot + yritys.Postinumero + " " + yritys.Postiosoite; // Laskun lähettäjän tiedot (loadedForm.Fields[66] as PdfLoadedTextBoxField).Text = yritys.Iban; // IBAN (loadedForm.Fields[67] as PdfLoadedTextBoxField).Text = yritys.Bicswift; // BIC/SWIFT = 67 (loadedForm.Fields[69] as PdfLoadedTextBoxField).Text = viitenumero; // Viitenumero = 69 (loadedForm.Fields[9] as PdfLoadedTextBoxField).Text = nimikeTbox.Text; // Nimike (loadedForm.Fields[10] as PdfLoadedTextBoxField).Text = määräTbox.Text; // Määrä (loadedForm.Fields[11] as PdfLoadedTextBoxField).Text = "kpl"; // Yks. (loadedForm.Fields[12] as PdfLoadedTextBoxField).Text = hintaTbox.Text; // hinta (loadedForm.Fields[13] as PdfLoadedTextBoxField).Text = "24%"; // Alv % (loadedForm.Fields[14] as PdfLoadedTextBoxField).Text = yhteensä.ToString(); // Verollinen yhteensä //Tällä luupilla voi katsoa missä kohdassa mikäkin field on. //for (int i = 0; i < 76; i++) //{ // (loadedForm.Fields[i] as PdfLoadedTextBoxField).Text = i.ToString(); //} // tallenetaan dokumentti loadedDocument.Save("Lasku.pdf"); document.Save("UusiLasku.pdf"); loadedDocument.Close(true); document.Close(true); Process.Start("Lasku.pdf"); // Tarkistetaan onko laskun tiedot saatu tallennettua if (laskunID != 0) { MessageBox.Show("Laskun tiedot tallennettiin tietokantaan", "Huom"); } else { MessageBox.Show("Laskun tietoja ei saatu tallennettua", "Huom"); } } }
/// <summary> /// /// </summary> /// <param name="filePath"></param> /// <param name="bodyHtml"></param> /// <param name="headerHtml"></param> /// <param name="footerHtml"></param> /// <param name="headerHeight"></param> /// <param name="footerHeight"></param> /// <param name="firstPageHeader"></param> /// <returns></returns> public static string HtmlToPdf(string filePath, string bodyHtml, string headerHtml, string footerHtml, int headerHeight = 96, int footerHeight = 96, bool firstPageHeader = false) { string css = ""; string html = ""; using (System.IO.StreamReader r = new System.IO.StreamReader(System.Web.Hosting.HostingEnvironment.MapPath(cssPath))) { css = r.ReadToEnd(); } using (System.IO.StreamReader r = new System.IO.StreamReader(System.Web.Hosting.HostingEnvironment.MapPath(emptyLayoutPath))) { html = r.ReadToEnd().Replace("[[CSS]]", css); } PdfDocument document = new PdfDocument(); document.PageSettings.Size = PdfPageSize.A4; document.PageSettings.SetMargins(0); //Initialize HTML to PDF converter var htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit); //Create new settings var setting = CreateConverterSetting(); var pdfHeader = HeaderHTMLtoPDF(html.Replace("[[HTML]]", headerHtml), headerHeight); var pdfFooter = FooterHTMLtoPDF(html.Replace("[[HTML]]", footerHtml), footerHeight); setting.PdfPageSize = new SizeF(PdfPageSize.A4.Width, PdfPageSize.A4.Height - pdfHeader.Height - pdfFooter.Height); htmlConverter.ConverterSettings = setting; //Convert to PDF var body = html.Replace("[[HTML]]", bodyHtml); PdfDocument docBody = htmlConverter.Convert(body, string.Empty); Stream stream = new MemoryStream(); docBody.Save(stream); docBody.Close(); PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(stream); for (int i = 0; i < pdfLoadedDocument.Pages.Count; i++) { PdfLoadedPage loadedPage = pdfLoadedDocument.Pages[i] as PdfLoadedPage; PdfTemplate template = loadedPage.CreateTemplate(); PdfSection section = document.Sections.Add(); PdfPage page = section.Pages.Add(); PdfGraphics graphics = page.Graphics; if (i == 0) { section.Template.Top = pdfHeader; section.Template.Bottom = FooterHTMLtoPDF(html.Replace("[[HTML]]", footerHtml?.Replace("[[Page]]", "1")?.Replace("[[TotalPage]]", pdfLoadedDocument.Pages.Count.ToString())), footerHeight); } else { if (firstPageHeader) { section.Template.Top = HeaderHTMLtoPDF(html.Replace("[[HTML]]", string.Empty), headerHeight); } else { section.Template.Top = HeaderHTMLtoPDF(html.Replace("[[HTML]]", headerHtml), headerHeight); } section.Template.Bottom = FooterHTMLtoPDF(html.Replace("[[HTML]]", footerHtml?.Replace("[[Page]]", (i + 1).ToString())?.Replace("[[TotalPage]]", pdfLoadedDocument.Pages.Count.ToString())), footerHeight); } graphics.DrawPdfTemplate(template, new PointF(0, pdfHeader.Height), new SizeF(page.Size.Width, page.Size.Height - pdfHeader.Height - pdfFooter.Height)); } //Save the document document.Save(filePath); document.Close(); stream.Dispose(); filePath = filePath.Replace("~", ""); return(filePath); }