请尽快提供解决方案,为什么 pdftron.PDF.Image.Create(doc,bitmap,JBIG2_hint) 给出异常“算术运算导致溢出”,但另一方面,如果我尝试 Create() 的另一个重载函数,然后它工作正常。
我正在使用它来将图像转换为 PDF。
private PDFDoc ConvertToPDF(System.Drawing.Image SourceImage)
{
string currentCulture = GetApplicationParameter("DataCenterCulture");
if (currentCulture == "en-US")
{
PDFNet.Initialize("some value already passed correctly");
}
else
{
PDFNet.Initialize("some value already passed correctly");
}
string path = GetApplicationParameter("some_PDF_Path");
bool found = PDFNet.SetResourcesPath(path);
PDFDoc doc = new PDFDoc();
ElementBuilder f = new ElementBuilder(); // Used to build new Element objects
ElementWriter writer = new ElementWriter(); // Used to write Elements to the page
Element element = null;
// Use JBIG2 Encoding
ObjSet hint_set = new ObjSet();
pdftron.SDF.Obj JBIG2_hint = hint_set.CreateArray();
JBIG2_hint.PushBack(hint_set.CreateName("g4"));
try
{
for (int i = 0; i < GetPageCount(SourceImage); i++)
{
SourceImage.SelectActiveFrame(FrameDimension.Page, i);
MemoryStream ms = new MemoryStream();
SourceImage.Save(ms, ImageFormat.Tiff);
ms.Seek(0, 0);
System.Drawing.Bitmap bitmap = new Bitmap(ms);
bitmap.SetResolution(200f, 200f);
Page page = doc.PageCreate(); // Start a new page
writer.Begin(page); // Begin writing to this page
// ColorSpace cs = ColorSpace.CreateDeviceRGB(); //Alternatively using this line, but not required.
//System.Drawing.Image img = (System.Drawing.Image)bitmap; //Working Fine, but No need of this line just verifying whether it gives error or not.
//byte[] bt = ms.ToArray();// Working Fine, but No need of //this line just verifying whether it gives error or not.
//pdftron.PDF.Image image = pdftron.PDF.Image.Create(doc, ms.ToArray(), 200, 200, 100, cs); // This line works fine, but we don’t need to use this.
// pdftron.PDF.Image image = pdftron.PDF.Image.Create(doc, "E:\\SomeTestImage.jpg", JBIG2_hint); // This line also works fine, but we don’t need to use this.
pdftron.PDF.Image image = pdftron.PDF.Image.Create(doc, bitmap, JBIG2_hint); // I need this line as its our requirement, but this line gives error “Arithmetic operation resulted in an overflow” and need solution for this why it gives error.
element = f.CreateImage(image, new Matrix2D(page.GetPageWidth(), 0, 0, page.GetPageHeight(), 0, 0));
writer.WriteElement(element);
writer.End();
doc.PagePushBack(page);
}
}
catch (ArithmeticException aex)
{
}
catch (PDFNetException pex)
{
//Why Arithmetic operation resulted in an overflow occurs and catch in this PDFNetException , and not in Arithmetic Exception, this is also my query.
}
catch (Exception ex)
{
}
return doc;
}