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.

Sat Aug 10, 2019 10:07 am

I used spire.pdf add text watermark to pdf header,but I find I can search my text watermark in result pdf,but I can't see it.I use the code normally in general.but this file is not normal.I wrote a function
public void AddTextToHeader(HeaderFooterTextParams dp, PdfDocument doc)
{
System.Drawing.SizeF pageSize = doc.Pages[0].Size;
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(dp.TextFont, true);
System.Drawing.SizeF sizeF = trueTypeFont.MeasureString(dp.text);
PdfBrush brush = new PdfSolidBrush(dp.BrushColor);
PdfPen pen = new PdfPen(brush, dp.PenWidth);
float x = 0;
float y = 0;
switch (dp.wmp)
{
case WaterMarkPosition.Left:
x = 0 + dp.nDis[0];
y = dp.Distance;
break;
case WaterMarkPosition.Center:
x = (pageSize.Width - sizeF.Width) / 2f + dp.nDis[1];
y = dp.Distance;
break;
case WaterMarkPosition.Right:
x = pageSize.Width - sizeF.Width - dp.nDis[2];
y = dp.Distance;
break;
}

for (int i = 0; i < doc.Pages.Count; i++)
{
doc.Pages[i].Canvas.DrawString(dp.text, trueTypeFont, pen, x, y);

}
}


struct params

public struct HeaderFooterTextParams
{
public string text;
public Font TextFont;
public Color BrushColor;
public WaterMarkPosition wmp;
public float PenWidth;
public float Distance;
public string PicFile;
public bool IsStretch;
public int[] nDis;
public HeaderFooterTextParams(object obj)
{
text = "this is a test";
TextFont = new Font("黑体", 12f);
BrushColor = Color.Red;
wmp = WaterMarkPosition.Left;
PenWidth = 0.5f;
Distance = 0;
PicFile = "";
IsStretch = false;
nDis = new int[3];
}
}

public enum WaterMarkPosition
{
Left,
Center,
Right
}
I use it
public HeaderFooterTextParams headerParams = new HeaderFooterTextParams(null);




headerParams.PenWidth =0.5;
headerParams.Distance =0;
headerParams.text = " I am a watermark";
headerParams.nDis[0] = 0;
headerParams.nDis[1] = 0;
headerParams.nDis[2] = 0;


PdfDocument doc = new PdfDocument("test.pdf");
AddTextToHeader(headerParams ,doc);
doc.SaveToFile("result.pdf");

futureflsl
 
Posts: 1
Joined: Sat Aug 10, 2019 9:49 am

Mon Aug 12, 2019 2:50 am

Hi,

Thanks for your inquiry. This is Amber from E-iceblue support team.
I have reproduced your issue and logged it into our bug tracking system. Once there is any progress, we will inform you.
Sorry for the inconvenience caused.

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Aug 19, 2019 8:20 am

Hi,

Hope you are doing well.
After further investigation, we found the starting point of the source document(test.pdf) is in the lower left corner. Please correct part of the code in the method “AddTextToHeader” as below.

Code: Select all
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                PdfGraphicsState state = doc.Pages[i].Canvas.Save();
                doc.Pages[i].Canvas.TranslateTransform(0, -doc.Pages[i].Size.Height);
                doc.Pages[i].Canvas.DrawString(dp.text, trueTypeFont, pen, x, y);
                doc.Pages[i].Canvas.Restore(state);
            }


Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Mon Sep 02, 2019 10:32 am

Hi,

Greetings from E-iceblue.
Have you tried the code I offered you last time? Does it solve your issue?
Could you please give us some feedback at your convenience?

Best wishes,
Amber
E-iceblue support team
User avatar

Amber.Gu
 
Posts: 525
Joined: Tue Jun 04, 2019 3:16 am

Return to Spire.PDF

cron