How to remove footer template from first page? - c#

Using cete Dynamic PDFtool I want to remove the footer template from the first page of the document. How to achieve this?
Page.Elements.Add(tblcontent);
Document.Pages.Add(Page);

The page class has an ApplyDocumentTemplate property that you can set to false, if you do not want a the template to be applied to a particular page.
Document document = new Document();
Template template = new Template();
// Add elements to template
document.Template = template;
Page page1 = new Page(PageSize.Letter);
// Add elements to page
page1.ApplyDocumentTemplate = false;
// Add additional pages leaving ApplyDocumentTemplate as true
// Save the PDF
document.Draw("output.pdf");
You can also accomplish this by using document sectioning. When a document is broken up into sections, each section can have its own template, or not have a template at all. In this following example, the first section doesn't have a template so the page numbers are not shown for the first two pages, and the second section does so page numbers are shown for the last 3 pages.
Document document = new Document();
// Create a template object and add a page numbering label
Template template = new Template();
template.Elements.Add(new PageNumberingLabel("%%SP%% of %%ST%%", 0, 680, 512, 12, Font.Helvetica, 12, TextAlign.Center));
// Begin the first section
document.Sections.Begin(NumberingStyle.RomanLowerCase);
// Add two pages
document.Pages.Add(new Page()); //Page 1
document.Pages.Add(new Page()); //Page 2
// Begin the second section
document.Sections.Begin(NumberingStyle.Numeric, template);
// Add three pages
document.Pages.Add(new Page()); //Page 3
document.Pages.Add(new Page()); //page 4
document.Pages.Add(new Page()); //page 5
// Save the PDF
document.Draw("output.pdf");
Here is a link to a topic on document sectioning:
http://docs.dynamicpdf.com/NET_Help_Library_19_08/Document%20Sectioning.html

Related

newLineAtOffset working inverse using pdfbox

When I'm appending the text in pdf using pdfbox is working fine but the problem is that text append inversely.
Example:
I'm adding some text in (x,y)=(0,0)
The text added in last of the page.
The text should be added in the first of the page.
PDDocument pDDocument = PDDocument.load(new java.io.File(filePath));
PDPage page = new PDPage();
page = pDDocument.getPages().get(0);
PDPageContentStream pDPageContentStream = new PDPageContentStream(pDDocument,page,PDPageContentStream.AppendMode.APPEND,true,true);
pDPageContentStream.setFont(org.apache.pdfbox.pdmodel.font.PDType1Font.TIMES_BOLD_ITALIC, 12);
pDPageContentStream.beginText();
pDPageContentStream.newLineAtOffset(0, 0);
pDPageContentStream.showText("Welcome!");
pDPageContentStream.endText();
pDPageContentStream.close();
pDDocument.save(newFilePath);
pDDocument.close();
When I'm set the position of the newLineAtOffset(0,0).
Expected result, text should be added in the first of the page.

How to add content template to RadPanelItem in RadPanelbar programatically

I am working with asp.net page with telerik, in which i have a telerik radgrid, when click on the grid edit, need to fetch some data that matches present scenario and that data in the form of data table containing multiple rows,
Now based on the no of rows in data table i need to generate RadPanelItems programmatically, i achieved this by using the following code
for (int i = 0; i < dtCompletedCust.Rows.Count; i++)
{
RadPanelItem panelItem = new RadPanelItem();
panelItem.Text = dtCompletedCust.Rows[i]["CustName"].ToString();
pnlReviewedCust.Items.Add(panelItem);
}
It is successfully adding me the no of RadPanelItems,
Now my requirement is that I need to add content template to each of this newly added RadPanelItem and this content template contains multiple controls,
can any one help me or suggest some thing on this?
Create a class that will inherit ITemplate: https://msdn.microsoft.com/en-us/library/system.web.ui.itemplate(v=vs.110).aspx and instantiate it: https://msdn.microsoft.com/en-us/library/system.web.ui.itemplate.instantiatein(v=vs.110).aspx
Something like:
RadPanelItem panelItem = new RadPanelItem();
panelItem.Text = dtCompletedCust.Rows[i]["CustName"].ToString();
panelItem.ContentTemplate = myITemplateClass;
pnlReviewedCust.Items.Add(panelItem);
Or use its Controls collection:
RadPanelItem panelItem = new RadPanelItem();
panelItem.Text = dtCompletedCust.Rows[i]["CustName"].ToString();
panelItem.Controls.Add(new LiteralControl(DateTime.Now.ToString()));
pnlReviewedCust.Items.Add(panelItem);

Using iTextSharp to construct header on multiple pdf pages

I'm generating a PDF file based on a record selected in my datagridview. It will consist of 3-5 pages. I created a table with 2 columns to represent my header. the first cell is left aligned and the 2nd cell is right aligned. I want this same inforamtion displayed on all pages.
After doing some googling, I saw a header.WriteSelectedRows() property which is supposed to help with that? One example was :
header.WriteSelectedRows(0, -1, doc.PageSize.GetLeft(5), doc.PageSize.GetTop(5), wri.DirectContent);
2nd was:
header.WriteSelectedRows(0, -1, doc.LeftMargin, doc.PageSize.Height - 36, wri.DirectContent);
However both resulted in just the first page having the table/header. Any ideas on what I need to fix? Thanks!
Code:
PdfPTable header = new PdfPTable(2);
header.HorizontalAlignment = Element.ALIGN_LEFT;
header.TotalWidth = doc.PageSize.Width - 20f;
header.LockedWidth = true;
Phrase cell1 = new Phrase(signal.ProformaType);
Phrase cell2 = new Phrase("text" + Environment.NewLine + "text"
+ Environment.NewLine + signal.Signal);
PdfPCell c1 = new PdfPCell(cell1);
c1.Border = iTextSharp.text.Rectangle.NO_BORDER;
c1.VerticalAlignment = iTextSharp.text.Element.ALIGN_TOP;
c1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
header.AddCell(c1);
PdfPCell c2 = new PdfPCell(cell2);
c2.Border = iTextSharp.text.Rectangle.NO_BORDER;
c2.VerticalAlignment = iTextSharp.text.Element.ALIGN_TOP;
c2.HorizontalAlignment = iTextSharp.text.Element.ALIGN_RIGHT;
header.AddCell(c2);
header.WriteSelectedRows(0, -1, doc.LeftMargin, doc.PageSize.Height - 36, wri.DirectContent);
The PdfPTable is added to the first page only because you are adding it to the first page only. If you want to add it to every page that is created by iText, you shouldn't add the PdfPTable where you are adding it now.
Instead you should add it in the OnEndPage() method of a page event. This is explained in answers to questions such as:
How can I add Header and footer in pdf using iText in java?
how to add an image to my header in iText generated PDF?
How to handle the case in wich an iText\iTextSharp table is splitted in two pages?
...
In other words, you need to create your own implementation of the PdfPageEvent interface. The best way is to extend the PdfPageEventHelper class:
public class MyPageHeader : PdfPageEventHelper
{
PdfPTable header = ... // define header table here
public override void OnEndPage(PdfWriter writer, Document document)
{
header.WriteSelectedRows(0, -1, document.Left, document.Top, writer.DirectContent);
}
}
To make this work, you need to declare this page event before opening the Document:
PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfFileStream);
pdfWriter.PageEvent = new MyPageHeader();
document.Open();
Now, every time a new page is created, the header will be added automatically.
You may want to adapt document.Left and document.Top in the code above, because right now, it will add the table in the upper-right corner of each page, you may want to use document.Left + 36 and document.Top - 5 or something like that.
Also: make sure that there is sufficient room for the header, otherwise your header will overlap with the content you are adding straight to the Document using document.Add(). You can change the margins in the constructor of the Document class.

set the footer in place

setting the footer..
in the above screen the footer is displayed after new member report and reporting period but i dont want like this
I'm working on windows application using C#.
I have generated the "Report" using Ms chart control. While printing and exporting into XPS format, Header and Footer are appearing its fine .
But i want the Footer will be displayed at the bottom of the report at present it was appearing just after the header, I want to add this Footer at the bottom using C#. So user can print page with Header and Footer. How to do this.
can any one have idea about this ..
Many thanks....
this is my code
Title maintitle = kpiChartControl.Titles.Add("New Members Report" + Environment.NewLine);
maintitle.Alignment = ContentAlignment.TopLeft;
maintitle.Font = new Font(FontFamily.GenericSansSerif, 11, FontStyle.Bold);
Title rangetitle = kpiChartControl.Titles.Add(string.Format("Report period from : {0} to {1}{2}", dStartDate.Value.ToString(xxx.dateFormat),
denddate.Value.ToString(xxxx.dateFormat), Environment.NewLine));
rangetitle.Alignment = ContentAlignment.TopLeft;
rangetitle.Font = new Font(FontFamily.GenericSansSerif, 11, FontStyle.Bold);
Title footertitle = kpiChartControl.Titles.Add("--------------------------------------------------------" + Environment.NewLine);
footertitle.Alignment = ContentAlignment.BottomCenter;
Title gompanytitle = kpiChartControl.Titles.Add("xxxx");
gompanytitle.Alignment = ContentAlignment.BottomLeft;
gompanytitle.Font = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular);
Title printedby = kpiChartControl.Titles.Add(string.Format("Printed By ("+text+") On :{0}", dt,Environment.NewLine));
printedby.Alignment = ContentAlignment.BottomRight;
printedby.Font = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular);
kpiChartControl.Printing.Print(true);
kpiChartControl.Titles.Remove(maintitle);
kpiChartControl.Titles.Remove(rangetitle);
kpiChartControl.Titles.Remove(footertitle);
kpiChartControl.Titles.Remove(gompanytitle);
kpiChartControl.Titles.Remove(printedby);
The trick is to use the Docking property. So for all items that you wish to place below the graph, do the following
printedby.Docking = Docking.Bottom;
gompanytitle.Docking = Docking.Bottom;
footertitle.Docking = Docking.Bottom;
I'm kinda guessing here to be honest - but it seems to me that you need to be adding the footer in the XPS document, rather than as part of the chart. My guess is that the chart will always by displaying titles at the top.
This SO - about adding header/footer to an XPS - should help: add footer to FlowDocumentsdocuments, both Sauron's and Vikram's answers there should provide the information you need.

How to identify end of page is reached in pdf file using itextsharp

Hi
I am using itextsharp to generate a pdf file.I am placing a backgound image on it and want that image on all the pages .But when the first page is completed the text move to next page automatically so the image is not appearing on the new page.
Is there a way to identify the end of page so that we can add a new page and then set the image first so will appear in background and then can add the remaining text.
All is i want a image in background on all the pages of pdf file.
I suggest you use a page event:
myWriter.setPageEvent(new BackgroundPageEvent(backgroundImage));
class BackgroundPageEvent extends PdfPageEventHelper {
Image backgroundImage = null;
public BackgroundPageEvent( Image img ) {
backgroundImage = img;
}
public void onStartPage(PdfWriter writer, Document doc) {
PdfContentByte underContent = writer.getDirectContentUnder();
underContent.addImage(backgroundImage);
}
}
With the above code, backgroundImage will be added to the "under content" as each page is created. No need to worry about when to add it yourself... iText will figure that out for you, and the first thing in the underContent of each page will be your image. You might need to play around with the various overrides of addImage to get the size you want.
I believe you can also query doc for the current page size if it varies in your program. If not, you should be able to create the image you pass in with an absolute position/scale (which may be what you're doing already).
PdfPageEvent has a number of other events you can override. PdfPageEventHelper covers all the bases with "no ops" so you can just override the event[s] you want:
OnStartPage
OnEndPage
OnCloseDocument
OnParagraph
OnParagraphEnd
OnChapter
OnChapterEnd
OnSection
OnSectionEnd
OnGenericTag
Generic tag is actually Really Handy. You can give a generic tag (a string) to just about anything within your document, and your OnGenericTag override will be called with the rect that was used to draw whatever it was you tagged. All kinds of spiffy possibilities.
Just check PdfWriter.PageNumber property like this:
using (FileStream fs = File.Create("test.pdf"))
{
Document document = new Document(PageSize.A4, 72, 72, 72, 72);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
int pageNumber = -1;
for (int i = 0; i < 20; i++)
{
if (pageNumber != writer.PageNumber)
{
// Add image
pageNumber = writer.PageNumber;
}
// Add something else
}
document.Close();
}

Categories

Resources