I'm trying to create a docx document that has header and footer for all pages.
For the header i want the image to ocuppy all of the header. Like the image below:
For the footer i want it to be on the left and at the right i want to have the number of the page. Like the image below:
what i have right now is:
using (var docx = DocX.Create(filename))
{
docx.AddHeaders();
docx.AddFooters();
var headerDefault = docx.Headers.odd;
var footerDefault = docx.Footers.odd;
Novacode.Paragraph hp = headerDefault.InsertParagraph();
Novacode.Paragraph fp = footerDefault.InsertParagraph();
Novacode.Image logoHeader = docx.AddImage(System.Web.HttpContext.Current.Server.MapPath("/Images/jpg/header_pdf.jpg"));
Novacode.Image logoFooter = docx.AddImage(System.Web.HttpContext.Current.Server.MapPath("/Images/jpg/footer_pdf.jpg"));
hp.AppendPicture(logoHeader.CreatePicture());
fp.AppendPicture(logoFooter.CreatePicture());
The problem is that both the header and the footer get the margins of the rest of the document and even if i do
docx.MarginTop = 0F;
docx.MarginRight = 0F;
docx.MarginBottom = 0F;
docx.MarginLeft = 0F;
there will still be a top margin on the header and a bottom margin on the footer.
Does anyone have a solution? thanks
Late answer, but maybe it would be useful for someone - I faced the same problem. The problem is here:
Novacode.Paragraph hp = headerDefault.InsertParagraph();
Novacode.Paragraph fp = footerDefault.InsertParagraph();
Header and footer already have paragraph, you should just get it:
Novacode.Paragraph hp = headerDefault.Paragraphs.First();
So the margins appeared because there were two paragraphs, one of them was empty but had new line symbol.
Related
I am generating a pdf from others with IText7 in C# and I need to add information in a corner of each page automatically, I add the information but in some pages, the content occupies the entire page and I need the information to be left behind in those cases of the content, I attach the image of the result, thanks in advance
I am using a canvas and Div to add the text
PdfPage pag = pdfDocument.GetLastPage();
Canvas canvas = new Canvas(pag, pag.GetPageSize());
var div = new Div().SetMargin(0).SetPadding(0).SetKeepTogether(true);
div.SetFixedPosition(0, 0, pag.GetPageSize().GetWidth());
div.Add(new Paragraph("info").SetFontSize(12));
canvas.Add(div);
canvas.Close();
Sorry my English
result:
Pdf text added result
expected:
Pdf text added Expected
I have found the solution, I create a PDFCanvas and indicate that NewContentStreamBefore(), and it already adds the information behind the page
PdfPage pag = pdfDocument.GetLastPage();
PdfCanvas under = new PdfCanvas(pag.NewContentStreamBefore(), pag.GetResources(), pdfDocument);
Canvas canvas = new Canvas(under, pag.GetPageSize());
var div = new Div().SetMargin(0).SetPadding(0).SetKeepTogether(true);
div.SetFixedPosition(0, 0, pag.GetPageSize().GetWidth());
div.Add(new Paragraph("info").SetFontSize(12));
canvas.Add(div);
canvas.Close();
I'm trying to add a watermark in a pdf file in the center bottom of pdf file .. I tried many ways but i did not figure out how it's works exactly to select the location of watermark.
Anyway here is the code:
string filepath = #"~/Images/logo-01.png";
iTextSharp.text.Image pageIn = iTextSharp.text.Image.GetInstance(Server.MapPath(filepath));
pageIn.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
pageIn.ScaleToFit(150, 150);
pageIn.Alignment = iTextSharp.text.Image.UNDERLYING;
pageIn.SetAbsolutePosition((PageSize.A4.Width - pageIn.ScaledWidth) / 2, (PageSize.A4.Height - pageIn.ScaledHeight) / 10);
Again i want to make the watermark position in the max center bottom of the page.
You may try using GroupDocs.Watermark for .NET that allows controlling the horizontal and vertical position of the watermark in the documents. This is how you can add watermark at the bottom of the pages in a PDF document:
using (PdfDocument doc = Document.Load<PdfDocument>("D:\\sample.pdf"))
{
// Add text watermark
TextWatermark watermark = new TextWatermark("Protected Document", new Font("Arial", 8));
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.VerticalAlignment = VerticalAlignment.Bottom;
doc.AddWatermark(watermark);
// Add image watermark
ImageWatermark imageWatermark = new ImageWatermark("D:\\watermark.png");
imageWatermark.HorizontalAlignment = HorizontalAlignment.Center;
imageWatermark.VerticalAlignment = VerticalAlignment.Bottom;
doc.AddWatermark(imageWatermark);
// Save document
doc.Save("D:\\output.pdf");
}
Disclosure: I work as a Developer Evangelist at GroupDocs.
I am trying to put some image in header of Word document through Microsoft. Office.Interop.Word.
I want to place the picture stretched to both boundaries of document but I am unable to do so; whenever I set right and left indent it only changes the left indent like image is only stretched on the left side but not on the right side, Any type of help will be very precious to me. This is the snippet I am trying with:
foreach (Microsoft.Office.Interop.Word.Section wordSection in docWord.Sections)
{
wordSection.PageSetup.HeaderDistance = 0;
Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
//footerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
//footerRange.ParagraphFormat.LeftIndent = -(docWord.Application.CentimetersToPoints(3));
footerRange.InlineShapes.AddPicture(#"C:\\test\\footer.png");
Microsoft.Office.Interop.Word.Range headerRange = wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
//headerRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
MessageBox.Show((headerRange.ParagraphFormat.RightIndent - (docWord.Application.CentimetersToPoints(72))).ToString());
//headerRange.ParagraphFormat.RightIndent = (docWord.Application.InchesToPoints(-1));
//headerRange.ParagraphFormat.LeftIndent = -(docWord.Application.CentimetersToPoints(3));
headerRange.ParagraphFormat.FirstLineIndent = -(docWord.Application.CentimetersToPoints(3));
//MessageBox.Show(headerRange.ParagraphFormat.RightIndent.ToString());
//headerRange.ParagraphFormat.SpaceBefore =0 ;
headerRange.InlineShapes.AddPicture(#"C:\\test\\header.png");
}
I have worked with Microsoft.interope Library. You are doing it correctly. Unfortunately, Microsoft.interope does not provide support for stretch header.
I am binding datatable to ASP.NET chart for which I am using custom label for X-Axis. The label text is aligned vertically and the chart size gets reduced.
I tried the following code :
Chart2.ChartAreas[0].AxisX.IsLabelAutoFit = false;
Chart2.ChartAreas[0].AxisX.LabelStyle.Enabled = true;
Chart2.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Comic Sans", 9F);
Chart2.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.WordWrap;
I also tried the following fix by searching the internet:
Chart2.ChartAreas[0].AxisX.LabelStyle.Angle = 90;
Chart2.ChartAreas[0].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.LabelsAngleStep90;
I tried changing various values for the angle but still it would align vertically.
Any links or suggestion on this would be much appreciated.
I am Ultra New, but this may help:" i have been searching for a solution for the last 4 hrs and someone suggested using annotations. Also this code is in VB, but it may help with another direction if you can't find an answer.
Dim MyTextAnnotation As TextAnnotation
MyTextAnnotation = New TextAnnotation
MyTextAnnotation.Text = "some notation"
MyTextAnnotation.X = 0
MyTextAnnotation.Y = 0
chart1.Annotations.Add(MyTextAnnotation)
I have a problem with regards on the page orientation of the paper size.
I have a pdf file which contains portrait and landscape page.
this code works perfectly.
string FileLocation = "c:\\Temp\\SomeFile.pdf";
string WatermarkLocation = "c:\\Temp\\watermark.gif";
Document document = new Document();
PdfReader pdfReader = new PdfReader(FileLocation);
PdfStamper stamp = new PdfStamper(pdfReader, new FileStream(FileLocation.Replace(".pdf","[temp][file].pdf"), FileMode.Create));
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(WatermarkLocation);
img.SetAbsolutePosition(250,300); // set the position in the document where you want the watermark to appear (0,0 = bottom left corner of the page)
PdfContentByte waterMark;
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
waterMark = stamp.GetUnderContent(page);
waterMark.AddImage(img);
}
stamp.FormFlattening = true;
stamp.Close();
// now delete the original file and rename the temp file to the original file
File.Delete(FileLocation);
File.Move(FileLocation.Replace(".pdf", "[temp][file].pdf"), FileLocation);
since I'm using absolute value to set the image position.
img.SetAbsolutePosition(250,300);
How can T set the image position if the page is landscape or portrait?
note: One pdf with landscape and portrait page orientation.
Is there by chance that I can use if statement?
if (//paper is landscape)
{
//code here
}
else
{
//code here
}
What do you want to achieve?
Normally, iText takes into account the value of the page rotation. This means that when a page is rotated, the coordinates will be rotated too.
If you want to overrule this, you can add this line:
stamper.RotateContents = false;
This is explained in Chapter 6 of my book. You can try this example to see the difference:
No rotation, text added normally: hello1.pdf
Rotation, text added normally ( = rotated): hello2.pdf
Rotation, text added with rotation ignored: hello3.pdf
Of course, this assumes that a rotation was defined for the pages. Sometimes, landscape is mimicked by defining a different page size instead of defining a rotation.
In that case, you should also read Chapter 6 because it explains how to get the MediaBox of a document. see the example PageInformation that introduces methods such as GetPageSize(), GetRotation() and GetPageSizeWithRotation().
This is all documented, but if it doesn't answer your question, please clarify. As demonstrated in the example, the rotation is taken into account by default when adding new content, so maybe I misunderstood the question.