Acumatica:文件类型转换为pdf

时间:2018-06-06 14:22:49

标签: c# acumatica

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

1 个答案:

答案 0 :(得分:0)

你可以像在任何其他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();
    }
}