Acumatica:文件类型转换为pdf



如何在Acumatica中将不同的文件类型转换为pdf(Excel,Word,Png,Jpg,Bmp(?Acumatica是否提供了任何API来执行此操作,或者也许有人知道任何可以帮助我解决此问题的免费库?

您可以像在任何其他 C# 程序中一样执行此操作。

一些 API 建议:

  • 对于 Excel/Word,请使用 Microsoft Office Interop API
  • 对于 PNG、JPG、BMP Microsoft .Net Framework

不过,Acumatica 中的文件管理有所不同,因此您可以使用以下示例作为起点,使用 .Net Framework 使用 Acumatica 转换图像:

using PX.SM;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Text;
// Create file maintenance graph to load and save file
UploadFileMaintenance fileGraph = PXGraph.CreateInstance<UploadFileMaintenance>();
// Files are attached to DAC records NoteID fields, get UID (unique identifier) of files
Guid[] uids = PXNoteAttribute.GetFileNotes(Base.Caches[typeof(DAC)], dacRecord);
if (uids.Length > 0)
{
// Get first file attached to DAC record using UID
// Note that FileInfo is part of Acumatica PX.SM namespace and is not .Net FileInfo class
FileInfo fileInfo = fileGraph.GetFile(uids[0]);
if (fileInfo != null)
{  
// Converting file raw binary data to a .Net Image object
Image image = ProcessImage(fileInfo.BinData);
// Write converted image to new file.
// File extension is changed but actual image conversion 
// happens in ConvertImageToByteArray function
const string numberGuidFormat = "N";
const string imageExtension = ".png";
FileInfo newImage = new FileInfo(System.IO.Path.ChangeExtension(Guid.NewGuid().ToString(numberGuidFormat, CultureInfo.InvariantCulture), imageExtension),
null,
ConvertImageToByteArray(image));
// Save image file in Acumatica                                      
fileGraph.SaveFile(newImage, FileExistsAction.ThrowException);
// Attach image file UID to a DAC record note Eield
PXNoteAttribute.SetFileNotes(Base.Caches[typeof(DAC)], dacRecord, newImage.UID.Value);
}    
}
public static System.Drawing.Image ProcessImage(byte[] imageData)
{
Bitmap convertedImage = null;
try
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream(imageData))
{
Image image = Image.FromStream(stream);
convertedImage = new Bitmap(image.Width, image.Height);
convertedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// Stripping out transparency                               
Graphics graphics = Graphics.FromImage(convertedImage);
graphics.Clear(Color.White);
graphics.DrawImageUnscaled(image, 0, 0);
}
}
catch
{
// Not a valid image
}
return convertedImage;
}

public static byte[] ConvertImageToByteArray(Image image)
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
// Setting the target format here, I used PNG but you can change it to BMP or JPG
image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}

最新更新