Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.
Tue Sep 28, 2021 4:11 am
hello
I want to know how DrawString and DrawImage on top margins set page
I set margins page
- Code: Select all
PdfDocument doc = new PdfDocument();
doc.PageSettings.Size = PdfPageSize.A4;
PdfMargins margins_specialDetail = new PdfMargins(30f, 165f, 30f, 198f);
doc.PageSettings.Margins = margins_specialDetail;
and set DrawString and DrawImage
- Code: Select all
PdfImage headerImage = PdfImage.FromFile("Picture/kurt.jpg");
PdfImage ImageLine = PdfImage.FromFile("Picture/line_img.png");
string headerText1 = "Form P.Q. 7-2\nแบบ พ.ก. ๗-๒";
doc.Pages[1].Canvas.DrawImage(ImageLine, 0f, -56f, 535f, 115f);
doc.Pages[1].Canvas.DrawImage(headerImage, 230, -46f, 65f, 66f);
doc.Pages[1].Canvas.DrawString(headerText1, font1, brush, doc.Pages[1].Size.Width - 55 - size.Width, -5);
result

-

jame_piyawat2
-
- Posts: 10
- Joined: Fri May 07, 2021 4:36 am
Tue Sep 28, 2021 7:57 am
Hello Piyawat,
Thanks for your inquiry.
If you want to add header image/text for the exsiting pdf file. Please refer to the following code.
- Code: Select all
static void Main(string[] args)
{
PdfDocument sourcePDF = new PdfDocument();
sourcePDF.LoadFromFile("Sample.pdf");
PdfDocument newPdf = new PdfDocument();
PdfMargins margins = new PdfMargins(30f, 165f, 30f, 198f);
PdfPageBase newPage;
string headerImageFile = "1.png";
string headerText = "Form P.Q. 7-2\nแบบ พ.ก. ๗-๒";
foreach (PdfPageBase sourcePage in sourcePDF.Pages)
{
SizeF newPageSize =new SizeF(sourcePage.Size.Width+margins.Left+margins.Right,sourcePage.Size.Height+margins.Top+margins.Bottom);
newPage = newPdf.Pages.Add(newPageSize, new PdfMargins(0));
sourcePage.CreateTemplate().Draw(newPage.Canvas, margins.Left, margins.Top);
SetDocumentHeaderTemplate(newPage,margins,headerImageFile,headerText);
}
string outputfilepath = "output.pdf";
newPdf.SaveToFile(outputfilepath);
System.Diagnostics.Process.Start(outputfilepath);
}
static void SetDocumentHeaderTemplate(PdfPageBase page,PdfMargins margins,string headerImageFile,string headerText)
{
PdfImage headerImage = PdfImage.FromFile(headerImageFile);
page.Canvas.DrawImage(headerImage, new PointF(230, 46), new SizeF(65, 66));
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12, FontStyle.Regular), true);
SizeF textSize = font1.MeasureString(headerText);
page.Canvas.DrawString(headerText, font1, PdfBrushes.Black, page.Size.Width - margins.Right-textSize.Width, 5);
}
If you want to create a brand new pdf file including header and body content with our Spire.PDF API, please read this tutorial,
https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Add-header-in-PDF-with-C-VB.NET.htmlFeel free to contact us if any questions.
Sincerely,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3043
- Joined: Wed Jun 27, 2012 8:50 am
Thu Sep 30, 2021 9:37 am
Hello Piyawat,
Has your issue been solved? Any feedback will be appreciated.
Sincerely,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3043
- Joined: Wed Jun 27, 2012 8:50 am
Fri Oct 01, 2021 9:59 am
amy.zhao wrote:Hello Piyawat,
Has your issue been solved? Any feedback will be appreciated.
Sincerely,
Amy
E-iceblue support team
i still have issue because I want the Header to have only the second page.
Now issue Header is now on every page.
-

jame_piyawat2
-
- Posts: 10
- Joined: Fri May 07, 2021 4:36 am
Fri Oct 01, 2021 10:13 am
Hello,
Thanks for your feedback!
If you want to add header image/text only the second page of the existing pdf file, just change this part of the code my colleague provided.
- Code: Select all
.......
foreach (PdfPageBase sourcePage in sourcePDF.Pages)
{
SizeF newPageSize = new SizeF(sourcePage.Size.Width + margins.Left + margins.Right, sourcePage.Size.Height + margins.Top + margins.Bottom);
newPage = newPdf.Pages.Add(newPageSize, new PdfMargins(0));
if(newPdf.Pages.IndexOf(sourcePage) == 1) //just set header of the second page
{
SetDocumentHeaderTemplate(newPage, margins, headerImageFile, headerText);
}
sourcePage.CreateTemplate().Draw(newPage.Canvas, margins.Left, margins.Top);
}
.......
Feel free to contact us if any questions.
Sincerely,
Marcia
E-iceblue support team
-


Marcia.Zhou
-
- Posts: 858
- Joined: Wed Nov 04, 2020 2:29 am
Fri Oct 08, 2021 7:39 am
Hi,
Have you tried adjusted code? Does it meet your need? Any feedback will be appreciated.
Sincerely,
Amy
E-iceblue support team
-


amy.zhao
-
- Posts: 3043
- Joined: Wed Jun 27, 2012 8:50 am