【发布时间】:2017-11-22 13:56:29
【问题描述】:
请尽快提供解决方案,为什么 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;
}
【问题讨论】:
-
任何图像源都会出现这种情况吗?还是只有某些?最后,您使用的是什么版本的 PDFNet?您可以在运行时调用 PDFNet.GetVersion
-
所有图像都会发生这种情况,我们以 byte[] 数组格式保存在 SQL 数据库中。图像没有损坏,通过不同的算法在其他地方工作正常。但我们只在这种特定情况下得到错误。如果出现问题,那么 PDFNET 的其他重载 Create() 函数也将无法工作。问题发生在我们在 Create() 函数中使用“BitMap”作为参数。由于我们对 BitMap 使用 SetResolution,因此我们只能使用这一行。哪一行代码工作正常,哪一行在 cmets 中没有解释。我们使用的是 PDFNet 6.01 版。
-
但早期相同版本的 PDFNET 库工作正常,但在过去的几个月里它不能正常工作。我想知道您是否确认,当在 Create() 函数中传递 BitMap 作为参数时,会在 6.01 版本的 PDFNet 库中引发错误。
标签: c# .net bitmap pdftron pdfnet