
¡Ayuda! Como unir dos pdf con la libreria itextsharp
Publicado por Carlos (19 intervenciones) el 08/05/2013 11:32:33
Hola buenas, soy yo otra vez, el cansino de antes.
Lamento el titulo tan poco explicativo de mi anterior tema: Como unir dos pdf usando
Meti la pata al olvidar escribir que era usando itextsharp.
Aqui les ofrezco posibles soluciones (necesito que me ayuden a solucionar errores del codigo vb.net). No se asusten por la longitud del mensaje, es que he añadido dos codigos enteros.
En fin, esto es lo que he encontrado:
Link en C# que realiza la union de pdf con itextsharp:
http://ant2e6.webs-interesantes.es/Articulos/Utilidades/Combinar-PDF.htm
Yo sinceramente no se usar C# (por lo que en realidad este codigo no me vale de gran cosa).
A todo esto, aqui les doy el codigo en C#:
Pero como ya habreis deducido, yo no utilizo C#, sino el maravilloso .net (vb.net).
Y realize una traduccion del codigo.
Lo malo es que al implementar el codigo me saltan errores, y agradeceria cualquier ayuda con esos errores.
Aqui les dejo el codigo en .net:
En fin, si alguien pudiese decirme como solucionar estos errores y hacer que el metodo funciones (vamos, que una unos pocos pdf en uno solo), ya sea modificando la sintaxis de las lineas que tienen error, proponiendo otros codigos...... como sea, cualquier ayuda sera bienvenida
Por cualquier duda sobre los codigos que he mostrado preguntad en el foro o en mi correo, solucionare cualquier duda (en la medida de lo posibl, ya que soy yo el primero que tiene dudas, jajajja).
Nos vemos en el foro.
Lamento el titulo tan poco explicativo de mi anterior tema: Como unir dos pdf usando
Meti la pata al olvidar escribir que era usando itextsharp.
Aqui les ofrezco posibles soluciones (necesito que me ayuden a solucionar errores del codigo vb.net). No se asusten por la longitud del mensaje, es que he añadido dos codigos enteros.
En fin, esto es lo que he encontrado:
Link en C# que realiza la union de pdf con itextsharp:
http://ant2e6.webs-interesantes.es/Articulos/Utilidades/Combinar-PDF.htm
Yo sinceramente no se usar C# (por lo que en realidad este codigo no me vale de gran cosa).
A todo esto, aqui les doy el codigo en C#:
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
internal static bool Merge(string strFileTarget, string [] arrStrFilesSource)
{ bool blnMerged = false;
// Crea el PDF de salida
try
{ using (System.IO.FileStream stmFile = new System.IO.FileStream(strFileTarget, System.IO.FileMode.Create))
{ Document objDocument = null;
PdfWriter objWriter = null;
// Recorre los archivos
for (int intIndexFile = 0; intIndexFile < arrStrFilesSource.Length; intIndexFile++)
{ PdfReader objReader = new PdfReader(arrStrFilesSource[intIndexFile]);
int intNumberOfPages = objReader.NumberOfPages;
// La primera vez, inicializa el documento y el escritor
if (intIndexFile == 0)
{ // Asigna el documento y el generador
objDocument = new Document(objReader.GetPageSizeWithRotation(1));
objWriter = PdfWriter.GetInstance(objDocument, stmFile);
// Abre el documento
objDocument.Open();
}
// Añade las páginas
for (int intPage = 0; intPage < intNumberOfPages; intPage++)
{ int intRotation = objReader.GetPageRotation(intPage + 1);
PdfImportedPage objPage = objWriter.GetImportedPage(objReader, intPage + 1);
// Asigna el tamaño de la página
objDocument.SetPageSize(objReader.GetPageSizeWithRotation(intPage + 1));
// Crea una nueva página
objDocument.NewPage();
// Añade la página leída
if (intRotation == 90 || intRotation == 270)
objWriter.DirectContent.AddTemplate(objPage, 0, -1f, 1f, 0, 0,
objReader.GetPageSizeWithRotation(intPage + 1).Height);
else
objWriter.DirectContent.AddTemplate(objPage, 1f, 0, 0, 1f, 0, 0);
}
}
// Cierra el documento
if (objDocument != null)
objDocument.Close();
// Cierra el stream del archivo
stmFile.Close();
}
// Indica que se ha creado el documento
blnMerged = true;
}
catch(Exception objException)
{ System.Diagnostics.Debug.WriteLine(objException.Message);
}
// Devuelve el valor que indica si se han mezclado los archivos
return blnMerged;
}
Pero como ya habreis deducido, yo no utilizo C#, sino el maravilloso .net (vb.net).
Y realize una traduccion del codigo.
Lo malo es que al implementar el codigo me saltan errores, y agradeceria cualquier ayuda con esos errores.
Aqui les dejo el codigo en .net:
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
Friend Shared Function Merge(ByVal strFileTarget As String, ByVal arrStrFilesSource() As String) As Boolean
Dim blnMerged As Boolean = False
' Crea el PDF de salida
Try
Using stmFile As System.IO.FileStream = New System.IO.FileStream(strFileTarget, System.IO.FileMode.Create)
Dim objDocument As Document = Nothing
Dim objWriter As PdfWriter = Nothing
' Recorre los archivos
For intIndexFile As Integer = 0 To arrStrFilesSource.Length - 1
Dim IntegerIndexFile As Integer
Dim objReader(0 To IntegerIndexFile - 1) As PdfReader
Dim intNumberOfPages As Integer = objReader(0 To IntegerIndexFile - 1).NumberOfPages
' La primera vez, inicializa el documento y el escritor
If IntegerIndexFile = 0 Then
' Asigna el documento y el generador
''LINEA ERROR: objDocument = New Document(objReader.GetPageSizeWithRotation(1))
''vale, el error que me sale es: GetPageSizeWithRotation(1) is not a member of System.Array
objDocument = New Document(objReader.GetPageSizeWithRotation(1))
objWriter = PdfWriter.GetInstance(objDocument, stmFile)
' Abre el documento
objDocument.Open()
End If
' Añade las páginas
For IntegerPage As Integer = 0 To intNumberOfPages - 1
''LINEA ERROR: Dim intRotation As Integer = objReader.GetPageRotation(IntegerPage + 1)
''Error: GetPageRotation is not a member of System.Array
Dim intRotation As Integer = objReader.GetPageRotation(IntegerPage + 1)
''LINEA ERROR: Dim objPage As PdfImportedPage = objWriter.GetImportedPage(objReader, IntegerPage + 1)
''Error: Value of type '1-dimensional array of iTextSharp.text.pdf.PdfReader' cannot be converted to 'iTextSharp.text.pdf.PdfReader'
Dim objPage As PdfImportedPage = objWriter.GetImportedPage(objReader, IntegerPage + 1)
' Asigna el tamaño de la página
''LINEA ERROR: objDocument.SetPageSize(objReader.GetPageSizeWithRotation(IntegerPage + 1))
''Error: GetPageSizeWithRotation(1) is not a member of System.Array
objDocument.SetPageSize(objReader.GetPageSizeWithRotation(IntegerPage + 1))
' Crea una nueva página
objDocument.NewPage()
' Añade la página leída
Dim IntegerRotation As Integer
If IntegerRotation = 90 OrElse IntegerRotation = 270 Then
''LINEA ERROR: esta de abajo tan sumuamente larga
''Error: GetPageSizeWithRotation(1) is not a member of System.Array
objWriter.DirectContent.AddTemplate(objPage, 0, -1.0F, 1.0F, 0, 0, objReader.GetPageSizeWithRotation(IntegerPage + 1).Height)
Else
objWriter.DirectContent.AddTemplate(objPage, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Next
' Cierra el documento
If objDocument IsNot Nothing Then
objDocument.Close()
End If
' Cierra el stream del archivo
stmFile.Close()
Next
' Indica que se ha creado el documento
blnMerged = True
End Using
Catch objException As Exception
System.Diagnostics.Debug.WriteLine(objException.Message)
End Try
' Devuelve el valor que indica si se han mezclado los archivos
Return blnMerged
End Function
En fin, si alguien pudiese decirme como solucionar estos errores y hacer que el metodo funciones (vamos, que una unos pocos pdf en uno solo), ya sea modificando la sintaxis de las lineas que tienen error, proponiendo otros codigos...... como sea, cualquier ayuda sera bienvenida
Por cualquier duda sobre los codigos que he mostrado preguntad en el foro o en mi correo, solucionare cualquier duda (en la medida de lo posibl, ya que soy yo el primero que tiene dudas, jajajja).
Nos vemos en el foro.
Valora esta pregunta


1