2

I am using iTextSharp to join multiple pdfs together that are from microsofts report viewer. page.AddImage(image__1) in the code below is throwing an error Object reference not set to an instance of an object. I am not seeing what is set to nothing. Is there a different way that I can join reports from the report viewer and add a watermark image?

    mybytes = MSRptViewer1.LocalReport.Render("PDF", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
    reader = New iTextSharp.text.pdf.PdfReader(mybytes)
    numberOfPages = reader.NumberOfPages
    currentPageNumber = 0

    Dim imageFile As String = Server.MapPath("WaterMark.png")
    Dim buffer As Byte() = IO.File.ReadAllBytes(imageFile)
    Dim image__1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(buffer)
    image__1.SetAbsolutePosition(100, 100)

    Do While (currentPageNumber < numberOfPages)
        currentPageNumber += 1
        doc.SetPageSize(PageSize.LETTER)
        doc.NewPage()

        page = writer.GetImportedPage(reader, currentPageNumber)
        page.AddImage(image__1)

        rotation = reader.GetPageRotation(currentPageNumber)
        If (rotation = 90) Or (rotation = 270) Then
            cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(currentPageNumber).Height)
        Else
            cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, 0)
        End If
    Loop
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
user1167466
  • 333
  • 1
  • 4
  • 14
  • possible duplicate of [How can I insert an image with iTextSharp in an existing PDF?](http://stackoverflow.com/questions/583629/how-can-i-insert-an-image-with-itextsharp-in-an-existing-pdf) – Bruno Lowagie Apr 12 '13 at 11:07

2 Answers2

0
<?php $data = file_get_contents('https://static1.squarespace.com/static/56c775ad27d4bd3fdb24775d/t/5a8b201324a694d7071662ee/1519067160925/dummy+logo.jpg');
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
?>

this method for image convert to base64 format

<?php echo '<div class="company-logo"><img src="'.$base64.'"  alt="base" /></div>'; ?>

or

<img src="<?php echo $base64; ?>"  alt="base" />
Naved Khan
  • 1,699
  • 16
  • 13
-1

I have asked my Publisher, Manning Publications, to offer chapter 6 of my book for free. I know this chapter only shows you examples in Java, but all the examples were ported to C# for your convenience.

If you read this chapter, you'll find out that you're making the following mistakes:

  1. You are using PdfReader/PdfWriter to add content to an existing document instead of PdfReader/PdfStamper. If you want a nice watermark example, take a look at the ManipulatePdf method in the StampStationery.cs example. Replace background.addTemplate() with background.addImage() to add an image instead of a background page taken from another PDF. You'll also learn more about using an image as a watermark here: How to add a watermark to a PDF file?
  2. You are using PdfReader/PdfWriter to concatenate existing documents instead of PdfCopy. By doing so, you risk all kinds of problems: e.g. parts of pages being cut off, links being removed, annotations getting lost,... Please read my answer to this question: How to merge documents correctly?
  3. You are ignoring the fact that PdfImportedPage is read-only. You can only add content in the context of PdfCopy after creating a PageStamp object. Please take a look at the ConcatenateStamp.cs example.

Summarized: your question is more or less a duplicate of (a combination of) other questions answered before on StackOverflow. It seems that you copy/pasted some code from a source that isn't one of the official sources of documentation.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Strange. SO says it was asked yesterday. It's probably a fluke. Thanks for accepting the answer anyway. – Bruno Lowagie Apr 13 '13 at 07:44
  • It was asked then, but that code for concatenating documents has been used for over a year in the product I am working on – user1167466 Apr 16 '13 at 17:37
  • Aha, I misunderstood your remark. Anyway: if it's a commercial product you're working on, please check if you have a support contract with iText Software. You may want to post questions like that to our support team. – Bruno Lowagie Apr 17 '13 at 06:15