/// <summary> /// How to edit Header and Footer in PDF file /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/edit-header-and-footer-in-pdf-net-csharp-vb.php /// </remarks> static void ChangeHeader() { string inpFile = @"..\..\somebody.pdf"; string outFile = "With changed header.pdf"; DocumentCore dc = DocumentCore.Load(inpFile); // Create new header with formatted text. HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault); header.Content.Start.Insert("Modified : 1 April 2020", new CharacterFormat() { Size = 14.0, FontColor = Color.DarkGreen }); foreach (Section s in dc.Sections) { if (s.Blocks.Count > 0) { s.Blocks.RemoveAt(1); } s.HeadersFooters.Add(header.Clone(true)); } dc.Save(outFile); // Open the results for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile) { UseShellExecute = true }); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true }); }
static void AddCustomFooter(string text, ref Section section) { HeaderFooter footer = new HeaderFooter(); footer.AddParagraph(text); footer.Format.Font.Size = 10; footer.Format.Font.Name = "Times-Roman"; footer.Format.Alignment = ParagraphAlignment.Center; section.Footers.Primary = footer.Clone(); }
/// <summary> /// Add page numbering into an existing PDF document. /// </summary> /// <remarks> /// https://www.sautinsoft.com/products/document/help/net/developer-guide/add-page-numbering-in-pdf-net-csharp-vb.php /// </remarks> static void AddPageNumbering() { string inpFile = @"..\..\shrek.pdf"; string outFile = @"With Pages.pdf"; DocumentCore dc = DocumentCore.Load(inpFile); // We place our page numbers into the footer. // Therefore we've to create a footer. HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault); // Create a new paragraph to insert a page numbering. // So that, our page numbering looks as: Page N of M. Paragraph par = new Paragraph(dc); par.ParagraphFormat.Alignment = HorizontalAlignment.Right; CharacterFormat cf = new CharacterFormat() { FontName = "Consolas", Size = 14.0 }; par.Content.Start.Insert("Page ", cf.Clone()); // Page numbering is a Field. // Create two fields: FieldType.Page and FieldType.NumPages. Field fPage = new Field(dc, FieldType.Page); fPage.CharacterFormat = cf.Clone(); par.Content.End.Insert(fPage.Content); par.Content.End.Insert(" of ", cf.Clone()); Field fPages = new Field(dc, FieldType.NumPages); fPages.CharacterFormat = cf.Clone(); par.Content.End.Insert(fPages.Content); footer.Blocks.Add(par); foreach (Section s in dc.Sections) { s.HeadersFooters.Add(footer.Clone(true)); } dc.Save(outFile); // Open the results for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile) { UseShellExecute = true }); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true }); }
/// <summary> /// How to add a header and footer into PDF document. /// </summary> /// <remarks> /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/add-header-and-footer-in-pdf-net-csharp-vb.php /// </remarks> static void AddHeaderFooter() { string inpFile = @"..\..\shrek.pdf"; string outFile = "Shrek with header and footer.pdf"; DocumentCore dc = DocumentCore.Load(inpFile); // Create new header with formatted text. HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault); header.Content.Start.Insert("Shrek and Donkey", new CharacterFormat() { Size = 14.0, FontColor = Color.Brown }); foreach (Section s in dc.Sections) { s.HeadersFooters.Add(header.Clone(true)); } // Create new footer with formatted text. HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault); footer.Content.Start.Insert("Fiona.", new CharacterFormat() { Size = 14.0, FontColor = Color.Blue }); foreach (Section s in dc.Sections) { s.HeadersFooters.Add(footer.Clone(true)); } dc.Save(outFile); // Open the PDF documents for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile) { UseShellExecute = true }); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true }); }
/// <summary> /// Removes the old header/footer and inserts a new one into an existing PDF document. /// </summary> /// <remarks> /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/remove-header-and-footer-in-pdf-net-csharp-vb.php /// </remarks> static void ReplaceHeader() { string inpFile = @"..\..\somebody.pdf"; string outFile = "With new Header.pdf"; DocumentCore dc = DocumentCore.Load(inpFile); // Create new header with formatted text. HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault); header.Content.Start.Insert("Modified : 1 April 2020", new CharacterFormat() { Size = 14.0, FontColor = Color.DarkGreen }); // Add 10 mm from Top before new header. (header.Blocks[0] as Paragraph).ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point); foreach (Section s in dc.Sections) { // Find the first paragraph (Let's assume that it's the header) and remove it. if (s.Blocks.Count > 0) { s.Blocks.RemoveAt(0); } // Insert the new header into the each section. s.HeadersFooters.Add(header.Clone(true)); } dc.Save(outFile); // Open the results for demonstration purposes. System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(inpFile) { UseShellExecute = true }); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true }); }
/// <summary> /// Parses the keywords «\header». /// </summary> private void ParseHeaderFooter(Section section) { if (section == null) throw new ArgumentNullException("section"); try { Symbol hdrFtrSym = Symbol; bool isHeader = hdrFtrSym == Symbol.Header || hdrFtrSym == Symbol.PrimaryHeader || hdrFtrSym == Symbol.FirstPageHeader || hdrFtrSym == Symbol.EvenPageHeader; // Recall that the styles "Header" resp. "Footer" are used as default if // no other style was given. But this belongs to the rendering process, // not to the DDL parser. Therefore no code here belongs to that. HeaderFooter headerFooter = new HeaderFooter(); ReadCode(); // read '[' or '{' if (Symbol == Symbol.BracketLeft) ParseAttributes(headerFooter); AssertSymbol(Symbol.BraceLeft); if (IsParagraphContent()) { Paragraph paragraph = headerFooter.Elements.AddParagraph(); ParseParagraphContent(headerFooter.Elements, paragraph); } else { ReadCode(); // parse '{' ParseDocumentElements(headerFooter.Elements, Symbol.HeaderOrFooter); } AssertSymbol(Symbol.BraceRight); ReadCode(); // parse beyond '{' HeadersFooters headersFooters = isHeader ? section.Headers : section.Footers; if (hdrFtrSym == Symbol.Header || hdrFtrSym == Symbol.Footer) { headersFooters.Primary = headerFooter.Clone(); headersFooters.EvenPage = headerFooter.Clone(); headersFooters.FirstPage = headerFooter.Clone(); } else { switch (hdrFtrSym) { case Symbol.PrimaryHeader: case Symbol.PrimaryFooter: headersFooters.Primary = headerFooter; break; case Symbol.EvenPageHeader: case Symbol.EvenPageFooter: headersFooters.EvenPage = headerFooter; break; case Symbol.FirstPageHeader: case Symbol.FirstPageFooter: headersFooters.FirstPage = headerFooter; break; } } } catch (DdlParserException ex) { ReportParserException(ex); AdjustToNextBlock(); } }
private void addhaederfooter() { string taskid = HttpContext.Current.Session["Name"].ToString(); HttpContext.Current.Session.Timeout = 60; string inputfile = System.Web.HttpContext.Current.Server.MapPath("~/storepdf/" + taskid + ".pdf"); string outputfile = System.Web.HttpContext.Current.Server.MapPath("~/editorpdf/" + taskid + ".pdf"); //string pictPath = "~/img/caslab.jpg"; DocumentCore dc = DocumentCore.Load(inputfile); HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault); SautinSoft.Document.Paragraph par1 = new SautinSoft.Document.Paragraph(dc); par1.ParagraphFormat.Alignment = HorizontalAlignment.Left; //header.Content.Start.Insert("CASLAB", new CharacterFormat() { Size = 15.0, FontColor = Color.Blue }); CharacterFormat cf1 = new CharacterFormat() { FontName = "Consolas", Size = 15.0, FontColor = Color.Blue }; par1.Content.Start.Insert("CASLAB", cf1.Clone()); header.Blocks.Add(par1); SautinSoft.Document.Paragraph par2 = new SautinSoft.Document.Paragraph(dc); par2.ParagraphFormat.Alignment = HorizontalAlignment.Right; //header.Content.Start.Insert("CASLAB", new CharacterFormat() { Size = 15.0, FontColor = Color.Blue }); CharacterFormat cf2 = new CharacterFormat() { FontName = "Consolas", Size = 15.0 }; par2.Content.Start.Insert(taskid, cf2.Clone()); header.Blocks.Add(par2); SautinSoft.Document.Paragraph par = new SautinSoft.Document.Paragraph(dc); par.ParagraphFormat.Alignment = HorizontalAlignment.Right; CharacterFormat cf = new CharacterFormat() { FontName = "Consolas", Size = 12.0 }; par.Content.Start.Insert("Page", cf.Clone()); Field fPage = new Field(dc, FieldType.Page); fPage.CharacterFormat = cf.Clone(); par.Content.End.Insert(fPage.Content); par.Content.End.Insert(" of ", cf.Clone()); Field fPages = new Field(dc, FieldType.NumPages); fPages.CharacterFormat = cf.Clone(); //header.Content.Start.Insert(taskid, new CharacterFormat() { Size = 15.0, FontColor = Color.Black }); par.Content.End.Insert(fPages.Content); header.Blocks.Add(par); SautinSoft.Document.Paragraph par3 = new SautinSoft.Document.Paragraph(dc); par3.ParagraphFormat.Alignment = HorizontalAlignment.Center; //header.Content.Start.Insert("CASLAB", new CharacterFormat() { Size = 15.0, FontColor = Color.Blue }); CharacterFormat cf3 = new CharacterFormat(); par3.Content.Start.Insert("______________________________________________________________________________________________", cf3.Clone()); header.Blocks.Add(par3); //Picture pict1 = new Picture(dc, InlineLayout.Inline(new Size(100, 100)), pictPath); foreach (SautinSoft.Document.Section s in dc.Sections) { s.HeadersFooters.Add(header.Clone(true)); } HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault); SautinSoft.Document.Paragraph par4 = new SautinSoft.Document.Paragraph(dc); par4.ParagraphFormat.Alignment = HorizontalAlignment.Center; //header.Content.Start.Insert("CASLAB", new CharacterFormat() { Size = 15.0, FontColor = Color.Blue }); CharacterFormat cf4 = new CharacterFormat(); par4.Content.Start.Insert("______________________________________________________________________________________________", cf4.Clone()); footer.Blocks.Add(par4); par4.Content.End.Insert("This document and all the information contained here are confidential and exclusive property of CASLAB and maynot be reproduced,disclosed,or made public in any manner prior to express written authorization by CASLAB.", new CharacterFormat() { Size = 12.0, FontColor = Color.Black }); foreach (SautinSoft.Document.Section s in dc.Sections) { s.HeadersFooters.Add(footer.Clone(true)); } dc.Save(outputfile); }