How To Convert iTextSharp.text.Image ObjectToByteArray in C#

  • Thread starter Thread starter Md. Altaf hossain
  • Start date Start date
M

Md. Altaf hossain

Guest
I want to convert iTextSharp.text.Image.GetInstance (a) to ObjectToByteArray. But the error has come.
The following code has been provided.


PdfReader pdfReader = new PdfReader(sourceFilePath);
float width = (float)GetPDFwidth(sourceFilePath);
float height = (float)GetPDFHeight(sourceFilePath);
float widthTo_Trim = iTextSharp.text.Utilities.MillimetersToPoints(5);


// Create a new document
Document document = new Document(PageSize.A4.Rotate(), 50f, -80f, 63f, 40f);

// Make a copy of the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFilePath, FileMode.Create));

// Open the newly created document
document.Open();

// Loop through all pages of the source document
for (int i = 1; i <= pdfReader.NumberOfPages; i++)
{
// Get a page
var page = pdfReader.GetPageN(i);

// Apply the rectangle filter we created


// Copy the content and insert into the new document
if (i == 3)
{
PdfRectangle rectLeftsidea = new PdfRectangle(60, 20, width - widthTo_Trim - 15, 480);
page.Put(PdfName.CROPBOX, rectLeftsidea);
page.Put(PdfName.MEDIABOX, rectLeftsidea);


for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++)
{

int widthThird = 540;
int heightThird = 480;
PdfRectangle rectLeftsidead = new PdfRectangle(302, 397, widthThird, heightThird);
page.Put(PdfName.CROPBOX, rectLeftsidead);
page.Put(PdfName.MEDIABOX, rectLeftsidead);
var a = writer.GetImportedPage(pdfReader, i);



ObjectToByteArray(a );

}
}
}

//Close the output document
document.Close();

public static byte[] ObjectToByteArray(Object obj)
{
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}

Continue reading...
 
Back
Top