Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there,

I have one pdf. i need to change background color. if you want to see the pdf, i will send. Please help me.




Regards,
Narasimha
Posted
Updated 16-Apr-23 23:47pm

1 solution

Hi,

There is an option to set Size and color ofr the PDF, if you use the
"ITextSharp"


var pageSize = new Rectangle(216, 720);
pageSize.BackgroundColor(new Color(0, 0, 0));
Document document = new Document(pageSize);


Thanks!
 
Share this answer
 
v4
Comments
K N R 5-Nov-13 6:08am    
Hi,

thanks for your comment. am using below code,

public void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
reader = new PdfReader(sourcePdfPath);
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
pdfCopyProvider = new PdfCopy(sourceDocument,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
}
sourceDocument.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}

Please give me a suggestion, how to apply background color.
[no name] 5-Nov-13 7:20am    
I am unable to Test thsi code, Please check it and reply.
Thanks.

public void ExtractPages(string sourcePdfPath, string outputPdfPath, int startPage, int endPage)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
var pageSize = new Rectangle(216, 720);
pageSize.BackgroundColor(new Color(0, 0, 0));
reader = new PdfReader(sourcePdfPath);
//sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
sourceDocument = new Document(pageSize);
pdfCopyProvider = new PdfCopy(sourceDocument,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
sourceDocument.Open();
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
}
sourceDocument.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
K N R 5-Nov-13 7:23am    
Please include below code,


string inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");

int Startpage=1;
int endpage=2;

PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
// Intialize a new PdfReader instance with the contents of the source Pdf file:
reader = new PdfReader(sourcePdfPath);

// For simplicity, I am assuming all the pages share the same size
// and rotation as the first page:
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));

// Initialize an instance of the PdfCopyClass with the source
// document and an output file stream:
pdfCopyProvider = new PdfCopy(sourceDocument,
new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));

sourceDocument.Open();

// Walk the specified range and add the page copies to the output file:
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);

}

sourceDocument.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
[no name] 5-Nov-13 7:30am    
Just replace the Line in your code to these lines,

// sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));

var pageSize = new Rectangle(216, 720);
pageSize.BackgroundColor(new Color(0, 0, 0));
sourceDocument = new Document(pageSize);
K N R 5-Nov-13 7:55am    
i already tried, but it's not working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900