Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Feb 05, 2024 5:11 am

How to use below commands to have Duplex setting & Three Punch Hole setting into the postscript file?

PdfDocument document = new PdfDocument();
document.LoadFromFile("c:\\TestFile.pdf");
document.SaveToFile("c:\\TestFile.ps",FileFormat.POSTSCRIPT);

NOC
 
Posts: 9
Joined: Thu Aug 03, 2017 9:04 pm

Mon Feb 05, 2024 6:09 am

Hello,

Thank you for your inquiry.
Regarding your question, I would like to inform you that our Spire.PDF library supports the conversion of PDF files to PS (PostScript) format, allowing you to customize the printing options, including duplex printing. However, I regret to inform you that we do not currently support the Three Punch Hole setting.
Please find below an example code snippet demonstrating how to print a PDF file as a PS file and enable duplex printing using Spire.PDF:
Code: Select all
PdfDocument pdfDocument = new PdfDocument();
pdfDocument.LoadFromFile("TestFile.pdf");
pdfDocument.PrintSettings.PrinterName = "Adobe PDF";
// Double-sided, horizontal printing.
pdfDocument.PrintSettings.Duplex = System.Drawing.Printing.Duplex.Horizontal;
pdfDocument.PrintSettings.PrintToFile("TestFile.ps");
pdfDocument.Print();

If you have any further questions or need assistance with any other features of Spire.PDF, please feel free to ask.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Thu Feb 08, 2024 7:43 pm

I am using the below code to generate pdf from html string and then using the pdf to generate postscript file.
Why is postscript files generated by using HTML Plugins so big in size.
Below i have generated a small file, but you may use any pdf file to convert to postscript printer.
Can you please check the below code to see if we are missing anything.

using (PdfDocument document = new PdfDocument())
{
string pdffilepath = "";
pdffilepath = "c:\\test\\testresult23.pdf";

HTMLToPDFWithPlugin hplug = new HTMLToPDFWithPlugin();
hplug.converthtmltoPDF();

// Method II --Print PDF in Duplex Mode
document.LoadFromFile(pdffilepath);
document.PrintSettings.PrinterName = "jduplex";//postscript printer
document.Print(); //This generates a postscript file. Output is going into a postscript file.
}


class HTMLToPDFWithPlugin
{

public void converthtmltoPDF()
{
StringBuilder htmlstringSb = new StringBuilder();

htmlstringSb.Append("<html><body><p>This is Test</p></body></html>");

// Create a MemoryStream to hold the generated PDF data
using (MemoryStream stream = new MemoryStream())
{
float top = 60;
float bottom = 50;
float left = 40;
float right = 20;

// Set the margins for the PDF document
PdfMargins margins = new PdfMargins(left, top, right, bottom);

// Set the path to the HTML converter plugins
Spire.Pdf.HtmlConverter.Qt.HtmlConverter.PluginPath = "C:\\SpireLibraries\\x86\\plugins";

// Convert the specified HTML content to PDF and write it to the MemoryStream
Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert(htmlstringSb.ToString(),
stream, false, 100 * 1000, new Size(1080, 1000),
margins, Spire.Pdf.HtmlConverter.LoadHtmlType.SourceCode);

// Create a new PdfDocument object and load the converted PDF from the MemoryStream
PdfDocument doc1 = new PdfDocument();

doc1.LoadFromStream(stream);

// Draw page numbers on each page of the PDF document
// DrawPageNumber(doc, margin, 1, doc.Pages.Count);

AddpaagenotoPDFFile(doc1);

// Save the PDF document to a file named "result.pdf"
doc1.FileInfo.IncrementalUpdate = false;

doc1.SaveToFile("c:\\test\\testresult23.pdf", FileFormat.PDF);


}
}

// Method for drawing page numbers on each page of the PDF document
private void AddpaagenotoPDFFile(PdfDocument pdf)
{

string pagefilename = "page" + DateTime.Now.ToString();
//Create a true type font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 10f, FontStyle.Bold), true);

//Create a PdfPageNumberField instance
PdfPageNumberField pageNumber = new PdfPageNumberField();

//Create a PdfPageCountField instance
PdfPageCountField pageCount = new PdfPageCountField();

//Create a PdfCompositeField instance
PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", pageNumber, pageCount);
//Set the text alignment for the composite field
compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top);

//Loop through the pages
for (int i = 0; i < pdf.Pages.Count; i++)
{
//Draw composite filed on each page
// compositeField.Draw(pdf.Pages[i].Canvas, pdf.Pages[i].Size.Width / 2 - 20, pdf.Pages[i].Size.Height - pdf.PageSettings.Margins.Top);
compositeField.Draw(pdf.Pages[i].Canvas, pdf.Pages[i].Size.Width / 2 - 35, 1);

}

}


}

NOC
 
Posts: 9
Joined: Thu Aug 03, 2017 9:04 pm

Fri Feb 09, 2024 2:25 am

Hello,

Thanks for your feedback.
I use the latest version of Spire.Pdf for Net (V 10.2.2)to test your scenario, I didn’t reproduce your issue, I attached my result file. According to your code, I find you doesn’t use the latest version, due to the html to pdf code has been adjusted in the latest version, please update to latest version to test, meanwhile, I put the adjusted code below for your reference. And I use the printer is “Adobe PDF” to convert pdf file to PS file. I think the printer also affects the size of the PS file, so you can try use “Adobe PDF” printer to test.
If you have any issue, just feel free to write back.

Code: Select all
using Spire.Additions.Qt;
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pdfSize
{
    internal class HtmlPdf
    {
        public void toHtml()
        {
            StringBuilder htmlstringSb = new StringBuilder();

            htmlstringSb.Append("<html><body><p>This is Test</p></body></html>");

            // Create a MemoryStream to hold the generated PDF data
            using (MemoryStream stream = new MemoryStream())
            {
                float top = 60;
                float bottom = 50;
                float left = 40;
                float right = 20;

                // Set the margins for the PDF document
                PdfMargins margins = new PdfMargins(left, top, right, bottom);

                // Set the path to the HTML converter plugins
                HtmlConverter.PluginPath = @"D:\DeskTop\plugins-windows-x86\plugins";

                // Convert the specified HTML content to PDF and write it to the MemoryStream
                HtmlConverter.Convert(htmlstringSb.ToString(),
                stream, false, 100 * 1000, new Size(1080, 1000),
                margins, LoadHtmlType.SourceCode);

                // Create a new PdfDocument object and load the converted PDF from the MemoryStream
                PdfDocument doc1 = new PdfDocument();

                doc1.LoadFromStream(stream);

                // Draw page numbers on each page of the PDF document
                // DrawPageNumber(doc, margin, 1, doc.Pages.Count);

                AddpaagenotoPDFFile(doc1);

                // Save the PDF document to a file named "result.pdf"
                doc1.FileInfo.IncrementalUpdate = false;

                doc1.SaveToFile(@"../../output/testresult23.pdf", FileFormat.PDF);
            }


        }
        private void AddpaagenotoPDFFile(PdfDocument pdf)
        {

            string pagefilename = "page" + DateTime.Now.ToString();
            //Create a true type font
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 10f, FontStyle.Bold), true);

            //Create a PdfPageNumberField instance
            PdfPageNumberField pageNumber = new PdfPageNumberField();

            //Create a PdfPageCountField instance
            PdfPageCountField pageCount = new PdfPageCountField();

            //Create a PdfCompositeField instance
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", pageNumber, pageCount);
            //Set the text alignment for the composite field
            compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Top);

            //Loop through the pages
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                //Draw composite filed on each page
                // compositeField.Draw(pdf.Pages[i].Canvas, pdf.Pages[i].Size.Width / 2 - 20, pdf.Pages[i].Size.Height - pdf.PageSettings.Margins.Top);
                compositeField.Draw(pdf.Pages[i].Canvas, pdf.Pages[i].Size.Width / 2 - 35, 1);

            }

        }

    }
}


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Thu Feb 15, 2024 7:14 pm

After generating PS files using below statement. Why is it holding the memory? I am trying to close & dispose and its still holding the memory.
Is there any way to force close the objects from the memory? Please advise.

document.Print()

NOC
 
Posts: 9
Joined: Thu Aug 03, 2017 9:04 pm

Fri Feb 16, 2024 1:18 am

Hello,

Thank you for your feedback.
Please use the following code to clear the object.
Code: Select all
...
document.Print();

document.Dispose();
document.Close();

If you have any further questions or need assistance with any other features of Spire.PDF, please feel free to ask.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF