Agregar marca de agua a un pdf existente en vb.net
Publicado por Lupita (1 intervención) el 27/05/2020 00:32:36
Buenas tardes!
Me gustaría por favor su ayuda.
Necesito colocar una marca de agua (texto) en un documento de PDF ya existente, estuve leyendo y encontré éste codigo, pero tenía muchos errores y lo corregí (eso creo) si embargo algo anda mal y no funciona bien. (crea un pdf dañado) Soy nueva en esto y no encuentro nada que me ayude. por favor, agradeceré sus aportes, En especifico trabajo con vb.net pero también puedo usar C#, cualquiera que realmente funcione, lo agradeceré
Me gustaría por favor su ayuda.
Necesito colocar una marca de agua (texto) en un documento de PDF ya existente, estuve leyendo y encontré éste codigo, pero tenía muchos errores y lo corregí (eso creo) si embargo algo anda mal y no funciona bien. (crea un pdf dañado) Soy nueva en esto y no encuentro nada que me ayude. por favor, agradeceré sus aportes, En especifico trabajo con vb.net pero también puedo usar C#, cualquiera que realmente funcione, lo agradeceré
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
protected void Button1_Click(object sender, EventArgs e)
{
string path = @"ruta de lectura";
string uriPath = @"ruta de escritura";
string localPath = new Uri(uriPath).LocalPath;
PdfReader reader = new PdfReader(path);
FileStream fs = null;
Document document = null;
document = new Document();
string outputPdf = string.Format(localPath,
Guid.NewGuid().ToString());
fs = new FileStream(outputPdf,
FileMode.CreateNew,
FileAccess.Write);
PdfStamper stamp = null;
stamp = new PdfStamper(reader, fs);
BaseFont bf =
BaseFont.CreateFont(@"c:\windows\fonts\arial.ttf",
BaseFont.CP1252, true);
PdfGState gs = new PdfGState();
gs.FillOpacity = 0.35F;
gs.StrokeOpacity = 0.35F;
for (int nPag = 1; nPag <= reader.NumberOfPages; nPag++)
{
Rectangle tamPagina = reader.GetPageSizeWithRotation(nPag);
PdfContentByte over = stamp.GetOverContent(nPag);
over.BeginText();
WriteTextToDocument(bf, tamPagina, over, gs, "probando");
over.EndText();
}
}
private static void
WriteTextToDocument(BaseFont bf,
Rectangle tamPagina,
PdfContentByte over,
PdfGState gs,
string texto)
{
over.SetGState(gs);
over.SetRGBColorFill(220, 220, 220);
over.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
over.SetFontAndSize(bf, 46);
Single anchoDiag =
(Single)Math.Sqrt(Math.Pow((tamPagina.Height - 120), 2)
+ Math.Pow((tamPagina.Width - 60), 2));
Single porc =
(Single)100 * (anchoDiag / bf.GetWidthPoint(texto, 46));
over.SetHorizontalScaling(porc);
double angPage = (-1)
* Math.Atan((tamPagina.Height - 60) / (tamPagina.Width - 60));
over.SetTextMatrix((float)Math.Cos(angPage),
(float)Math.Sin(angPage),
(float)((-1F) * Math.Sin(angPage)),
(float)Math.Cos(angPage),
30F,
(float)tamPagina.Height - 60);
over.ShowText(texto);
}
}
Valora esta pregunta


0