Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO
Imports System.Xml
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
' اسم الملف PDF الناتج
Dim outputFile As String = "output.pdf"
' إنشاء ملف PDF
Using doc As New Document()
' إضافة جميع البيانات الموجودة في ملف XML إلى الملف PDF
Using writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(outputFile, FileMode.Create))
doc.Open()
' قراءة ملف XML
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("data.xml")
' تعيين ترميز الخط
Dim baseFont As BaseFont = BaseFont.CreateFont("c:\windows\\fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
Dim font As New Font(baseFont, 12)
' استخراج البيانات من ملف XML
Dim invoiceDate As String = xmlDoc.SelectSingleNode("//cbc:IssueDate", GetNamespaceManager(xmlDoc))?.InnerText
Dim customerName As String = xmlDoc.SelectSingleNode("//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName", GetNamespaceManager(xmlDoc))?.InnerText
Dim totalAmount As String = xmlDoc.SelectSingleNode("//cbc:PayableAmount", GetNamespaceManager(xmlDoc))?.InnerText
Dim discount As String = xmlDoc.SelectSingleNode("//cbc:AllowanceTotalAmount", GetNamespaceManager(xmlDoc))?.InnerText
Dim tax As String = xmlDoc.SelectSingleNode("//cbc:TaxAmount", GetNamespaceManager(xmlDoc))?.InnerText
Dim taxNumber As String = xmlDoc.SelectSingleNode("//cac:PartyTaxScheme/cbc:CompanyID", GetNamespaceManager(xmlDoc))?.InnerText
Dim electronicSignature As String = xmlDoc.SelectSingleNode("//ds:SignatureValue", GetNamespaceManager(xmlDoc))?.InnerText
' إضافة البيانات إلى ملف PDF بشكل منسق
doc.Add(New Paragraph("Invoice Details", font))
doc.Add(New Paragraph($"Invoice Date: {invoiceDate}", font))
doc.Add(New Paragraph($"Customer Name: {customerName}", font))
doc.Add(New Paragraph($"Total Amount: {totalAmount}", font))
doc.Add(New Paragraph($"Discount: {discount}", font))
doc.Add(New Paragraph($"Tax: {tax}", font))
doc.Add(New Paragraph($"Tax Number: {taxNumber}", font))
doc.Add(New Paragraph($"Electronic Signature: {electronicSignature}", font))
' إضافة الأصناف إلى ملف PDF
doc.Add(New Paragraph("Items:", font))
Dim table As New PdfPTable(5)
table.WidthPercentage = 100
table.SetWidths(New Single() {10, 30, 10, 20, 20})
' إضافة العناوين للجدول
table.AddCell(New PdfPCell(New Phrase("ID", font)))
table.AddCell(New PdfPCell(New Phrase("Name", font)))
table.AddCell(New PdfPCell(New Phrase("Quantity", font)))
table.AddCell(New PdfPCell(New Phrase("Unit Price", font)))
table.AddCell(New PdfPCell(New Phrase("Line Extension Amount", font)))
' استخراج وإضافة الأصناف إلى الجدول
Dim nodes As XmlNodeList = xmlDoc.SelectNodes("//cac:InvoiceLine", GetNamespaceManager(xmlDoc))
For Each node As XmlNode In nodes
Dim id As String = node.SelectSingleNode("cbc:ID", GetNamespaceManager(xmlDoc))?.InnerText
Dim name As String = node.SelectSingleNode("cac:Item/cbc:Name", GetNamespaceManager(xmlDoc))?.InnerText
Dim quantity As String = node.SelectSingleNode("cbc:InvoicedQuantity", GetNamespaceManager(xmlDoc))?.InnerText
Dim unitPrice As String = node.SelectSingleNode("cac:Price/cbc:PriceAmount", GetNamespaceManager(xmlDoc))?.InnerText
Dim lineExtensionAmount As String = node.SelectSingleNode("cbc:LineExtensionAmount", GetNamespaceManager(xmlDoc))?.InnerText
table.AddCell(New PdfPCell(New Phrase(id, font)))
table.AddCell(New PdfPCell(New Phrase(name, font)))
table.AddCell(New PdfPCell(New Phrase(quantity, font)))
table.AddCell(New PdfPCell(New Phrase(unitPrice, font)))
table.AddCell(New PdfPCell(New Phrase(lineExtensionAmount, font)))
Next
doc.Add(table)
doc.Close()
MessageBox.Show("تم إنشاء ملف PDF بنجاح.")
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Function GetNamespaceManager(xmlDoc As XmlDocument) As XmlNamespaceManager
Dim nsmgr As New XmlNamespaceManager(xmlDoc.NameTable)
nsmgr.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
nsmgr.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
nsmgr.AddNamespace("ext", "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2")
nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#")
Return nsmgr
End Function
End Class