मुझे एक एसक्यूएल डेटाबेस में एक मान के आधार पर प्रत्येक पीडीएफ पेज पर रंगीन बार रखने के लिए पीडीएफ विलय को संपादित करने की आवश्यकता है जहां पीडीएफ फ़ाइल नाम भी संग्रहीत किया जाता है। मैं सी # सूचियों के साथ बहुत अच्छा नहीं हूं, लेकिन सोचा कि शायद मैं iTextSharp "PDFReader" सूची में एक पूरक सूची बना सकता हूं, फिर जब PDFReader सूची के माध्यम से पुनरावृत्ति करते हुए एक सशर्त कथन लिखें जो कहता है "यदि सूची 2 मान = "उपयोगिताएँ" तो हरा वर्ग बनाएं " पीडीएफ मर्ज प्रक्रिया के दौरान।

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if ((Session["AccessLevel"].ToString() == "admin") || (Session["AccessLevel"].ToString() == "worker") || (Session["AccessLevel"].ToString() == "client"))
        {
            if (Request.QueryString["type"] == "QC")
            {
                //SqlDataSource2.Update();
            }
            else
            {
            }

            string checkID = Request.QueryString["id"];

            SqlDataReader rdr = null;
            SqlConnection con2 = new SqlConnection(sqlConnection);
            con2.Open();
            //string sqlRowCount = "SELECT COUNT(*) FROM [Attachment] WHERE RequestId = '" + checkID + "' AND AttachType != 'Invoice' AND AttachType != 'Cover Sheet' ORDER BY AttachOrder ASC";
            sqlUserName2 = "SELECT  AttachmentName,AttachType FROM [Attachment] WHERE RequestId = '" + checkID + "' AND AttachType != 'Invoice' AND AttachType != 'Cover Sheet' ORDER BY AttachOrder ASC";
            //SqlCommand cmd = new SqlCommand(sqlRowCount, con2);
            //string count = cmd.ExecuteScalar().ToString();
            SqlCommand cmd2 = new SqlCommand(sqlUserName2, con2);
            rdr = cmd2.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(rdr);
            List<PdfReader> readerList = new List<PdfReader>();
            List<string> pdfName = new List<string>();
            foreach (DataRow row in dt.Rows)
            {
                PdfReader pdfReader = new PdfReader(Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/uploads/reports/" +
                  Convert.ToString(row[0])));

                readerList.Add(pdfReader);
                pdfName.Add(Convert.ToString(row[1]));
                //pdfName.Add(rdr["AttachType"].ToString());
            }

            System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
            contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Pdf;
            Document document = new Document(PageSize.A4, 0, 0, 40, 0);
            //Get instance response output stream to write output file.
            PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);

            // document.Header = new HeaderFooter(new Phrase("Header Text"), false);
            // Parameters passed on to the function that creates the PDF 
            String headerText = "";
            String footerText = "PAGE";

            // Define a font and font-size in points (plus f for float) and pick a color
            // This one is for both header and footer but you can also create seperate ones
            Font fontHeaderFooter = FontFactory.GetFont("arial", 12f);
            fontHeaderFooter.Color = Color.BLACK;

            // Apply the font to the headerText and create a Phrase with the result
            Chunk chkHeader = new Chunk(headerText, fontHeaderFooter);
            Phrase p1 = new Phrase(chkHeader);

            // create a HeaderFooter element for the header using the Phrase
            // The boolean turns numbering on or off
            HeaderFooter header = new HeaderFooter(p1, false);

            // Remove the border that is set by default
            header.Border = Rectangle.NO_BORDER;
            // Align the text: 0 is left, 1 center and 2 right.
            header.Alignment = 1;

            // add the header to the document
            document.Header = header;

            // The footer is created in an similar way

            // If you want to use numbering like in this example, add a whitespace to the
            // text because by default there's no space in between them
            if (footerText.Substring(footerText.Length - 1) != " ") footerText += " ";
            //string newFooter = footerText + pageCount;

            Chunk chkFooter = new Chunk(footerText, fontHeaderFooter);
            Phrase p2 = new Phrase(chkFooter);

            // Turn on numbering by setting the boolean to true
            HeaderFooter footer = new HeaderFooter(p2, true);
            footer.Border = Rectangle.NO_BORDER;
            footer.Alignment = 1;

            document.Footer = footer;
            Response.Write(pdfName);
            Response.Write("test");
            // Open the Document for writing and continue creating its content
            document.Open();
            foreach (PdfReader reader in readerList)
            {                   
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {

                    PdfImportedPage page = writer.GetImportedPage(reader, i);

                    if ("if list 2 value = "Utilities" then create green square")
                    {
                        PdfContentByte cb = writer.DirectContent;
                        var rect = new iTextSharp.text.Rectangle(200, 200, 100, 100);
                        rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER;
                        rect.BorderWidth = 5; rect.BorderColor = new BaseColor(2, 3, 0);
                        cb.Rectangle(rect);
                    }
                    document.Add(iTextSharp.text.Image.GetInstance(page));
                }
            }
            document.Close();
            Response.AppendHeader("content-disposition", "inline; filename=" + Request.QueryString["id"] + "-Final");
            Response.ContentType = "application/pdf";
            con2.Close();
            Response.Write(pdfName);
        }
    }
    catch
    {
        // Response.Redirect("~/PDFProblem.aspx", false);
    }
}
0
Kyle G 1 मार्च 2019, 17:46

1 उत्तर

सबसे बढ़िया उत्तर

यह थोड़ा क्लंकी है लेकिन यह तब तक काम करता है जब तक मैं इसे दोबारा नहीं करता। मैंने पहली सूची में दूसरी सूची (pdfName) को पहले iTextSharp एक (pdfReader) के भीतर दूसरी सूची को आगे बढ़ाने के लिए एक वृद्धिशील पूर्णांक का उपयोग करके पुनरावृत्त किया:

 foreach (PdfReader reader in readerList)
            {                   
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    string totalValue = pdfName[nextOne].ToString();
                    PdfImportedPage page = writer.GetImportedPage(reader, i);

                    if (totalValue == "Permit")
                    {
                        PdfContentByte cb = writer.DirectContent;
                        var rect = new iTextSharp.text.Rectangle(200, 200, 100, 100);
                        rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER;
                        rect.BorderWidth = 5; rect.BorderColor = new BaseColor(2, 3, 0);
                        cb.Rectangle(rect);
                    }
                    if (totalValue == "TaxBill")
                    {
                        PdfContentByte cb = writer.DirectContent;
                        var rect = new iTextSharp.text.Rectangle(200, 200, 100, 100);
                        rect.Border = iTextSharp.text.Rectangle.LEFT_BORDER | iTextSharp.text.Rectangle.RIGHT_BORDER;
                        rect.BorderWidth = 15; rect.BorderColor = new BaseColor(3, 2, 0);
                        cb.Rectangle(rect);
                    }
                    nextOne = nextOne + 1;
                    document.Add(iTextSharp.text.Image.GetInstance(page));
                }
            }
0
Kyle G 1 मार्च 2019, 18:16