I need to add an HTML block as the header of all pages using iText7. The header contains an image logo and some text.
I have this at the moment, before closing the document object:
for (int i = 1; i <= n; i++)
{
float x = pdf.GetPage(i).GetPageSize().GetWidth() / 2; // 297.5f
float yFooter = 20;
if (headerBlock != null)
{
float yHeader = 600;// pdf.GetPage(i).GetPageSize().GetTop() - 20;
// Header
document.ShowTextAligned(headerBlock, 0, yHeader, page, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
}
// Footer
Paragraph footerBlock = new Paragraph(String.Format("Página {0} de {1}", i, n));
document.ShowTextAligned(footerBlock, x, yFooter, page, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
}
Footer works correctly but header.
Header was loaded this way:
Paragraph headerBlock = String.IsNullOrWhiteSpace(header) ? null : CreateHtmlParagraph(header);
where, CreateHtmlParagraph is defined this way:
private Paragraph CreateHtmlParagraph(string html)
{
Paragraph p = new Paragraph();
ConverterProperties properties = new ConverterProperties();
properties.SetBaseUri(HttpContext.Current.Server.MapPath("/"));
var elements = HtmlConverter.ConvertToElements(html, properties);
foreach (IElement e in elements)
p.Add((IBlockElement)e);
return p;
}
When I add the header using the document.Add method, it works well, but for the first page only. All other content follows it.
When I try to add it using ShowTextAligned method, only the image is rendered in all pages.
By the way, is there a way to get the actual height of the header paragraph? I think, once the header positioning is solved, I will have the problem that the other content blocks will be overlapped by the header.
I believe you need to use page events. This is well documented. Create a class that implements IEventHandler that will handle specific events.
Add a event handler for a specific event
pdf.AddEventHandler(PdfDocumentEvent.START_PAGE, new StartPageEventHandler());
StartPageEventHandler is a class you create, implementing IEventHandler. You'll likely need to take this approach for both header and footer.
See this link for more info
Related
I am iterating through all storyranges in a word document, to find shapes that do not adhere to company standards. When I find such a shape, I would like to add a textbox in the upper right corner of it's page. I only managed to add it to the first page so far.
foreach (Word.InlineShape shape in storyRange.InlineShapes)
{
if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)
{
if (shape.Width != CurrentWordApp.CentimetersToPoints(Constants.LogoWidth))
{
anchor = shape.Range;
shapePageNumber = (int)shape.Range.Information[Word.WdInformation.wdActiveEndPageNumber];
AddMarkerToPage(shapePageNumber, anchor);
}
}
}
This is an excerpt from the AddMarkerToPage method. The only place I found to add a textbox, is the header. And the only place I found the header was through the section object. But section does not equal page.
Word.HeaderFooter header = anchor.Sections.First.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
if (!pageHasMarker)
{
Word.Shape tbx = header.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerFromLeft),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerFromTop),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerWidth),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerHeight), pageRange);
tbx.Name = "errorBox";
tbx.TextFrame.TextRange.Text = Resources.Strings.txtDesignCheckHeaderLogo;
}
}
How can I either get to the header on the page the shape is on, or have another object that allows me to position a textbox on a specific page (I have the number available from the shape object)
Don't use the header, use the Document.Shapes collection.
Word.Shape tbx = <document>.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerFromLeft),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerFromTop),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerWidth),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerHeight), anchor);
tbx.Name = "errorBox";
tbx.TextFrame.TextRange.Text = Resources.Strings.txtDesignCheckHeaderLogo;
I think you'll also need these:
tbx.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
tbx.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionColumn;
I have an application that uses itextsharp to fill PDF form fields.
One of these fields has some text with tags. For example:
<U>This text should be underlined</>.
I'd like that the text closed in .. has to be underlined.
How could I do that?
How could I approch it with HTMLWorker for example?
Here's the portion of code where I write my description:
for (int i = 0; i < linesDescription.Count; i++)
{
int count = linesDescription[i].Count();
int countTrim = linesDescription[i].Trim().Count();
Chunk cnk = new Chunk(linesDescription[i] + GeneralPurpose.ReturnChar, TextStyle);
if (firstOpe && i > MaxLinePerPage - 1)
LongDescWrapped_dt_extra.Add(cnk);
else
LongDescWrapped_dt.Add(cnk);
}
Ordinary text fields do not support rich text. If you want the fields to remain interactive, you will need RichText fields. These are fields that are flagged in a way that they accept an RV value. This is explained here: Set different parts of a form field to have different fonts using iTextSharp (Note that I didn't succeed in getting this to work, but you may have better luck.)
If it is OK for you to flatten the form (i.e. remove all interactivity), please take a look at the FillWithUnderline example:
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.setFormFlattening(true);
AcroFields form = stamper.getAcroFields();
FieldPosition pos = form.getFieldPositions("Name").get(0);
ColumnText ct = new ColumnText(stamper.getOverContent(pos.page));
ct.setSimpleColumn(pos.position);
ElementList elements = XMLWorkerHelper.parseToElementList("<div>Bruno <u>Lowagie</u></div>", null);
for (Element element : elements) {
ct.addElement(element);
}
ct.go();
stamper.close();
}
In this example, we don't fill out the field, but we get the fields position (a page number and a rectangle). We then use ColumnText to add content at this position. As we are inputting HTML, we use XML Worker to parse the HTML into iText objects that we can add to the ColumnText object.
This is a Java example, but it should be easy to port this to C# if you know how to code in C# (which I don't).
You can trythis
Chunk chunk = new Chunk("Underlined TExt", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12.0f, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.UNDERLINE));
Paragraph reportHeadline = new Paragraph(chunk);
reportHeadline.SpacingBefore = 12.0f;
pdfDoc.Add(reportHeadline);
I would like to know if there is a way to check if the "New Page" happened because of exceeded table or programmatically (by using doc.NewPage();)?
If the new page caused because of exceeded table or text, I need to hide the header table and show a text instead, else if the new page caused programmatically I need to display the header table normally.
I tried to find a flag or something like this in the "OnStartPage" event that show me if the page exceeded or not, but I found nothing.
I hope that some one can help me here.
Thanks!
I would look at the IPdfPTableEventSplit interface that you can implement and assign to a PdfPTable.TableEvent property. It has two methods, SplitTable and TableLayout. The first method is called whenever a table split happens, the second is called whenever the table actually gets written to the canvas. In the first method you could set a flag and disable the header rows if a split happened and in the second method you could write your content out.
The SplitTable method is fired before the new page is added so you need to keep track of a trinary state, "no split", "draw on next page" and "draw on this page". I've packaged these up as an enum:
[Flags]
public enum SplitState {
None = 0,
DrawOnNextPage = 1,
DrawOnThisPage = 2
}
The implemented interface would look like this:
public class SplitTableWatcher : IPdfPTableEventSplit {
/// <summary>
/// The current table split state
/// </summary>
private SplitState currentSplitState = SplitState.None;
public void SplitTable(PdfPTable table) {
//Disable header rows for automatic splitting (per OP's request)
table.HeaderRows = 0;
//We now need to split on the next page, so append the flag
this.currentSplitState |= SplitState.DrawOnNextPage;
}
public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
//If a split happened and we're on the next page
if (this.currentSplitState.HasFlag(SplitState.DrawOnThisPage)) {
//Draw something, nothing too special here
var cb = canvases[PdfPTable.TEXTCANVAS];
cb.BeginText();
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false), 18);
//Use the table's widths and heights to find a spot, this probably could use some tweaking
cb.SetTextMatrix(widths[0][0], heights[0]);
cb.ShowText("A Split Happened!");
cb.EndText();
//Unset the draw on this page flag, it will be reset below if needed
this.currentSplitState ^= SplitState.DrawOnThisPage;
}
//If we previously had the next page flag set change it to this page
if (currentSplitState.HasFlag(SplitState.DrawOnNextPage)) {
this.currentSplitState = SplitState.DrawOnThisPage;
}
}
}
And finally the actual implementation of that class with some simple test data:
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
var t = new PdfPTable(1);
//Implement our class
t.TableEvent = new SplitTableWatcher();
//Add a single header row
t.HeaderRows = 1;
t.AddCell("Header");
//Create 100 test cells
for (var i = 1; i < 100; i++) {
t.AddCell(i.ToString());
}
doc.Add(t);
doc.Close();
}
}
}
I have a problem using Winnovative Html to Pdf Converter v8.0.0.0, my pdf page number are not correct.
Here is what I 've done.
I set a footer on my converter
pdfConverter.PdfFooterOptions.AddTextArea(new TextArea(500, 0, 50, "&p; / &P;", new Font(new FontFamily("Arial"), 8)));
then I create a document from a url
pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(FirstUrl);
then I append a document from a url
pdfDocument.AppendDocument(pdfConverter.GetPdfDocumentObjectFromUrl(SecondUrl));
all the pages resulting from the first url contain a correct page number, but page number on the appended document does not seems to be recalculate
example :
FirstUrl result in 3 pages and second in 2 pages.
Page Numbers are
1/5
2/5
3/5
1/2
2/2
I want them to be like :
1/5
2/5
3/5
4/5
5/5
I also try not to add the footer to the converter but to the pdfDocument after the conversion like this:
PdfFont pdfFont = pdfDocument.Fonts.Add(new Font(new FontFamily("Arial"), 8));
string strPageNum = "&p; / &P;";
TextElement footerPageNumber = new TextElement(500, 0, 50, strPageNum, pdfFont);
pdfDocument.FooterTemplate.AddElement(footerPageNumber);
But in this case. Only pages from the first Url get page number on them and are nowhere to be seen on page from appended document.
Thanks for your help.
After encountering the same problem with another project. I finally find the correct way of creating a pdf from many Url.
I Still set the footer on the document like this:
pdfConverter.PdfFooterOptions.AddTextArea(new TextArea(500, 0, 50, "&p; / &P;", new Font(new FontFamily("Arial"), 8)));
then I create a document from a url just as before:
pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(firstUrl);
but instead of appending another pdf I get the second url in a HtmlToPdfElement like this:
HtmlToPdfElement pdfBody = new HtmlToPdfElement(secoundUrl);
and finally add it to the pdf document using a new page.
PdfPage newPage = pdfDocument.Pages.AddNewPage();
newPage.AddElement(pdfBody);
In the latest version of Winovative HTML to PDF converter you can add now headers and footers in external PDF documents appeneded or prepended to the result of conversion. You can find a complete example with C# source code in Add Header and Footer In External PDF Documents demo
Here is the relevant code copied from that demo application:
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set the PDF file to be inserted before conversion result
string pdfFileBefore = Server.MapPath("~/DemoAppFiles/Input/PDF_Files/Merge_Before_Conversion.pdf");
htmlToPdfConverter.PdfDocumentOptions.AddStartDocument(pdfFileBefore, addHeaderFooterInInsertedPdfCheckBox.Checked,
showHeaderInFirstPageCheckBox.Checked, showFooterInFirstPageCheckBox.Checked);
// Set the PDF file to be added after conversion result
string pdfFileAfter = Server.MapPath("~/DemoAppFiles/Input/PDF_Files/Merge_After_Conversion.pdf");
htmlToPdfConverter.PdfDocumentOptions.AddEndDocument(pdfFileAfter, addHeaderFooterInAppendedPdfCheckBox.Checked, true, true);
// Enable header in the generated PDF document
htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true;
// Draw header elements
if (htmlToPdfConverter.PdfDocumentOptions.ShowHeader)
DrawHeader(htmlToPdfConverter, true);
// Enable footer in the generated PDF document
htmlToPdfConverter.PdfDocumentOptions.ShowFooter = true;
// Draw footer elements
if (htmlToPdfConverter.PdfDocumentOptions.ShowFooter)
DrawFooter(htmlToPdfConverter, true, true);
string url = urlTextBox.Text;
// Convert the HTML page to a PDF document and add the external PDF documents
byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Header_Footer_in_External_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
/// <summary>
/// Draw the header elements
/// </summary>
/// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param>
/// <param name="drawHeaderLine">A flag indicating if a line should be drawn at the bottom of the header</param>
private void DrawHeader(HtmlToPdfConverter htmlToPdfConverter, bool drawHeaderLine)
{
string headerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Header_HTML.html");
// Set the header height in points
htmlToPdfConverter.PdfHeaderOptions.HeaderHeight = 60;
// Set header background color
htmlToPdfConverter.PdfHeaderOptions.HeaderBackColor = Color.White;
// Create a HTML element to be added in header
HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl);
// Set the HTML element to fit the container height
headerHtml.FitHeight = true;
// Add HTML element to header
htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml);
if (drawHeaderLine)
{
// Calculate the header width based on PDF page size and margins
float headerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width -
htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin;
// Calculate header height
float headerHeight = htmlToPdfConverter.PdfHeaderOptions.HeaderHeight;
// Create a line element for the bottom of the header
LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1);
// Set line color
headerLine.ForeColor = Color.Gray;
// Add line element to the bottom of the header
htmlToPdfConverter.PdfHeaderOptions.AddElement(headerLine);
}
}
/// <summary>
/// Draw the footer elements
/// </summary>
/// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param>
/// <param name="addPageNumbers">A flag indicating if the page numbering is present in footer</param>
/// <param name="drawFooterLine">A flag indicating if a line should be drawn at the top of the footer</param>
private void DrawFooter(HtmlToPdfConverter htmlToPdfConverter, bool addPageNumbers, bool drawFooterLine)
{
string footerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Footer_HTML.html");
// Set the footer height in points
htmlToPdfConverter.PdfFooterOptions.FooterHeight = 60;
// Set footer background color
htmlToPdfConverter.PdfFooterOptions.FooterBackColor = Color.WhiteSmoke;
// Create a HTML element to be added in footer
HtmlToPdfElement footerHtml = new HtmlToPdfElement(footerHtmlUrl);
// Set the HTML element to fit the container height
footerHtml.FitHeight = true;
// Add HTML element to footer
htmlToPdfConverter.PdfFooterOptions.AddElement(footerHtml);
// Add page numbering
if (addPageNumbers)
{
// Create a text element with page numbering place holders &p; and & P;
TextElement footerText = new TextElement(0, 30, "Page &p; of &P; ",
new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point));
// Align the text at the right of the footer
footerText.TextAlign = HorizontalTextAlign.Right;
// Set page numbering text color
footerText.ForeColor = Color.Navy;
// Embed the text element font in PDF
footerText.EmbedSysFont = true;
// Add the text element to footer
htmlToPdfConverter.PdfFooterOptions.AddElement(footerText);
}
if (drawFooterLine)
{
// Calculate the footer width based on PDF page size and margins
float footerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width -
htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin;
// Create a line element for the top of the footer
LineElement footerLine = new LineElement(0, 0, footerWidth, 0);
// Set line color
footerLine.ForeColor = Color.Gray;
// Add line element to the bottom of the footer
htmlToPdfConverter.PdfFooterOptions.AddElement(footerLine);
}
}
I have taken the link values from PDF file like http://google.com
but I need to take the anchor text value, for example click here.
How to to take the anchor link value text?
I have taken the URL value of the PDF file by using the below URL: Reading hyperlinks from pdf file
for example.
Anchor a = new Anchor("Test Anchor");
a.Reference = "http://www.google.com";
myParagraph.Add(a);
Here I get the http://www.google.com but I need to get anchor value i.e. Test Anchor
Need your suggestions.
From the PDF file you need to identify the region where the link is placed and then read the text below the link using iTextSharp.
This way you can extract the text underneath the link. The limitation of this approach is that if the link region is wider than the text, the extraction will read the full text under that region.
private void GetAllHyperlinksFromPDFDocument(string pdfFilePath)
{
string linkTextBuilder = "";
string linkReferenceBuilder = "";
PdfDictionary PageDictionary = default(PdfDictionary);
PdfArray Annots = default(PdfArray);
PdfReader R = new PdfReader(pdfFilePath);
List<BinaryHyperlink> ret = new List<BinaryHyperlink>();
//Loop through each page
for (int i = 1; i <= R.NumberOfPages; i++)
{
//Get the current page
PageDictionary = R.GetPageN(i);
//Get all of the annotations for the current page
Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);
//Make sure we have something
if ((Annots == null) || (Annots.Length == 0))
continue;
//Loop through each annotation
foreach (PdfObject A in Annots.ArrayList)
{
//Convert the itext-specific object as a generic PDF object
PdfDictionary AnnotationDictionary = (PdfDictionary)PdfReader.GetPdfObject(A);
//Make sure this annotation has a link
if (!AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
continue;
//Make sure this annotation has an ACTION
if (AnnotationDictionary.Get(PdfName.A) == null)
continue;
//Get the ACTION for the current annotation
PdfDictionary AnnotationAction = (PdfDictionary)AnnotationDictionary.GetAsDict(PdfName.A);
if (AnnotationAction.Get(PdfName.S).Equals(PdfName.URI))
{
//Get action link URL : linkReferenceBuilder
PdfString Link = AnnotationAction.GetAsString(PdfName.URI);
if (Link != null)
linkReferenceBuilder = Link.ToString();
//Get action link text : linkTextBuilder
var LinkLocation = AnnotationDictionary.GetAsArray(PdfName.RECT);
List<string> linestringlist = new List<string>();
iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(((PdfNumber)LinkLocation[0]).FloatValue, ((PdfNumber)LinkLocation[1]).FloatValue, ((PdfNumber)LinkLocation[2]).FloatValue, ((PdfNumber)LinkLocation[3]).FloatValue);
RenderFilter[] renderFilter = new RenderFilter[1];
renderFilter[0] = new RegionTextRenderFilter(rect);
ITextExtractionStrategy textExtractionStrategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), renderFilter);
linkTextBuilder = PdfTextExtractor.GetTextFromPage(R, i, textExtractionStrategy).Trim();
}
}
}
}
Unfortunately I don't think you're going to be able to do this, at least not without a lot of guess-work. In HTML this would be easy because a hyperlink and its text are stored together as:
Click here
However, in a PDF these two entities are not stored with any form of relationship. What we think of as a "hyperlink" within a PDF is technically a PDF Annotation that just happens to be sitting on top of text. You can see this by opening a PDF in an editing program such as Adobe Acrobat Pro. You can change the text but the "clickable" area doesn't change. You can also move and resize the "clickable" area and put it anywhere in the document.
When creating PDFs, iText/iTextSharp abstract this away so you don't have to think about this. You can create a "hyperlink" with clickable text but when it generates a PDF it ultimately will create the text as normal text, calculate the rectangle coordinates and then put an annotation at that rectangle.
I did say that you could try to guess at this, and it might or might not work for you. To do this you'd need to get the rectangle for annotation and then find the text that's also at those coordinates. It won't be an exact match, however, because of padding issues. If you absolutely have to get the text under a hyperlink then this is the only way that I know of for doing this. Good luck!