تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
صناعة ملف PDF مع ملف XML
#6
(11-07-24, 10:42 AM)تركي الحلواني كتب :
(05-07-24, 12:33 PM)asd103 كتب : هل من حل ولكم جزيل الشكر
جرب هذا التعديل
PHP كود :
Imports iTextSharp.text
Imports iTextSharp
.text.pdf
Imports System
.IO
Imports System
.Xml

Public Class Form1
    Private Sub Button1_Click
(sender As ObjectAs EventArgsHandles 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() {1030102020})

 
                   ' إضافة العناوين للجدول
                    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(idfont)))
 
                       table.AddCell(New PdfPCell(New Phrase(namefont)))
 
                       table.AddCell(New PdfPCell(New Phrase(quantityfont)))
 
                       table.AddCell(New PdfPCell(New Phrase(unitPricefont)))
 
                       table.AddCell(New PdfPCell(New Phrase(lineExtensionAmountfont)))
 
                   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 

جبتها من الاخر مع الجذر ياتركي 
مشكور الله يزيدك من علمه وينفع بيك ويبارك ليك الف الف الف شكر  Heart
الرد }}}
تم الشكر بواسطة:


الردود في هذا الموضوع
صناعة ملف PDF مع ملف XML - بواسطة asd103 - 12-06-24, 04:56 PM
RE: صناعة ملف PDF مع ملف XML - بواسطة asd103 - 15-06-24, 02:03 AM
RE: صناعة ملف PDF مع ملف XML - بواسطة asd103 - 05-07-24, 12:33 PM
RE: صناعة ملف PDF مع ملف XML - بواسطة asd103 - 16-07-24, 01:11 PM


التنقل السريع :


يقوم بقرائة الموضوع: