Generate more than one boleto with the DLL "Boleto.Net"

Asked

Viewed 1,191 times

6

I noticed that the DLL Boleto.Net has a class BoletoBancario with the property FormatoCarne. However, I can not generate three tickets in the same page.

I would like to view a code that makes the printing of 3 billets on a page, only.

  • Good morning, did you manage to generate the card? It’s with webforms or MVC?

  • Make a mistake, what happens?

  • Dudu Moreira, managed to print more than one boleto on the same page?

1 answer

1

Well, unfortunately I didn’t find a solution in the library itself Boleto.Net, but I had to implement exactly the same requirement of three billets on a single sheet, and because I still had to register them, I decided to generate them asynchronously. So I managed the billets separately, and then gathered them through the library Pdfsharp with the code below my authorship.

public static byte[] SinglePageCombine(List<byte[]> boletoList)
{
    var outputDocument = new PdfDocument();

    foreach (var boletoArray in boletoList)
    {
        var outputDocumentPageCount = outputDocument.PageCount;

        try
        {
            PdfDocument inputDocument = GetPdfDocument(boletoArray);

            var inputDocumentPageCount = inputDocument.PageCount;
            if (inputDocumentPageCount == 1)
            {
                int outputDocumentPageIndex;
                if (elementsInPage >= 1 && elementsInPage < 3)
                    outputDocumentPageIndex = outputDocumentPageCount - 1;
                else
                    outputDocumentPageIndex = outputDocumentPageCount;

                PdfPage outputDocumentPage = GetPdfPage(ref outputDocument, outputDocumentPageIndex);
                PdfPage inputDocumentPage = GetPdfPage(ref inputDocument, 0);

                double width = inputDocumentPage.Width,
                        height = inputDocumentPage.Height;

                SetPageDimensions(ref outputDocumentPage, width, height);

                XPdfForm xPdfForm = GetXPdfForm(boletoArray);
                var xRect = new XRect(0, elementsInPage * 280, width, height);

                using (var pdfXGraphics = XGraphics.FromPdfPage(outputDocumentPage, XGraphicsPdfPageOptions.Append))
                {
                    pdfXGraphics.DrawImage(xPdfForm, xRect);
                }

                elementsInPage++;
                if (elementsInPage == 3)
                    elementsInPage = 0;
            }
            else
            {
                throw new Exception("Deve conter apenas uma página em cada boleto.");
            }
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }

    byte[] outputDocumentBytes;
    using (var documentStream = new MemoryStream())
    {
        outputDocument.Save(documentStream);
        outputDocumentBytes = documentStream.ToArray();
    }

    return outputDocumentBytes;
}

private static PdfDocument GetPdfDocument(byte[] fileContent)
{
    PdfDocument inputDocument;
    using (var documentStream = new MemoryStream(fileContent))
    {
        inputDocument = PdfReader.Open(documentStream, PdfDocumentOpenMode.Import);
    }

    return inputDocument;
}

private static PdfPage GetPdfPage(ref PdfDocument pdfDocument, int pageIndex)
{
    var pdfDocumentPageCount = pdfDocument.PageCount;

    if (pageIndex < 0)
        pageIndex = 0;

    if (pdfDocumentPageCount == 0)
    {
        pdfDocument.AddPage();

    }
    else if (pageIndex > (pdfDocumentPageCount - 1))
    {
        var nextPage = pdfDocumentPageCount;
        if (nextPage == pageIndex)
            pdfDocument.AddPage();

    }

    return pdfDocument.Pages[pageIndex];
}

private static XPdfForm GetXPdfForm(byte[] fileContent)
{
    XPdfForm xPdfForm;
    using (var documentStream = new MemoryStream(fileContent))
    {
        xPdfForm = XPdfForm.FromStream(documentStream);
    }

    return xPdfForm;
}

private static void SetPageDimensions(ref PdfPage pdfPageToChange, double width, double height)
{
    pdfPageToChange.Width = width;
    pdfPageToChange.Height = height;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.