【问题标题】:Acumatica: file types conversion to pdfAcumatica:文件类型转换为 pdf
【发布时间】:2018-06-06 14:22:48
【问题描述】:

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

【问题讨论】:

标签: c# acumatica


【解决方案1】:

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

一些 API 建议:

  • 对于 Excel/Word,请使用 Microsoft Office 互操作 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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2022-07-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2020-01-08
    相关资源
    最近更新 更多